0

我正在尝试在我的服务中播放歌曲,并且我能够为棒棒糖及以上设备成功播放它。我看到了一些教程,它表明使用类名方法我们可以为预棒棒糖设备做到这一点,这就是我得到的错误

 FATAL EXCEPTION: main
                                                                    java.lang.RuntimeException: Unable to start service beatbox.neelay.beatbox.MediaService@41e92b60 with Intent { cmp=beatbox.neelay.beatbox/.MediaService }: java.lang.IllegalArgumentException: MediaButtonReceiver component may not be null.
                                                                        at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2510)
                                                                        at android.app.ActivityThread.access$1900(ActivityThread.java:133)
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1295)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                        at android.os.Looper.loop(Looper.java:137)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:4793)
                                                                        at java.lang.reflect.Method.invokeNative(Native Method)
                                                                        at java.lang.reflect.Method.invoke(Method.java:511)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:808)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:575)
                                                                        at dalvik.system.NativeStart.main(Native Method)
                                                                     Caused by: java.lang.IllegalArgumentException: MediaButtonReceiver component may not be null.
                                                                        at android.support.v4.media.session.MediaSessionCompat$MediaSessionImplBase.<init>(MediaSessionCompat.java:1507)
                                                                        at android.support.v4.media.session.MediaSessionCompat.<init>(MediaSessionCompat.java:278)
                                                                        at beatbox.neelay.beatbox.MediaService.initMediaSession(MediaService.java:660)
                                                                        at beatbox.neelay.beatbox.MediaService.onStartCommand(MediaService.java:172)
                                                                        at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2493)
                                                                        at android.app.ActivityThread.access$1900(ActivityThread.java:133) 
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1295) 
                                                                        at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                        at android.os.Looper.loop(Looper.java:137) 
                                                                        at android.app.ActivityThread.main(ActivityThread.java:4793) 
                                                                        at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                        at java.lang.reflect.Method.invoke(Method.java:511) 
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:808) 
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:575) 
                                                                        at dalvik.system.NativeStart.main(Native Method) 

07

这就是我在主要活动绑定部分调用服务的方式

    private ServiceConnection serviceConnection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        // We've bound to LocalService, cast the IBinder and get LocalService instance
        MediaService.LocalBinder binder = (MediaService.LocalBinder) service;
        MainActivity.this.player = binder.getService();
        serviceBound = true;
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        serviceBound = false;
    }
};

这就是我调用服务的方式

Intent playerIntent = new Intent(getApplicationContext(), MediaService.class);
        playerIntent.setAction(Constants.ACTION.STARTFOREGROUND_ACTION);
        startService(playerIntent);
        bindService(playerIntent, serviceConnection, Context.BIND_AUTO_CREATE);

这是我收到错误的媒体会话方法

 private void initMediaSession() throws RemoteException {
    if (mediaSessionManager != null)  //mediaSessionManager exists

    mediaSessionManager  = new ComponentName(getApplicationContext(), MediaButtonReceiver.class);

    // Create a new MediaSession
    mediaSession = new MediaSessionCompat(getApplicationContext(), "Tag", mediaSessionManager, null);
    //Get MediaSessions transport controls
    transportControls = mediaSession.getController().getTransportControls();
    //set MediaSession -> ready to receive media commands
    mediaSession.setActive(true);
    //indicate that the MediaSession handles transport control commands
    // through its MediaSessionCompat.Callback.
    mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
    Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
    mediaButtonIntent.setClass(this, MediaButtonReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, mediaButtonIntent, 0);
    mediaSession.setMediaButtonReceiver(pendingIntent);

该应用程序在 android M 上正常运行,但在 android jellybean 上显示此错误

4

1 回答 1

2

您应该MediaButtonReceiver在 manifest.xml 文件中声明一个将所有媒体按钮键委托给 MediaSession 服务的文件

<receiver android:name="android.support.v4.media.session.MediaButtonReceiver" >
   <intent-filter>
     <action android:name="android.intent.action.MEDIA_BUTTON" />
   </intent-filter>
 </receiver>

更多在这里 https://developer.android.com/reference/android/support/v4/media/session/MediaButtonReceiver.html

和这里

https://developer.android.com/guide/topics/media-apps/mediabuttons.html

于 2017-07-20T18:23:24.293 回答