从锁定屏幕开始活动时无声音 [英] No sound when the Activity starts from Lock Screen
问题描述
这似乎是一个奇怪的问题.我正在使用AlarmManager设置带有自定义音频轨道的警报.活动开始并正常播放音乐,但是当我锁定手机时,活动开始但音频没有播放.
This seems to be a strange issue. I am using AlarmManager to set a alarm with custom audio track. The activity start and plays the music normally, but when i lock the phone the activity starts but the audio is not playing.
这是我正在使用的代码.
Here is the code that i am using it.
onCreate方法
The onCreate Method
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakelock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK|PowerManager.ACQUIRE_CAUSES_WAKEUP,"My Wake Log");
mWakelock.acquire();
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
setContentView(R.layout.sampleAlarm);
mediplayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediplayer.setDataSource(Environment.getExternalStorageDirectory()+"track1/1.mp3");
mediplayer.setVolume(100,100);
mediplayer.prepare();
mediplayer.setLooping(true);
mediplayer.start();
任何想法我在做什么错.
Any idea what i am doing wrong.
推荐答案
我能够弄清楚.并解决了问题.我在onStop和onPause方法中添加了Mediaplayer版本,当手机从锁定模式唤醒时,由于某种原因,它反复触发onStop和onPause.因此,我在onStop和onPause中添加了isFinishing以确保活动实际上已停止.
I was able to figure out. And solved the problem. I had added a mediaplayer release in onStop and onPause method, and when the phone wakes up from the lock mode it was repetitively triggering onStop and onPause for some reason. So i added a isFinishing in onStop and onPause to make sure the activity is actually stopped.
if(this.isFinishing(){
mediplayer.stop();
mediplayer.release();
}
这篇关于从锁定屏幕开始活动时无声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!