gpt4 book ai didi

android - 检测 MediaBrowserServiceCompat 是否正在运行

转载 作者:行者123 更新时间:2023-11-29 02:37:56 26 4
gpt4 key购买 nike

我正在使用这个 example code构建一个 MediaPlayer 服务。我或多或少地工作,但是如果用户返回到包含媒体控件的 Activity,我需要检测 MediaPlayer 处于什么状态。我的代码我当前使用的是 onResume 中的 NullReferenceException 错误,因为 getPlaybackState() 始终为 null

我刚开始使用 MediaSessionCompat,根据文档,我可以通过某种方式获取当前 session :

“创建 session 后, session 的所有者可以将其 session token 传递给其他进程,以允许它们创建 MediaControllerCompat 以与 session 交互。”

    public class MediaActivity extends AppCompatActivity {

private MediaBrowserCompat mMediaBrowserCompat;
private MediaControllerCompat mMediaControllerCompat;
private Activity mActivity;

@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_media);

mActivity = this;
mPlayButton = (Button)findViewById(R.id.btn_play);

mMediaBrowserCompat = new MediaBrowserCompat(
getApplicationContext(),
new ComponentName(mContext, MediaPlayerService.class),
mMediaBrowserCompatConnectionCallback,
getIntent().getExtras()
);

mMediaBrowserCompat.connect();

mPlayButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if( mCurrentState == STATE_PAUSED ) {
getSupportMediaController().getTransportControls().play();
mCurrentState = STATE_PLAYING;
mPlayButton.setText("Pause");
} else {
MediaControllerCompat.getMediaController(mActivity).getTransportControls().pause();
mCurrentState = STATE_PAUSED;
mPlayButton.setText("Play");
}
}
});

@Override
public void onResume() {
super.onResume();

if (MediaControllerCompat.getMediaController(mActivity).getPlaybackState().getState() == PlaybackStateCompat.STATE_PLAYING) {
mPlayButton.setText("Pause")
}
else{
mPlayButton.setText("Play")
}

}

private MediaBrowserCompat.ConnectionCallback mMediaBrowserCompatConnectionCallback = new MediaBrowserCompat.ConnectionCallback() {

@Override
public void onConnected() {
super.onConnected();
try {
mMediaControllerCompat = new MediaControllerCompat(PodcastEpisodeActivity.this, mMediaBrowserCompat.getSessionToken());
mMediaControllerCompat.registerCallback(mMediaControllerCompatCallback);
MediaControllerCompat.setMediaController(mActivity, mMediaControllerCompat);
MediaControllerCompat.getMediaController(mActivity).getTransportControls().playFromUri(Uri.parse("http://www.url.com"), extras);

} catch( RemoteException e ) {
Log.e(mActivity.getPackageName(), e.toString());
}
}
};


private MediaControllerCompat.Callback mMediaControllerCompatCallback = new MediaControllerCompat.Callback() {

@Override
public void onPlaybackStateChanged(PlaybackStateCompat state) {
super.onPlaybackStateChanged(state);

if (state == null ) return;

switch (state.getState()) {
case PlaybackStateCompat.STATE_PLAYING: {
mCurrentState = STATE_PLAYING;
break;
}
case PlaybackStateCompat.STATE_PAUSED: {
mCurrentState = STATE_PAUSED;
break;
}
}
}
};
}
}

最佳答案

我找不到通过可用 API 的方法,所以我只是跟踪正在通过本地存储 (sqlite) 播放的媒体并通过它更新显示。可能不是最优雅的解决方案,但它确实有效。

关于android - 检测 MediaBrowserServiceCompat 是否正在运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45922408/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com