我实现了一个音乐播放器但我的问题是,当我用关闭按钮关闭后打开应用程序音乐会自动停止。我想检查我的MediaBrowserSessionService是否正在运行或者告诉我其他方法。
private static final int STATE_PAUSED = 0;
private static final int STATE_PLAYING = 1;
private int mCurrentState;
private MediaBrowserCompat mMediaBrowserCompat;
private MediaControllerCompat mMediaControllerCompat;
private MediaBrowserCompat.ConnectionCallback mMediaBrowserCompatConnectionCallback = new MediaBrowserCompat.ConnectionCallback() {
@Override
public void onConnected() {
super.onConnected();
try {
mMediaControllerCompat = new MediaControllerCompat(MainActivity.this, mMediaBrowserCompat.getSessionToken());
mMediaControllerCompat.registerCallback(mMediaControllerCompatCallback);
setSupportMediaController(mMediaControllerCompat);
Log.i("Kunal","Kunal");
MediaControllerCompat mediaControllerCompat=getSupportMediaController();
Log.d("Controller", String.valueOf(mediaControllerCompat));
Log.d("PlayBackState", String.valueOf(getSupportMediaController().getPlaybackState()));
} catch( RemoteException e ) {
}
}
};
private MediaControllerCompat.Callback mMediaControllerCompatCallback = new MediaControllerCompat.Callback() {
@Override
public void onMetadataChanged(MediaMetadataCompat metadata) {
super.onMetadataChanged(metadata);
}
@Override
public void onPlaybackStateChanged(PlaybackStateCompat state) {
super.onPlaybackStateChanged(state);
if( state == null ) {
return;
}
switch( state.getState() ) {
case PlaybackStateCompat.STATE_PLAYING: {
mCurrentState = STATE_PLAYING;
MainActivity.this.startService(new Intent(MainActivity.this,BackgroundAudioService.class));
break;
}
case PlaybackStateCompat.STATE_PAUSED: {
mCurrentState = STATE_PAUSED;
break;
}
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getPermission();
settingFirstSong();
Log.i("Service","Running");
mMediaBrowserCompat = new MediaBrowserCompat(this, new ComponentName(this, BackgroundAudioService.class),
mMediaBrowserCompatConnectionCallback, this.getIntent().getExtras());
mMediaBrowserCompat.connect();
// Log.d("Controller", String.valueOf(mediaControllerCompat));
/*if(getSupportMediaController().getPlaybackState().getState()==2)
{
getSupportMediaController().getTransportControls().playFromMediaId(CurrentlyPlayingSongData.getTrackPath(), null);
getSupportMediaController().getTransportControls().seekTo(sharedPreferences.getInt("position",0));
}*/
songList = new SongList(this);
songDataArrayList = songList.getData();
BitmapExtraction bitmapExtraction = new BitmapExtraction(this, songDataArrayList);
bitmapExtraction.bitmapExtraction();
View bottomSheet = findViewById(R.id.design_bottom_sheet);
BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);
behavior.setHideable(false);
artistList = new ArtistList(this);
artistDatas = artistList.getData();
if (isNetworkAvailable()) {
AddDataToArtistDB addDataToArtistDB = new AddDataToArtistDB(this, artistDatas);
addDataToArtistDB.start();
}
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(pagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab);
tabLayout.setupWithViewPager(viewPager);
}
当我再次打开应用程序执行
时,这是我的实现 getSupportMediaController().getTransportControls().playFromMediaId("any.mp3", null);
再一次,我的播放器是从我开始做的歌曲?
我的错误
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v4.media.session.PlaybackStateCompat android.support.v4.media.session.MediaControllerCompat.getPlaybackState()' on a null object reference
at com.techweblearn.musicplayer.MainActivity.onCreate(MainActivity.java:115)
at android.app.Activity.performCreate(Activity.java:6303)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2402)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2509)
at android.app.ActivityThread.access$1000(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:5527)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)