0

I have an app that plays sounds on button press. These sounds loop continuously and are between 10-20 seconds long.

Everything works exactly as it should, however whenever I lock my phone by pressing the power button (lock button), the MediaPlayer starts to lag and the looping is no longer seamless or continuous and so there is a clear gap between each loop.

I'm using ogg files and when the screen is on there is no noticeable gap.

This leads to believe I need a wakelock to ensure the CPU is kept on, however even by using this, there is still noticeable and significant lag from the MediaPlayer when the phone is locked.

This is how I'm calling wakelock. Has anyone had this same issue before?

    // Create a new MediaPlayer to play this sound
    mp = MediaPlayer.create(this, resId);
    mp.setLooping(true);
    PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
    WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
    "MyWakelockTag");
    wakeLock.acquire();     
    mp.start();
rossd
  • 229
  • 1
  • 3
  • 14

1 Answers1

0

Are you using the media player as a service? If so: i had a similar problem (media player failed to trigger oncompletionlistener immediately when in lock-screen mode), i solve this by setting a wake lock for the activity AND the media player

//service:
mPlayer = new MediaPlayer();
mPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);

//activity:
PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "myWakeLock");
wakeLock.acquire();
leberknecht
  • 1,526
  • 15
  • 27
  • "adb shell dumpsys power" is saying `Activity`'s `wakeLock`, "myWakeLock" is never released. – John Mar 10 '15 at 11:20