Hasan Saykın
Hasan Saykın

Reputation: 148

One public media player class to be used across all activities

Updated.

After the recommendations, I decided to run media player in a new thread. Because I need Media Player only when activities are on the screen. Here is the new code:

First, my public SingleMP (Media Player) class used in multiple classes:

import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;

public class SingleMP implements Runnable
{
    public static MediaPlayer mp;
    private static Context context;
    private static Uri uri;

    public SingleMP(Context context, Uri uri){
    this.context= context;
    this.uri= uri;
    }

    @Override
    public void run(){
        try {
            if (mp != null) {
                mp.stop();
                mp.reset();
                mp.release();
                mp = null;
            }

            mp = MediaPlayer.create(context, uri);
            mp.start();
        } catch (Exception e) {
            if (mp != null) {
                mp.stop();
                mp.reset();
                mp.release();
                mp = null;
            }
            e.printStackTrace();
            mp = MediaPlayer.create(context, uri);
            mp.start();
        }
    }

    // Called in OnDestroy of used class.
    public static void mpstop()
    {
        if (mp != null) {
            mp.stop();
            mp.reset();
            mp.release();
            mp = null;
        }
    }
}

And an example of using it in another Java class:

public class MainMenu
{
    private Uri uri;
    private Runnable MPthread;

    public void onCreate(Bundle savedInstanceState)
    {
        RadioButton rbtnA = (RadioButton) findViewById(R.id.radio0);
        rbtnA.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                // Assign a sound from raw folder.
                uri =Uri.parse("android.resource://"+getPackageName()+"/raw/nice");
                MPthread = new SingleMP(MainMenu.this, uri));
                new Thread(MPthread).start();               
                }
             }
        });

        RadioButton rbtnB = (RadioButton) findViewById(R.id.radio1);
        rbtnB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                // Assign a sound from raw folder.
                uri =Uri.parse("android.resource://"+getPackageName()+"/raw/morning");
                MPthread = new SingleMP(MainMenu.this, uri));
                new Thread(MPthread).start();
                }
             }
         });

    }


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

    if(MPthread!=null) {
        SingleMP.mpstop();
    }

    }
}

What do you think? It seems that my UI works a little bit smoother.

Upvotes: 0

Views: 812

Answers (1)

Distwo
Distwo

Reputation: 11749

Your media player instance is going to live on the main thread, which is the UI thread. This is not recommended.

I would probably create a service that would create a new thread holding the media player. Each of your activities could then bind to the service to control the media player.

See section Extending the service class.

You can also look at the media player sample.

Upvotes: 3

Related Questions