首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过主活动了解会话中媒体播放器的状态

如何通过主活动了解会话中媒体播放器的状态
EN

Stack Overflow用户
提问于 2017-01-26 01:18:23
回答 1查看 555关注 0票数 1

如何知道通过服务运行的媒体播放器是正在运行还是已停止。目前,我正在使用操作调用服务。

如何检查MainActivity中的媒体播放器状态?这样我就可以对UI进行适当的更改!

下面是我实现mediaplayer的服务代码。

代码语言:javascript
复制
    public class MyService extends Service implements MediaPlayer.OnPreparedListener {

    private static final String ACTION_PLAY = "com.demo.tron.mediaplayerasaservice.PLAY";
    private static final String ACTION_STOP = "com.demo.tron.mediaplayerasaservice.STOP";

    public MediaPlayer mMediaPlayer = null;

    public int onStartCommand(Intent intent, int flags, int startId) {

        if (intent.getAction().equals(ACTION_PLAY)) {
            mMediaPlayer = new MediaPlayer(); // initialize it here
            mMediaPlayer.setOnPreparedListener(this);
            try{
                mMediaPlayer.setDataSource("http://mp3channels.webradio.antenne.de:80/antenne");
            }
            catch (IOException e)
            {
                Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
            }
            mMediaPlayer.prepareAsync(); // prepare async to not block main thread
        }
        else if (intent.getAction().equals(ACTION_STOP)){
            if (mMediaPlayer.isPlaying())
            {
                mMediaPlayer.stop();
                mMediaPlayer.reset();
            }
        }

        return START_STICKY;
    }

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

    /** Called when MediaPlayer is ready */
    @Override
    public void onPrepared(MediaPlayer player) {
        mMediaPlayer.start();
    }
}

下面是通过MAIN活动调用媒体播放器服务的代码:

代码语言:javascript
复制
button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this,MyService.class);
                intent.setAction("com.demo.tron.mediaplayerasaservice.PLAY");
                startService(intent);
            }
        });
EN

回答 1

Stack Overflow用户

发布于 2017-01-26 01:31:37

您需要检查服务是否已经在运行,然后采取相应的操作:

代码语言:javascript
复制
private boolean isServiceRunning(Class<?> serviceClass) {
    ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (serviceClass.getName().equals(service.service.getClassName())) {
            return true;
        }
    }
    return false;
}

然后在任何地方调用方法isServiceRunning,如下所示:

代码语言:javascript
复制
isServiceRunning(YourServiceClass.class)

定义一个PlayerConstants类来跟踪播放器的状态。它将包含所有静态变量:

代码语言:javascript
复制
public class PlayerConstants {
    //List of Songs
    public static ArrayList<MediaItem> SONGS_LIST = new ArrayList<MediaItem>();
    //song number which is playing right now from SONGS_LIST
    public static int SONG_NUMBER = 0;
    //song is playing or paused
    public static boolean SONG_PAUSED = true;
}

现在您需要检查服务是否正在运行,如果正在运行,歌曲可以暂停或已经播放,您可以从PlayerConstants获取暂停状态,否则歌曲正在播放。如果服务未运行,则没有播放任何歌曲。因此,您可以处理您的UI。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41857710

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档