Mediaplayer stops after a few seconds

Asked

Viewed 33 times

0

I’m developing a radio app, to acquire knowledge, so I took a stream and I took this code:

package tk.davidev.android.ews;

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

import java.io.IOException;

public class WebRadio extends AppCompatActivity {


private final static String stream = "http://170.75.144.170:10262/;";
Button play;
MediaPlayer mediaPlayer;
boolean started = false;
boolean prepared = false;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_web_radio);

    play = (Button) findViewById(R.id.btnWebRadio);
    play.setEnabled(false);
    play.setText("Carregando stream..");
    mediaPlayer = new MediaPlayer();
    mediaPlayer.setLooping(true);
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

    play.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if (started) {
                mediaPlayer.pause();
                started = false;
                play.setText("Play");
                mediaPlayer.setLooping(true);
            } else {
                mediaPlayer.start();
                started = true;
                mediaPlayer.setLooping(true);
                play.setText("Pausar");
            }

        }
    });

    new PlayTask().execute(stream);
}

@Override
protected void onPause() {
    super.onPause();
   if(started)
        mediaPlayer.pause();

}

@Override
protected void onResume() {
    super.onResume();
    if(!started)
        mediaPlayer.start();
}

@Override
protected void onDestroy() {
    super.onDestroy();
}

private class PlayTask extends AsyncTask<String, Void, Boolean> {

    @Override
    protected Boolean doInBackground(String... strings) {

        try {
            mediaPlayer.setDataSource(strings[0]);
            mediaPlayer.prepare();
            prepared = true;
            mediaPlayer.setLooping(true);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return prepared;
    }

    @Override
    protected void onPostExecute(Boolean aBoolean) {
        super.onPostExecute(aBoolean);
        play.setEnabled(true);
        play.setText("Play");
        mediaPlayer.setLooping(true);

    }
}

}

It works! But after a 2/3 seconds it stops automatically, and if you give Pause and Play it does not come back, only restarting the Activity.

  • try this solution in the original OS! I hope it works!

  • I have tried, however, without result.

  • See that it instantiates the Playtask class and executes it in the onCreate event, so it only works at the time of creation. As for running only a few seconds I imagine it is because the stream that this IP provides is just a small demo even.

  • Is the interruption always at the same point? Check how the app’s memory consumption is.

  • no, with other Apis works just right.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.