简体   繁体   中英

MediaPlayer in BroadcastReceiver not working in Android

I have an issue getting MediaPlayer in a BroadcastReceiver to work. What I want to do in high level, is to play a sound and a vibration x minutes after a notification from Firebase comes, like a second reminder. I have a class that extends FirebaseMessagingService and in the onMessageReceived() method I call the setOnetimeTimer(this) which is:

public void setOnetimeTimer(Context context) {
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
    am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (1000 * 60 * 1), pi); // 1 minute
}

Then the AlarmManagerBroadcastReceiver() is:

public class AlarmManagerBroadcastReceiver extends BroadcastReceiver {

MediaPlayer mp = null;

@Override
public void onReceive(Context context, Intent intent) {
    if (mp != null) {
        mp.stop();
        mp.release();
        mp = null;
    }

    mp = MediaPlayer.create(context, R.raw.my_weird_sound);
    mp.start();
    Vibrator vib = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
    vib.vibrate(1000);

    mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            mp.release();
        }
    });
  }
}

The vibration is executed without a problem but the sound is not playing and the error that I am getting is:

E/MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0
E/MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0
D/MediaPlayer: setSubtitleAnchor in MediaPlayer

keep in mind also that I have AlarmManagerBroadcastReceiver in my manifest.

Any help would be appreciated!

These type of exception arise when MediaPlayer has not implemented its architecture properly. Achitecure

Now try this:

MediaPlayer mp = null;

    @Override
    public void onReceive(Context context, Intent intent) {
        if (mp != null && mp.isPlaying()) {
            mp.stop();
            mp.release();
            mp=null;
        }
        else {
            mp = null;
        }

        mp = MediaPlayer.create(context, R.raw.my_weird_sound);
        mp.start();
        Vibrator vib = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
        vib.vibrate(1000);

        mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                if (!mp.isPlaying()) {
                    mp.release();
                }
                else {
                    mp.stop();
                    mp.release();
                }

            }
        });
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM