Android error while loading - MediaButtonReceiver may not be valid

I have a problem in my Unity app where Android crashes on load on a device running Android 4.3 The app loads a custom plugin I wrote to play music in the background when the app is paused. It uses the Android MediaBrowser API and therefore I am using Compatibility versions, so continue to support previous Android versions.

06-07 09:59:37.000: E/AndroidRuntime(1748): FATAL EXCEPTION: main
06-07 09:59:37.000: E/AndroidRuntime(1748): java.lang.Error: FATAL EXCEPTION [main]
06-07 09:59:37.000: E/AndroidRuntime(1748): Unity version     : 5.6.1f1
06-07 09:59:37.000: E/AndroidRuntime(1748): Device model      : samsung GT-I9300
06-07 09:59:37.000: E/AndroidRuntime(1748): Device fingerprint: samsung/m0xx/m0:4.3/JSS15J/I9300XXUGMK6:user/release-keys
06-07 09:59:37.000: E/AndroidRuntime(1748): Caused by: java.lang.RuntimeException: Unable to create service com.help.stressfree.mediabrowser.MusicService: java.lang.IllegalArgumentException: MediaButtonReceiver component may not be null.
06-07 09:59:37.000: E/AndroidRuntime(1748):     at android.app.ActivityThread.handleCreateService(ActivityThread.java:2697)
06-07 09:59:37.000: E/AndroidRuntime(1748):     at android.app.ActivityThread.access$1700(ActivityThread.java:159)
06-07 09:59:37.000: E/AndroidRuntime(1748):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1404)
06-07 09:59:37.000: E/AndroidRuntime(1748):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-07 09:59:37.000: E/AndroidRuntime(1748):     at android.os.Looper.loop(Looper.java:176)
06-07 09:59:37.000: E/AndroidRuntime(1748):     at android.app.ActivityThread.main(ActivityThread.java:5419)
06-07 09:59:37.000: E/AndroidRuntime(1748):     at java.lang.reflect.Method.invokeNative(Native Method)
06-07 09:59:37.000: E/AndroidRuntime(1748):     at java.lang.reflect.Method.invoke(Method.java:525)
06-07 09:59:37.000: E/AndroidRuntime(1748):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
06-07 09:59:37.000: E/AndroidRuntime(1748):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
06-07 09:59:37.000: E/AndroidRuntime(1748):     at dalvik.system.NativeStart.main(Native Method)
06-07 09:59:37.000: E/AndroidRuntime(1748): Caused by: java.lang.IllegalArgumentException: MediaButtonReceiver component may not be null.
06-07 09:59:37.000: E/AndroidRuntime(1748):     at android.support.v4.media.session.MediaSessionCompat$MediaSessionImplBase.<init>(MediaSessionCompat.java:1054)
06-07 09:59:37.000: E/AndroidRuntime(1748):     at android.support.v4.media.session.MediaSessionCompat.<init>(MediaSessionCompat.java:176)
06-07 09:59:37.000: E/AndroidRuntime(1748):     at android.support.v4.media.session.MediaSessionCompat.<init>(MediaSessionCompat.java:118)
06-07 09:59:37.000: E/AndroidRuntime(1748):     at com.help.stressfree.mediabrowser.MusicService.onCreate(MusicService.java:169)
06-07 09:59:37.000: E/AndroidRuntime(1748):     at android.app.ActivityThread.handleCreateService(ActivityThread.java:2687)
06-07 09:59:37.000: E/AndroidRuntime(1748):     ... 10 more

      

My overridden Unity activity has this in onCreate

protected void onCreate(Bundle savedInstanceState) {

        // call UnityPlayerActivity.onCreate()
        super.onCreate(savedInstanceState);

        Instance = this;

        LogHelper.d(TAG, "ThriveUnityPlayerActivity.onCreate");

        //Intent intent = new Intent(this, MusicPlayerActivity.class);
        //startActivity(intent);

        Intent intent = new Intent(this, MusicService.class);
        startService(intent);
    }

      

And my MusicService class has the following onCreate function

@Override
     public void onCreate() {
         super.onCreate();
         LogHelper.d(TAG, "onCreate");

         //mPlayingQueue = new ArrayList<>();
         //mMusicProvider = new MusicProvider();
         //mPackageValidator = new PackageValidator(this);

         mMyServiceHandler = new Handler()
         {
             //here we will receive messages from activity(using sendMessage() from activity)
             public void handleMessage(Message msg)
             {
                 LogHelper.i(TAG,"handleMessage(Message msg)" );
                 switch(msg.what)
                 {
                     case 0:
                         PlayTrackByName((String) msg.obj);

                         break;

                     default:
                         break;
                 }
             }
         };

         // Start a new MediaSession
         mSession = new MediaSessionCompat(this, "MusicService");
         setSessionToken(mSession.getSessionToken());
         mSession.setCallback(new MediaSessionCallback());
         mSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
             MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);

         mPlayback = new Playback(this/*, mMusicProvider*/);
         mPlayback.setState(PlaybackStateCompat.STATE_NONE);
         mPlayback.setCallback(this);
         mPlayback.start();

         Context context = getApplicationContext();
         Intent intent = new Intent(context, MusicPlayerActivity.class);
         PendingIntent pi = PendingIntent.getActivity(context, 99 /*request code*/,
                 intent, PendingIntent.FLAG_UPDATE_CURRENT);
         mSession.setSessionActivity(pi);

         Bundle extras = new Bundle();
         //CarHelper.setSlotReservationFlags(extras, true, true, true);
         mSession.setExtras(extras);

         updatePlaybackState(null);

         mMediaNotificationManager = new MediaNotificationManager(this);
     }

      

Does anyone know what crash means and how can I fix it?

+3


source to share


2 answers


Try adding a media button receiver in the manifest under the action that controls the media I've encountered with this 4.4 launch, but not higher versions.

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

      



and read; Media Button Receiver

+6


source


I have the same problem due to my mistake. I have defined two MEDIA_BUTTON actions in AndroidManifest.xml.

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

      



So, I remove one of them and the error is rejected.

+1


source







All Articles