Java 设置MediaBrowserService和MediaSession时遇到问题
我想将我的应用程序更新到最新的android标准,所以我想实现MediaSession和更进一步的MediaBrowserCompat来控制我的播放 实际上,我得到了这个错误: java.lang.NullPointerException:尝试调用虚拟方法 空对象上的“boolean java.lang.String.equals(java.lang.Object)” 参考 位于android.os.Binder.queryLocalInterface(Binder.java:247) 在 android.service.media.imediabrowservice$Stub.asInterface(imediabrowservice.java:31) 在 android.media.browse.MediaBrowser$MediaServiceConnection.onServiceConnected(MediaBrowser.java:709) 在 LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:1209) 在 LoadedApk$servicedpatcher$RunConnection.run(LoadedApk.java:1226) 位于android.os.Handler.handleCallback(Handler.java:739) 位于android.os.Handler.dispatchMessage(Handler.java:95) 位于android.os.Looper.loop(Looper.java:135) 位于android.app.ActivityThread.main(ActivityThread.java:5294) 位于java.lang.reflect.Method.invoke(本机方法) 位于java.lang.reflect.Method.invoke(Method.java:372) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) 位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699) 由该代码引起的:Java 设置MediaBrowserService和MediaSession时遇到问题,java,android,playback,android-mediasession,Java,Android,Playback,Android Mediasession,我想将我的应用程序更新到最新的android标准,所以我想实现MediaSession和更进一步的MediaBrowserCompat来控制我的播放 实际上,我得到了这个错误: java.lang.NullPointerException:尝试调用虚拟方法 空对象上的“boolean java.lang.String.equals(java.lang.Object)” 参考 位于android.os.Binder.queryLocalInterface(Binder.java:247) 在 an
mMediaBrowser = new MediaBrowserCompat(this, new ComponentName(this, MusicService.class), new MediaBrowserCompat.ConnectionCallback() {
@Override
public void onConnectionSuspended() {
super.onConnectionSuspended();
}
@Override
public void onConnectionFailed() {
super.onConnectionFailed();
}
@Override
public void onConnected() {
super.onConnected();
if (MusicPlayer.isPreparing() || MusicPlayer.isPlaying()) {
Start_Timer();
}
}
}, null);
mMediaBrowser.connect();
这是我音乐服务的一部分:
public class MusicService extends MediaBrowserServiceCompat
implements AudioManager.OnAudioFocusChangeListener, MediaPlayer.OnPreparedListener, MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener {
@Override
public void onCreate() {
super.onCreate();
InitMediaPlayer();
mMediaSession = new MediaSessionCompat(getApplicationContext(), MusicService.class.getSimpleName());
mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
mPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
iCurrentSongIndex = 0;
iSeekTo = -1;
bPlayingFromQueue = false;
bStarted = bPreparing = bRestartAfterLoss = bControlReceiverRegistered = false;
mPlayMode = PlayMode.PASS;
mSongQueue = new ArrayList<>();
nManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
int iRequestResult = mAudioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
if (iRequestResult != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
Toast.makeText(getApplicationContext(), "Couldn't gain the Permission to play music!", Toast.LENGTH_LONG).show();
stopForeground(true);
stopSelf();
System.exit(0);
return;
}
mControlReceiver = new MediaControlReceiver();
IntentFilter infNotification = new IntentFilter();
infNotification.addAction(MediaControlReceiver.NOTIFY_PLAYPAUSE);
infNotification.addAction(MediaControlReceiver.NOTIFY_PREVIOUS);
infNotification.addAction(MediaControlReceiver.NOTIFY_NEXT);
infNotification.addAction(MediaControlReceiver.NOTIFY_CANCEL);
infNotification.addAction(AudioManager.ACTION_AUDIO_BECOMING_NOISY);
if (!bControlReceiverRegistered) {
registerReceiver(mControlReceiver, infNotification);
bControlReceiverRegistered = true;
}
mRemoteControlComponent = new ComponentName(this, RemoteControlReceiver.class);
mAudioManager.registerMediaButtonEventReceiver(mRemoteControlComponent);
getTheme().applyStyle(RuntimeInfo.getThemeID(), true);
}
private Notification Build_Notification() {
NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(this);
nBuilder.setShowWhen(false);
TypedValue typedValue = new TypedValue();
getTheme().resolveAttribute(R.attr.colorPrimary, typedValue, true);
int iPrimaryColor = typedValue.data;
nBuilder.setColor(iPrimaryColor);
Intent notIntent = new Intent(getApplicationContext(), MainActivity.class);
notIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent notOpenOnClick = PendingIntent.getActivity(getApplicationContext(), 0, notIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mMediaSession.setActive(true);
ArtworkProvider artworkProvider = new ArtworkProvider(this);
MediaMetadataCompat.Builder mMetaDataBuilder = new MediaMetadataCompat.Builder();
Bitmap bCover = artworkProvider.getAlbumArtwork(getCurrentSong().getAlbumID(), 150, 150);
mMetaDataBuilder
.putString(MediaMetadataCompat.METADATA_KEY_TITLE, getCurrentSong().getTitle())
.putString(MediaMetadataCompat.METADATA_KEY_ALBUM, getCurrentSong().getAlbum())
.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, getCurrentSong().getArtist());
String ArtworkOnLockscreenKey = getString(R.string.keyArtworkOnLockscreen);
boolean bArtworkOnLockscreen = mPreferences.getBoolean(ArtworkOnLockscreenKey, true);
if (bArtworkOnLockscreen) {
mMetaDataBuilder.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, bCover);
}
MediaMetadataCompat mMetadata = mMetaDataBuilder.build();
mMediaSession.setMetadata(mMetadata);
mMediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
nBuilder.setSmallIcon(R.drawable.not_icon)
.setContentTitle(mMetadata.getString(MediaMetadataCompat.METADATA_KEY_TITLE))
.setContentText(mMetadata.getString(MediaMetadataCompat.METADATA_KEY_ALBUM) + Constants.Char.SEPERATOR_DOT + mMetadata.getString(MediaMetadataCompat.METADATA_KEY_ARTIST))
.setLargeIcon(bCover)
.setContentIntent(notOpenOnClick);
if (MainActivity.isActive()) {
nBuilder.setOngoing(true);
}
PendingIntent pPrevious = MediaStyleHelper.getActionIntent(getApplicationContext(), KeyEvent.KEYCODE_MEDIA_PREVIOUS);
PendingIntent pPlayPause = MediaStyleHelper.getActionIntent(getApplicationContext(), KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE);
PendingIntent pNext = MediaStyleHelper.getActionIntent(getApplicationContext(), KeyEvent.KEYCODE_MEDIA_NEXT);
PendingIntent pCancel = MediaStyleHelper.getActionIntent(getApplicationContext(), KeyEvent.KEYCODE_MEDIA_STOP);
nBuilder.addAction(R.drawable.ic_previous_48dp, MediaControlReceiver.NOTIFY_PREVIOUS, pPrevious);
if (isPreparing() || isPlaying()) {
nBuilder.addAction(R.drawable.ic_pause_48dp, MediaControlReceiver.NOTIFY_PLAYPAUSE, pPlayPause);
}
else {
nBuilder.addAction(R.drawable.ic_play_48dp, MediaControlReceiver.NOTIFY_PLAYPAUSE, pPlayPause);
}
nBuilder.addAction(R.drawable.ic_next_48dp, MediaControlReceiver.NOTIFY_NEXT, pNext);
nBuilder.setStyle(new NotificationCompat.MediaStyle()
.setMediaSession(mMediaSession.getSessionToken())
.setShowActionsInCompactView(1, 2)
.setShowCancelButton(true)
.setCancelButtonIntent(pCancel));
nBuilder.setDeleteIntent(pCancel);
mNotification = nBuilder.build();
return mNotification;
}
}
公共类音乐服务扩展MediaBrowserServiceCompat
实现AudioManager.OnAudioFocusChangeListener、MediaPlayer.OnPreparedListener、MediaPlayer.OnCompletionListener、MediaPlayer.OnErrorListener{
@凌驾
public void onCreate(){
super.onCreate();
InitMediaPlayer();
mmediasion=newMediaSessionCompat(getApplicationContext(),MusicService.class.getSimpleName());
mAudioManager=(AudioManager)getSystemService(音频服务);
mPreferences=PreferenceManager.GetDefaultSharedReferences(getApplicationContext());
iCurrentSongIndex=0;
iSeekTo=-1;
bPlayingFromQueue=false;
bStart=bPreparing=bRestartAfterLoss=bControlReceiverRegistered=false;
mPlayMode=PlayMode.PASS;
mSongQueue=newarraylist();
nManager=(NotificationManager)getSystemService(通知服务);
int iRequestResult=mAudioManager.requestAudioFocus(这是AudioManager.STREAM\u音乐,AudioManager.AUDIOFOCUS\u增益);
如果(iRequestResult!=AudioManager.AUDIOFOCUS请求已批准){
Toast.makeText(getApplicationContext(),“无法获得播放音乐的权限!”,Toast.LENGTH\u LONG.show();
停止前景(真);
stopSelf();
系统出口(0);
返回;
}
mControlReceiver=新的MediaControlReceiver();
IntentFilter infNotification=新建IntentFilter();
infNotification.addAction(MediaControlReceiver.NOTIFY\u PLAYPAUSE);
infNotification.addAction(MediaControlReceiver.NOTIFY_PREVIOUS);
infNotification.addAction(MediaControlReceiver.NOTIFY_NEXT);
infNotification.addAction(MediaControlReceiver.NOTIFY\u CANCEL);
infNotification.addAction(AudioManager.ACTION\u AUDIO\u变得嘈杂);
如果(!bControlReceiverRegistered){
注册接收人(mControlReceiver,infNotification);
bControlReceiverRegistered=true;
}
mRemoteControlComponent=新组件名称(这是RemoteControlReceiver.class);
mAudioManager.RegisterMaidiaButtoneVentreceiver(mRemoteControlComponent);
getTheme().applyStyle(RuntimeInfo.getThemeID(),true);
}
私有通知生成通知(){
NotificationCompat.Builder nBuilder=新的NotificationCompat.Builder(此);
nBuilder.setshowhen(false);
TypedValue TypedValue=新的TypedValue();
getTheme().resolveAttribute(R.attr.colorPrimary,typedValue,true);
int iPrimaryColor=typedValue.data;
nBuilder.setColor(iPrimaryColor);
Intent notIntent=newintent(getApplicationContext(),MainActivity.class);
notIntent.addFlags(Intent.FLAG\u ACTIVITY\u CLEAR\u TOP);
PendingEvent notOpenOnClick=PendingEvent.getActivity(getApplicationContext(),0,notIntent,PendingEvent.FLAG_UPDATE_CURRENT);
mmediasion.setActive(真);
ArtworkProvider ArtworkProvider=新的ArtworkProvider(此);
MediaMetadataCompat.Builder mMetaDataBuilder=新的MediaMetadataCompat.Builder();
位图bCover=artworkProvider.getAlbumArtwork(getCurrentSong().getAlbumID(),150150);
mMetaDataBuilder
.putString(MediaMetadataCompat.METADATA\u KEY\u TITLE,getCurrentSong().getTitle())
.putString(MediaMetadataCompat.METADATA\u KEY\u相册,getCurrentSong().getAlbum())
.putString(MediaMetadataCompat.METADATA_KEY_ARTIST,getCurrentSong().getArtist());
String-artworknlockscreen=getString(R.String.keyartworknlockscreen);
boolean BartWorkNLockscreen=mPreferences.getBoolean(ArtWorkNLockscreen,true);