1
I have the code below that opens a URL of Streaming of a web radio and plays/pauses her.
Got and bugs in this código
I don’t think so 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) No onCreate have the setOnClickListener button that tries to change its text. It simply does not work, the text does not change, but the line below is executed 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 to put
btnPlayPause.setText("Carregando...");
inside:
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, it’s harder here. 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 touch again, it gives error in line 43 inside the method tocaPausa()
.
player.setDataSource(URL);
That 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)
According to the documentation android this error occurs when the file indicated in
setDataSource
does not exist, checks whether theurl
is correct. Another thing is that you may need to inform thesetAudioStreamType
.– Icaro Martins
as I said, when clicking the button for the first time, the radio plays normally, it leads me to believe that the url is ok.
– Carlos Rocha
puts a
log
in theif
another in theelse
of histocaPausa
, I think the second time he’s been throughelse
because I believe this is still trueplayer.isPlaying()
because you just gavepause
.– Icaro Martins
If that’s the case to play again after one
pause
you need to call the functionstart
.– Icaro Martins
passed the first block even. so much that it stuck in the line of the URL. I did the Log test here
– Carlos Rocha
Then try to store something indicating that it is in
pause
, and within its functiontocaPausa
if you are inpause
just call thestart
to see if it works.– Icaro Martins
another option is to give instead
pause
call formediaPlayer.stop(); mediaPlayer.reset();
– Icaro Martins
discovered! note that every time I click play again, the player needs to be created again. there is the error
– Carlos Rocha
added if (player != null) { player.reset(); player.release(); player = null; } at the end
– Carlos Rocha
but it brought me another problem, the shipment needs to be done again.
– Carlos Rocha