MediaPlayer sometimes doesn’t get ready when the screen is locked

I have a MusicService for MediaPlayback that uses MediaPlayer with settings:

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

and these listeners are set:

 OnPreparedListener, OnCompletionListener, OnErrorListener, OnSeekCompleteListener 

MediaPlayer is used to play mp3 files. When one song is finished, onCompletion is called. Then a PlayNext is called, which resets MediaPlayer, and then sets the data source with the URI of the next track. Uri is loading:

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

Then the player is ready, then playback will begin. This works fine, but only sometimes, when the device is locked and about 1-3 songs are played, when it was locked , and the next song should start, the player does not prepare until I hit the power button. It turned out that if I did not get into PowerButton, you would need about 2 minutes for the player.

Now I registered everything and made some user exits with Log.e (...). This is issued before prepare () (prepareAsync () returns the same result): E/MusicService: Preparing now...

This is issued when onPrepared is called:

 E/MusicService: Player prepared. 

So, this is the complete output of the device after the release of "Preparing Now ...":

 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 PowerButton. Then the song is ready.

If someone is interested in a complete exit after I hit PowerButton until it calls “Player prepared,” I created a Gist here .

Sidenote: Although the application is in use, some fragments display several album art. They are loaded by Picasso, so I don’t have to worry about memory caching. Some ImageViews are populated without Picasso (for example, ImageViews that contain drawings of my PlayerControls). Perhaps there are problems with memory / resources?

+3
android media-player media android-mediaplayer prepare
source share
1 answer

I could find the answer in another thread where some ran into the same problem while streaming music here .

My final solution is to use WakeLock , which I need before preparing a song and releasing onPrepared and onError and onDestroy of my service again. It is important to release it again for a safe battery. Before you release it, make sure to check if WakeKock is gone .

I create my WakeLock like this in my service's onCreate:

 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 playback now about 10 minutes and has not stopped so far. I do not know if this is really a battery safety solution.

0
source share

All Articles