如何检查声音是否在设备上被禁用 反应原生博览会 ?我试图检查,但在官方文档中没有对此的描述。
您可以尝试以下操作:
import { Audio } from 'expo-av';
import React from 'react';
import { Text, View } from 'react-native';
const App = () => {
const audioPlayer = new Audio.Sound();
React.useEffect(() => {
playAudio();
})
async function playAudio (){
try {
await Audio.setIsEnabledAsync(false); // Only added to test what would happen if sound is disabled on device.
await audioPlayer.unloadAsync();
await audioPlayer.loadAsync(require('./assets/alarm1-b238.mp3')); //alarm1-b238.mp3 would be any audio file inside the assets folder.
await audioPlayer.playAsync();
await audioPlayer.stopAsync(); //Since you might not want a sound to play before whatever you want the code to do
} catch(error) {
console.log('error', error); //Should produce an error
}
}
return null;
}
export default App;
引用:
- 世博会。音频。https://docs.expo.io/versions/latest/sdk/audio/。 (2019年11月22日访问(
- 堆栈溢出。反应原生 - 世博音频停止所有声音。 https://stackoverflow.com/a/51207494/8121551。 (11月访问 22, 2019(
Audio.setIsEnabledAsync(true(; 是禁用或启用音频的调用。
它会自动设置为 true,因此您很可能遇到 .loadAsync 问题