MediaPlayer sometimes not preparing when screen is locked

爷,独闯天下 提交于 2019-12-22 10:23:37

问题


I have a MusicService for MediaPlayback, wich uses a MediaPlayer with the settings:

player.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);

and those Listeners are set:

OnPreparedListener, OnCompletionListener, OnErrorListener, OnSeekCompleteListener

The MediaPlayer is used for mp3-Playback. When one Song is finished, onCompletion is called. Then PlayNext is called, wich resets the MediaPlayer, then sets the Datasource with the URI of the next Track. Uri is loaded with:

Uri trackUri = ContentUris .withAppendedId(android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, songId);

Then the Player is prepared, then the player is started. This is working fine, but only sometimes, when the Device is locked and played about 1-3 Songs while it was locked and the next Song should start, the Player doesn't prepare until i hit the power button. Figured out, that if I don't hit the PowerButton it takes the Player about 2 Minutes to prepare.

I've Logged everything now and made a few custom outputs with Log.e(...). This is put out before prepare() (prepareAsync() delivers the same result) is called: E/MusicService: Preparing now...

This is put out, when onPrepared is called:

E/MusicService: Player prepared.

So this is the full Device-Output after "Preparing now..." is put out:

04-02 13:54:55.506 12517-12517/at.htlleonding.musync E/MusicService: Preparing now.
04-02 13:54:55.525 811-888/? E/libsuspend: Error writing to /sys/power/state: Device or resource busy
04-02 13:54:55.544 246-14756/? D/offload_visualizer: thread exit
04-02 13:54:55.546 246-14754/? V/offload_effect_bundle: offload_effects_bundle_hal_stop_output output 1879 pcm_id 9
04-02 13:54:55.546 246-14754/? D/hardware_info: hw_info_append_hw_type : device_name = speaker
04-02 13:54:55.549 246-14752/? E/audio_hw_primary: offload_thread_loop: Compress handle is NULL
04-02 13:54:55.549 246-924/? D/audio_hw_primary: adev_close_output_stream: enter:stream_handle(0xb5bfa640)
04-02 13:54:55.549 246-924/? D/audio_hw_primary: out_standby: enter: stream (0xb5bfa640) usecase(3: compress-offload-playback)
04-02 13:54:55.555 246-924/? W/AudioFlinger: moveEffectChain_l() effect chain for session 0 not on source thread 0xb59fa000
04-02 13:54:55.611 246-15030/? I/FFmpegExtractor: android-source:0xb1834060
04-02 13:54:55.820 811-888/? E/libsuspend: Error writing to /sys/power/state: Device or resource busy
04-02 13:54:55.972 246-15030/? I/FFMPEG: [mp3 @ 0xae2f4400] Skipping 0 bytes of junk at 2177007.

... Then theres no output until i hit the PowerButton. Then the Song is prepared.

If someone is interested in the full output after I hit the PowerButton until "Player prepared" is called, I created a Gist here.

Sidenote: While the App is used, a few Album-Covers are displayed in some Fragments. They are loaded with Picasso, so I don't need to worry about memory caching. Some ImageViews are filled without Picasso (for example the ImageViews that hold the drawables of my PlayerControls). Maybe there are Problems with the memory/resources?


回答1:


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



来源:https://stackoverflow.com/questions/36373449/mediaplayer-sometimes-not-preparing-when-screen-is-locked

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