【问题标题】:AudioTrack sound loss while screen lock in nexus 5Nexus 5中屏幕锁定时AudioTrack声音丢失
【发布时间】:2015-11-20 07:25:01
【问题描述】:
我正在使用 wifi 创建实时音频流。因此我使用 AudioTrack 对象,但是当我做屏幕锁定时,某些设备会丢失音频。在带有 Android Marshmallow 的 nexus 5 中,在 Android Lollipop Device 中也出现了同样的问题,但不是全部。
在 nexus 4、android one、galaxy S4 中运行良好。
请帮助我。对不起我的英语不好。
【问题讨论】:
标签:
android
stream
streaming
wifi
audiotrack
【解决方案1】:
我找到了简单的解决方案,屏幕睡眠时音频流停止。
我在我的服务中设置了以下代码。
public class MyService extends Service
{
MulticastLock multicastLock;
PowerManager.WakeLock wakeLock;
WifiManager.WifiLock wifiLock;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
PowerManager pMgr = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakeLock = pMgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"MyWakeLock");
wakeLock.acquire();
WifiManager wMgr = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifiLock = wMgr.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF,
"MyWifiLock");
wifiLock.acquire();
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
multicastLock = wifi.createMulticastLock("multicastLock");
multicastLock.acquire();
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
if (multicastLock != null) {
multicastLock.release();
multicastLock = null;
}
if (wifiLock != null) {
wifiLock.release();
wifiLock = null;
}
if (wakeLock != null) {
wakeLock.release();
wakeLock = null;
}
super.onDestroy();
}
}