在我的 iPhone 12 Mini 上运行
eas build --profile preview --platform ios
并安装后,当我关闭应用程序或锁定屏幕时,音频不会继续播放。
我已根据 Expo Audio 文档在 app.json 中设置了以下内容。
在我的设备上安装该版本后,我希望当我离开应用程序或激活锁屏时音乐能够继续播放。音频似乎立即暂停,但当我重新打开应用程序时立即恢复。
"ios": {
"supportsTablet": true,
"bundleIdentifier": "com.rahlok.pplay",
"infoPlist": {
"UIBackgroundModes": [
"audio"
]
}
},
这是我的 Player.js
import * as React from 'react';
import { Text, View, StyleSheet, Button } from 'react-native';
import { Audio } from 'expo-av';
export default function Player() {
const [sound, setSound] = React.useState();
React.useEffect(() => {
Audio.setAudioModeAsync({
staysActiveInBackground: true,
});
})
async function playSound() {
console.log('Loading Sound');
const { sound } = await Audio.Sound.createAsync( require('../assets/demo.mp3')
);
setSound(sound);
console.log('Playing Sound');
await sound.playAsync();
}
async function pauseSound() {
console.log('Pausing Sound');
await sound.pauseAsync();
}
async function resumeSound() {
console.log('Resuming Sound');
await sound.playAsync();
}
React.useEffect(() => {
return sound
? () => {
console.log('Unloading Sound');
sound.unloadAsync();
}
: undefined;
}, [sound]);
return (
<View>
<Button title="Play Sound" onPress={playSound} />
<Button title="Pause Sound" onPress={pauseSound} />
<Button title="Resume Sound" onPress={resumeSound} />
</View>
);
}
我在Android上也有同样的问题,我估计如果我在iOS上尝试也会发生同样的事情。 能解决吗?
这是我的配置app.json
"ios": {
"supportsTablet": true,
"infoPlist": {
"UIBackgroundModes": [
"audio"
]
},
"bundleIdentifier": "com.enegraso.radiomasvidaapp"
},
"android": {
"adaptiveIcon": {
"foregroundImage": "./assets/adaptive-icon.png",
"backgroundColor": "#ffffff"
},
"permissions": [
"android.permission.WAKE_LOCK",
"android.permission.RECORD_AUDIO",
"android.permission.MODIFY_AUDIO_SETTINGS"
],
所以我有播放按钮
async function playSound() {
console.log("Loading Sound", url);
setIsPlaying(true);
await Audio.setAudioModeAsync({
staysActiveInBackground: true,
playsInSilentModeIOS: true,
interruptionModeIOS: InterruptionModeIOS.DuckOthers, // Change as you like
interruptionModeAndroid: InterruptionModeAndroid.DuckOthers, // Change as you like
shouldDuckAndroid: true,
playThroughEarpieceAndroid: false,
});
const { sound } = await Audio.Sound.createAsync({ "uri": url });
setSound(sound);
console.log("Playing Sound", url);
await sound.playAsync();
}
但在某些手机中,屏幕暂停时它会持续长达 10 分钟,而在其他手机中,一旦暂停,声音就会关闭,当我返回应用程序时,我无法再发出声音,因为只有暂停和停止按钮处于活动状态