MediaPlayer sometimes not preparing when screen is locked

China☆狼群 提交于 2019-12-06 00:52:47
Dominik Seemayr

I might have found the answer in another thread where some faced the same problem while streaming music here.

My final solution is to use a WakeLock, wich I require before preparing a Song and release again onPrepared and onError and onDestroy of my Service. It's important to release it again to safe Battery. Make sure to check if the WakeLock is held before releasing it.

I create my WakeLock like this in onCreate of my Service:

PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MusicService");

aquire:

wakeLock.acquire();

release:

if(wakeLock.isHeld())
        wakeLock.release();

Tested the Playback now for about 10 Minutes and didn't stop until now. I don't know if this is a very battery-safing solution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!