阿玛说必须不用有网名 發表於 2021-12-4 22:55:00

react native 音频播放 react-native-sound

<p><span style="font-size: 16px">先放一个效果图:该图实现的效果,点击播放按钮,进度条随着时间移动,点击暂停按钮,进度条停止移动</span></p>
<p><img src="https://img2020.cnblogs.com/blog/1723157/202112/1723157-20211204222517106-191351802.gif"></p>
<p>&nbsp;</p>
<p><span style="font-size: 18pt">第一步,用到什么库</span></p>
<div class="cnblogs_code">
<p><span style="font-size: 16px">1.zmxv/react-native-sound 播放声音的库&nbsp; &nbsp;</span><br><span style="font-size: 16px">2.callstack/react-native-slider 进度条的库 </span></p>

</div>
<p><span style="font-size: 18pt">第二步,准备播放音频</span></p>
<p><span style="font-size: 16px">我测试时,是将mp3格式的声音放在本地的,根据官网描述,本地音频,放在 `android\app\src\main\res\raw`,注意要重新编译打包项目哦</span></p>
<div class="cnblogs_code" style="text-align: left">
<pre><span style="font-size: 16px">import React from "react"</span><span style="color: rgba(0, 0, 0, 1)">;
import { View, Text, StyleSheet } from </span><span style="font-size: 16px">'react-native'</span><span style="color: rgba(0, 0, 0, 1)">;
<br>
import Sound from </span><span style="font-size: 16px">"react-native-sound"</span><span style="color: rgba(0, 0, 0, 1)">;
Sound.setCategory(</span><span style="font-size: 16px">'Playback'</span><span style="color: rgba(0, 0, 0, 1)">);

class SoundScreen extends React.Component {
  constructor(props) {
      super(props);
      </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="font-size: 16px">.state =</span><span style="color: rgba(0, 0, 0, 1)"> {
            whoosh: </span><span style="color: rgba(0, 0, 255, 1)">null</span><span style="font-size: 16px">, </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">音频对象</span><span style="color: rgba(0, 0, 0, 1)">      }
    }
  componentDidMount() {<br>      //构建好音频对象
      </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.build();
    }
  build(){
    let audioSrc </span><span style="font-size: 16px">= 'whoosh.mp3' </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">这个音频放在android\app\src\main\res\raw</span><span style="font-size: 16px">
    </span><span style="color: rgba(0, 0, 255, 1)">let </span><span style="font-size: 16px">whoosh = </span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="font-size: 16px"> Sound(audioSrc, Sound.MAIN_BUNDLE, (error) =&gt;</span><span style="color: rgba(0, 0, 0, 1)"> {
            </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (error) {
                alert(</span><span style="font-size: 16px">'加载声音失败'</span><span style="color: rgba(0, 0, 0, 1)">);
                </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">;
            }       </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)"><span style="font-size: 16px">.setState({ whoosh: whoosh});
      });
  }<br>    playAudio=()=&gt;{<br>    this.state.whoosh.play((success) =&gt; {
        if (success) {//播放完成后的逻辑在这里处理
               
        } else {
                console.log('playback failed due to audio decoding errors');
        }
    });
    }<br>    render(){<br>    return (<br>      &lt;Text onPress={this.playAudio}&gt;播放&lt;/Text&gt;<br>    )<br>  }
}</span></span></pre>
</div>
<p><br><span style="font-size: 18pt">第三步,添加Slider滑块组件</span></p>
<p><span style="font-size: 16px">自己看文档添加吧,这里有个小问题,就是设置长度300,但是它左右两边有8个距离的空袭,我也没解决<br><br><span style="font-size: 18pt">第四步,mp3音频播放时间 和 Slider进度同步的问题</span><br>思路:Slider的最小值为0 ,最大值为1 ,之后通过定时器,获得当前播放时间 除以 总时长,这个值就是Slider的值<br><span style="color: rgba(255, 0, 0, 1)">注意,将debug关掉,不然定时任务不准确</span></span></p>
<div class="cnblogs_code">
<pre><span style="font-size: 16px"><span style="color: rgba(0, 128, 0, 1)">   //</span><span style="color: rgba(0, 128, 0, 1)">定时任务:监听当前播放时间</span>
<span style="color: rgba(0, 0, 0, 1)">    listenAudio() {
      let that </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">;
      let timer </span>= setInterval(<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> () {
            that.state.whoosh.getCurrentTime(</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> (seconds) {
                let duration </span>=<span style="color: rgba(0, 0, 0, 1)"> that.state.duration;
                let temp </span>= seconds / duration;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">当前时间/总时间 = 播放进度比</span>
<span style="color: rgba(0, 0, 0, 1)">                that.setState({ sliderValue: temp, })
            });
      }, </span>1000<span style="color: rgba(0, 0, 0, 1)">);
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">将定时器私有化</span>
      <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.setState({ Timer: timer });
    }</span></span></pre>
</div>
<p>&nbsp;</p>
<p><span style="font-size: 16px">主要的内容都说完,注意清除定时器对象</span></p>
<p><span style="font-size: 16px">&nbsp;</span></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p id="1638627917872">&nbsp;</p><br><br>
来源:https://www.cnblogs.com/tengyuxin/p/15643615.html
頁: [1]
查看完整版本: react native 音频播放 react-native-sound