我正在使用android的AlarmManager开发类似服务的应用程序。该应用程序基本上连接到服务器并调用远程过程。如果返回的值为True,它将播放声音:
I'm working on a service-like application using android's AlarmManager. The app basically connects to a server and calls a remote procedure. If the value returned is True, it plays a sound:
MediaPlayer mediaPlayer = new MediaPlayer(); mediaPlayer.setAudioStreamType(AudioManager.STREAM_RING); try { mediaPlayer.setDataSource(getApplicationContext(), Uri.parse("file:///system/media/audio/ringtones/Rolling_tone.ogg")); } ... mediaPlayer.start();当手动启动应用程序时,此方法可以正常工作。但是,当电话重启时(我已经实现了BroadcastReceiver),声音播放了大约一秒钟,然后立即被打断。
This works all and well when the application is started manually. However when the phone is rebooted (I've implemented the BroadcastReceiver), the sound is played for like a second then gets interrupted immediately.
听起来几乎像是在播放被某种东西切断。我需要停止并重新启动该应用程序才能使其再次正常运行。
It sounds almost like the play cycle is being cut off by something. I need to stop and re-start the application to have it working correctly again.
关于可能原因的任何线索吗?
Any clues on what the cause could be?
推荐答案您可以使用 MediaPlayer 的 setScreenOnWhilePlaying(),以确保在后台播放音频时您的应用程序不会进入睡眠状态。
You could use MediaPlayer's setScreenOnWhilePlaying() to ensure that your application doesn't go to sleep while the audio plays in the background.
developer.android/reference/android/media/MediaPlayer.html#setWakeMode %28android.content.Context,%20int%29
另一种可能的解决方案是使用 PowerManager 使用 WakeLock 使设备保持唤醒状态:
Another possible solution is the use the PowerManager to keep your device awake with a WakeLock:
请参见 developer.android/reference/android/os/PowerManager.html
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag"); wl.acquire(); ... wait until sound has finished playing (with listener) wl.release();更多推荐
重启后android警报管理器声音
发布评论