0

我試圖在我的服務中播放歌曲,並且能夠成功播放棒棒糖和上面的設備。我看到一些教程,它顯示使用classname方法我們可以爲pre棒棒糖設備這是我收到的錯誤在mediabuttonreceiver中獲取非法參數異常

FATAL EXCEPTION: main 
                    java.lang.RuntimeException: Unable to start service [email protected] 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)  

我這是怎麼調用的主要活動服務 結合部分

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); 

這是我mediasession方法我在哪裏得到的錯誤

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

+0

你爲什麼要叫兩個startService&bindService? – pantos27

回答

1

您應該在manifest.xml文件中聲明一個MediaButtonReceiver,該文件將所有媒體按鈕密鑰委託給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