I have the code below that opens a URL for Streaming from a web radio and rings / pauses.
I have and errors in this código
that I do not think at all and I ask for your help.
package carcleo.com.player;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.io.IOException;
public class player extends AppCompatActivity {
private MediaPlayer player;
private String URL;
private Button btnPlayPause;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.player);
URL = "rtsp://cdn-the-2.musicradio.com:80/LiveAudio/Capital";
btnPlayPause = (Button) findViewById(R.id.btnPlayPause);
btnPlayPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
btnPlayPause.setText("Carregando...");
if (player == null) player = new MediaPlayer();
tocaPausa();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
private void tocaPausa () throws IOException {
if (!player.isPlaying()) {
player.setDataSource(URL);
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
mediaPlayer.stop();
mediaPlayer.reset();
}
});
player.prepare();
player.start();
btnPlayPause.setText("Pausar");
} else {
player.pause();
btnPlayPause.setText("Tocar");
}
}
}
The 2 problems are:
A) On onCreate I have the setOnClickListener button that tries to change your text. It just does not work, the text does not change, but the line below runs normally.
btnPlayPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
btnPlayPause.setText("Carregando...");
if (player == null) player = new MediaPlayer();
tocaPausa();
} catch (IOException e) {
e.printStackTrace();
}
}
});
I tried putting
btnPlayPause.setText("Carregando...");
within:
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
btnPlayPause.setText("Carregando...");
mediaPlayer.stop();
mediaPlayer.reset();
}
});
But it turned out the same.
B) Well, here is more difficult. When I open the player, I click the button and after about 12 seconds the radio starts playing, when I click the button again, the radio is paused normally. But when I click again to play again, it gives error on line 43 of the tocaPausa()
method.
player.setDataSource(URL);
Here is the error:
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: carcleo.com.player, PID: 365
java.lang.IllegalStateException
at android.media.MediaPlayer.nativeSetDataSource(Native Method)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1078)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1039)
at carcleo.com.player.player.tocaPausa(player.java:43)
at carcleo.com.player.player.access$200(player.java:10)
at carcleo.com.player.player$1.onClick(player.java:30)
at android.view.View.performClick(View.java:5640)
at android.view.View$PerformClick.run(View.java:22455)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6165)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)