【问题标题】:Check is sound is disabled on device React Native EXPO检查设备 React Native EXPO 上的声音是否被禁用
【发布时间】:2019-11-20 18:07:04
【问题描述】:

如何检查设备 React Native EXPO 是否禁用声音?我试图检查,但在官方文档中没有对此的描述。

【问题讨论】:

    标签: reactjs react-native audio expo


    【解决方案1】:

    您可以尝试以下方法:

    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;
    

    参考资料:

    【讨论】:

    • @livindead 如果对您有帮助,请接受此答案,或者指出您是否遇到其他问题。
    【解决方案2】:

    Audio.setIsEnabledAsync(true);是您调用来禁用或启用音频的方法。

    它会自动设置为 true,因此您很可能遇到 .loadAsync 问题

    【讨论】:

      猜你喜欢
      • 2022-09-29
      • 2018-10-21
      • 2021-11-06
      • 2022-01-21
      • 1970-01-01
      • 2019-11-20
      • 2016-07-07
      • 2016-10-25
      • 1970-01-01
      相关资源
      最近更新 更多