One public media player class to be used across all activities

心不动则不痛 提交于 2019-12-12 04:34:25

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/41730394/one-public-media-player-class-to-be-used-across-all-activities

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!