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);
}
}