No sound when the Activity starts from Lock Screen

自古美人都是妖i 提交于 2019-12-12 04:43:08

问题


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.

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.


回答1:


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



回答2:


Just adding to @RagZ anwer

If you are using Fragments then just replace isFinishing() with isRemoving().

if(this.isRemoving(){
   mediplayer.stop();
   mediplayer.release();
}


来源:https://stackoverflow.com/questions/22643175/no-sound-when-the-activity-starts-from-lock-screen

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