Android MediaPlayer Explanation with Service

Android MediaPlayer Explanation with Service

Hello everyone, in this article we are going to talk about MediaPlayer usage in Android with Java. We will make an example about Android MediaPlayer and see how it works.

Let's get started.

I will use the mediaplayer with my own service.I want you to think we are going to create a musicplayer application and we will play our musics from a background service.

If you do not have any idea how to use an android service please take a look at this article: https://thecodeprogram.com/explanation-of-android-services-with-example

First import the required library.


import android.media.MediaPlayer;

And create a new class named MusicPlayerService and extend this class with Service SuperClass. After it create MediaPlayer component. Initialize this mediaPlayer class at onCreate of service. Also we are setting a listener to update media player is prepared.

Below code block will do all of these.


public class MusicPlayerService extends Service  {

    MediaPlayer mediaPlayer;
    boolean isPlayerReady  false;

    @Override
    public void onCreate() {
        mediaPlayer = new MediaPlayer();

        //To know media player is ready and music file loaded
       mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                isPlayerReady  = true;
                Log.d("media_is_ready","media_is_ready");
            }
        });
     }
}

Now we need to set the music files and and prepare for playing music. After it I strongly recommend you to create a listener to update music playing is ended. Let's take a look at below code block to see how to do it.


//resetting MediaPlayer is always good.
mediaPlayer.reset();

try {

       mediaPlayer.setDataSource(_music.musicPath);

       //You can prepare the mediaplayer syncrhonised or asynchronised.
       //chose one of them.
       mediaPlayer.prepare();
       mediaPlayer.prepareAsync();

   } catch (IOException e) {
       Log.e("start_playing", "play: ", e);
   }

     //After it I strongly recommend to create a listener to when the setted music playing is completed.
     mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                Log.d("music_completed", "music_completed");
                if (startToPlay ) {
                    fnc_goNextMusic();
                }
            }
        });

Now let's use these two code blocks together.



import android.media.MediaPlayer;

public class MusicPlayerService extends Service  {

    MediaPlayer mediaPlayer;
    boolean isPlayerReady  false;

    @Override
    public void onCreate() {
        mediaPlayer = new MediaPlayer();

        //To know media player is ready and music file loaded
       mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                isPlayerReady  = true;
                Log.d("media_is_ready","media_is_ready");
            }
        });

     //resetting MediaPlayer is always good.
     mediaPlayer.reset();

     try {

       mediaPlayer.setDataSource("music_file_path");

       //You can prepare the mediaplayer syncrhonised or asynchronised.
       //chose one of them.
       mediaPlayer.prepare();
       mediaPlayer.prepareAsync();

     } catch (IOException e) {
        Log.e("start_playing", "play: ", e);
     }

     //After it I strongly recommend to create a listener to when the setted music playing is completed.
     mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                Log.d("music_completed", "music_completed");
               // music is completed and you can go the next music
            }
        });

     }
}

That is all in this article.

Have a good music playing

Burak Hamdi TUFAN


Tags


Share this Post

Send with Whatsapp

Post a Comment

Success! Your comment sent to post. It will be showed after confirmation.
Error! There was an error sending your comment. Check your inputs!

Comments

  • There is no comment. Be the owner of first comment...