从锁定屏幕开始活动时无声音

编程入门 行业动态 更新时间:2024-10-28 14:29:07
本文介绍了从锁定屏幕开始活动时无声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这似乎是一个奇怪的问题.我正在使用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(); }

更多推荐

从锁定屏幕开始活动时无声音

本文发布于:2023-11-27 01:15:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635963.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:屏幕   声音

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!