问题
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