屏幕锁定时,MediaPlayer有时不准备 [英] MediaPlayer sometimes not preparing when screen is locked

查看:237
本文介绍了屏幕锁定时,MediaPlayer有时不准备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于MediaPlayback的MusicService,但使用的MediaPlayer具有以下设置:

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);

并设置了那些侦听器:

OnPreparedListener, OnCompletionListener, OnErrorListener, OnSeekCompleteListener

MediaPlayer用于mp3-Playback.当一首乐曲完成时,将调用onCompletion.然后调用PlayNext,重置MediaPlayer,然后使用下一个Track的URI设置数据源. Uri加载了:

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);

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

然后准备播放器,然后启动播放器.这项工作正常,但是仅在某些情况下,当设备被锁定并在其锁定时播放大约1-3首歌曲,并且下一首歌曲应开始播放时,播放机才会准备好,直到我按下电源按钮. 弄清楚,如果我不按PowerButton,则播放器需要大约2分钟的时间来准备.

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.

我现在已经记录了所有内容,并使用Log.e(...)进行了一些自定义输出. 这是在调用prepare()(prepareAsync()提供相同结果)之前提出的: E/MusicService: Preparing now...

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...

在调用onPrepared时将其推出:

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.

...然后直到我按下PowerButton之前都没有输出.然后就准备好了歌曲.

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

如果在我按下PowerButton直到调用准备播放器"之前,有人对完整输出感兴趣,请我在这里创建了要点.

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

旁注: 使用该应用程序时,一些片段中会显示一些专辑封面.它们装有Picasso,所以我不必担心内存缓存.一些不带毕加索的ImageView被填充(例如,保存我的PlayerControls的可绘制对象的ImageView).也许内存/资源有问题?

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?

推荐答案

我可能在另一个线程中找到了答案,其中一些人在流音乐时遇到相同的问题

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

我的最终解决方案是使用 WakeLock ,在准备乐曲之前,我需要这样做,然后再次释放我的Service的onPrepared和onError和onDestroy.再次将其释放到安全的电池中很重要.在释放它之前,请确保检查是否已握住WakeLock .

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.

我在服务的onCreate中创建这样的WakeLock:

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");

获取:

wakeLock.acquire();

版本:

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

现在测试了播放大约10分钟,并且直到现在都没有停止.我不知道这是否是一种非常省电的解决方案

这篇关于屏幕锁定时,MediaPlayer有时不准备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆