user11255734
user11255734

Reputation:

Implement Background Service on a Media Player

I'm trying to implement the background service on a media player that runs a web radio. But I'm a beginner in programming with java/Android and I'm not able to implement the codes I've seen as examples.

This is my MainActivity.java:

public class MainActivity extends AppCompatActivity {
    Button button_play;
    boolean prepared = false;
    boolean started = false;
    String Stream = "UrlFromWebRadio";
    // in my case i use http://wz5.dnip.com.br/litoral/litoral.stream/playlist.m3u8
    MediaPlayer mediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button_play = findViewById(R.id.button_play);
        button_play.setEnabled(false);
        button_play.setText("Loading");
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        new PlayerTask().execute(Stream);

        button_play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(started){
                    started = false;
                    mediaPlayer.pause();
                    button_play.setText("Play");
                }
                else{
                    started = true;
                    mediaPlayer.start();
                    button_play.setText("Stop");
                }
            }
        });
    }

    class PlayerTask extends AsyncTask<String, Void, Boolean> {
        @Override
        protected Boolean doInBackground(String... strings){
            try{
                mediaPlayer.setDataSource(strings[0]);
                mediaPlayer.prepare();
                prepared = true;
            }
            catch (IOException e){
                e.printStackTrace();
            }
            return prepared;
        }

        @Override
        protected void onPostExecute(Boolean aBoolean){
            super.onPostExecute(aBoolean);
            button_play.setEnabled(true);
            button_play.setText("Play");
        }
    }

    @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();
        if(prepared){
            mediaPlayer.release();
        }
    }
}

This is my example class for Service (WebRadioService.java):

public class WebRadioService extends Service{
    MediaPlayer mediaPlayer;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        Toast.makeText(this, "Service Created", Toast.LENGTH_SHORT).show();
        //
        // mediaPlayer = MediaPlayer.create(this, R.drawable.test);
        // the line above will play specific file
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();
        mediaPlayer.start();
        return super.onStartCommand(intent, flags, startId);
    }
    @Override
    public void onDestroy() {
        Toast.makeText(this, "Service Finalized", Toast.LENGTH_SHORT).show();
        mediaPlayer.release();
    }
}

This is how I currently have my code, and the MainActivity works. And now wanted to implement the background service. But I could not with the examples I saw now, probably because of lack of knowledge. Can someone help?

Upvotes: 1

Views: 339

Answers (1)

user9032612
user9032612

Reputation:

You have to use something called Android Services.

From the docs:

"A Service is an application component representing either an application's desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use."

START_STICKY

If the system kills the service after onStartCommand() returns, recreate the service and call onStartCommand(), but do not redeliver the last intent. Instead, the system calls onStartCommand() with a null intent unless there are pending intents to start the service. In that case, those intents are delivered. This is suitable for media players (or similar services) that are not executing commands but are running indefinitely and waiting for a job.

Upvotes: 1

Related Questions