0

First of all, I'm new to Java. Second, my intents here are as follows:

User clicks button (playPause) -> button toggles to pause drawable (pause1) and stream begins and user clicks button -> stream pauses and button toggles to play drawable(play1).

Now my problem is how to implement this behavior, an onClick method, inside of the current method playPauseMusic which contains an onPrepared method that is used to prepare the MediaPlayer asynchronously.

My intuition is to make a check for isPlaying and toggle from there, but my attempts so far have ended in failure.

Here is the relevant code and thank you for your time:

radio.java

package com.example.jacob.wutk;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;

import java.io.IOException;

public class radio extends AppCompatActivity {

    /** Called when the user touches the button */

    public void playMusic(View view) throws IOException {
        String url = "http://streamer.cci.utk.edu:8000/wutk-vorbis"; // your URL here
        MediaPlayer mediaPlayer = new MediaPlayer();

        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

            public void onPrepared(MediaPlayer mediaPlayer){
                mediaPlayer.start();
            }
        });
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlayer.setDataSource(url);
        mediaPlayer.prepareAsync();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_radio);
    }
}
Nisarg
  • 1,358
  • 14
  • 30
McLemore
  • 605
  • 1
  • 6
  • 15
  • 1
    Have you read this discussion? http://stackoverflow.com/questions/18120174/how-to-play-and-pause-in-only-one-button-android – jakubbialkowski Jul 15 '16 at 06:55
  • @jakubbialkowski Yes I have. Again, I'm very new to this language, and have had no success in implementing the ideas found there successfully. – McLemore Jul 15 '16 at 07:00
  • I guess this tutorial might be good start for you: http://www.tutorialspoint.com/android/android_mediaplayer.htm It is fully featured, so my suggestion is to basically copy paste content from tutorial and play with that. Then if you have any specific question regarding one particular element ask it. Your current question is to broad to cover in one answer. – jakubbialkowski Jul 15 '16 at 07:05
  • Link's dead @jakubbialkowski, but I found the tutorial. – McLemore Jul 15 '16 at 07:07

1 Answers1

0

I hope this helps.

public class radio extends AppCompatActivity {
 MediaPlayer mediaPlayer;
boolean prepared=false;

public void playMusic(View view) throws IOException {

    playpause();

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_radio);
    mediaPlayer = new MediaPlayer();
    String url = "http://streamer.cci.utk.edu:8000/wutk-vorbis"; 
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mediaPlayer.setDataSource(url);
    mediaPlayer.prepareAsync();
    mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

        public void onPrepared(MediaPlayer mediaPlayer){
            prepared=ture;
        }
    });
}
}

public void playPause() {
if (!mediaplayer.isPlaying()&&prepared) {
     mediaplayer.start();
    mediaplayer.setImageResource(R.drawable.ic_pause);
} else if(mediaplayer.isPlaying()) {

    mediaplayer.pause();
    mediaplayer.setImageResource(R.drawable.ic_play);
}
}
jelmood jasser
  • 878
  • 1
  • 13
  • 30