简体   繁体   English

在所有活动中使用一个公共媒体播放器类

[英]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. 因为我仅在屏幕上显示活动时才需要Media Player。 Here is the new code: 这是新代码:

First, my public SingleMP (Media Player) class used in multiple classes: 首先,我的公共SingleMP(媒体播放器)类在多个类中使用:

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: 在另一个Java类中使用它的示例:

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. 看来我的用户界面工作更流畅。

Your media player instance is going to live on the main thread, which is the UI thread. 您的媒体播放器实例将驻留在主线程(即UI线程)上。 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 . 您也可以查看媒体播放器示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM