[英]Can updating an Activity from MediaBrowserService cause the Service to crash?
I have an audio app with a MediaBrowserService
.我有一个带有MediaBrowserService
的音频应用程序。 To update the SeekBar
in the Activity
, I have a Runnable
in the service that updates the MediaSession
extras every 250 milliseconds (this is to ensure the SeekBar
movement isn't janky).要更新Activity
中的SeekBar
,我在服务中有一个Runnable
,它每 250 毫秒更新一次MediaSession
附加功能(这是为了确保SeekBar
移动不会卡顿)。
Everything works fine except a small number of users are reporting that after a couple of minutes the playback consistently cuts out.一切正常,除了少数用户报告说几分钟后播放一直中断。 The logs don't show that onDestroy
or onPause
or onStop
of the MediaSessionCompat.Callback
is called or any of the error handling.日志不显示MediaSessionCompat.Callback
的onDestroy
或onPause
或onStop
被调用或任何错误处理。 It just stops.它只是停止。 However, I'm unable to recreate it myself.但是,我无法自己重新创建它。 Once the audio starts there is nothing running in the service but this.音频开始后,服务中没有任何运行,只有这个。 Is it possible updating the SeekBar
this way is causing the playback to stop?以这种方式更新SeekBar
是否可能导致播放停止?
public class MediaPlayerService extends MediaBrowserServiceCompat {
private Handler mMediaHandler;
private MediaSessionCompat mMediaSessionCompat;
private ExoPlayer player;
@Override
public void onCreate() {
super.onCreate();
mMediaSessionCompat = new MediaSessionCompat(this, MediaPlayerService.class.getSimpleName());
mMediaSessionCompat.setCallback(mMediaSessionCallback);
}
public void PlayAudio()
{
player = new ExoPlayer.Builder(this).build();
player.setMediaItem(MediaItem.fromUri(uri), position);
player.prepare();
player.play();
mMediaHandler = new Handler(Looper.getMainLooper());
mMediaHandler.post(mUpdateMediaPosition);
}
Runnable mUpdateMediaPosition = new Runnable() {
public void run() {
final Bundle extras = new Bundle();
extras.putInt("position", player.getCurrentPosition());
mMediaSessionCompat.setExtras(extras);
mMediaHandler.postDelayed(this, 250);
}
};
}
public class MediaActivity
{
private MediaBrowserCompat mMediaBrowserCompat;
private MediaControllerCompat mMediaController;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mMediaBrowserCompat = new MediaBrowserCompat(
getApplicationContext(),
new ComponentName(mContext, MediaPlayerService.class),
mMediaBrowserCompatConnectionCallback,
getIntent().getExtras());
mMediaBrowserCompat.connect();
}
MediaControllerCompat.Callback mMediaControllerCompatCallback = new MediaControllerCompat.Callback() {
@Override
public void onExtrasChanged(Bundle extras) {
super.onExtrasChanged(extras);
int position = extras.getInt("position");
seekBar.setProgress(position);
}
}
MediaBrowserCompat.ConnectionCallback mMediaBrowserCompatConnectionCallback = new MediaBrowserCompat.ConnectionCallback() {
@Override
public void onConnected() {
super.onConnected();
mMediaController = new MediaControllerCompat(mContext, mMediaBrowserCompat.getSessionToken());
mMediaController.registerCallback(mMediaControllerCompatCallback);
}
};
}
Answering my own question in case it helps anyone in the future but connecting to a MediaBrowserService
does not actually start the service.回答我自己的问题,以防将来对任何人有帮助,但连接到MediaBrowserService
并不会真正启动该服务。 You have to call startService
in the MediaBrowserService
(the documentation recommends when playback starts) or the MediaBrowserService
will stop when all connections to it disconnect.您必须在MediaBrowserService
中调用startService
(文档建议在播放开始时),否则MediaBrowserService
将在所有连接断开时停止。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.