日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

react-native-sound的使用

發布時間:2025/3/21 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 react-native-sound的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.安裝:yarn add react-native-sound

react-native link react-native-sound

?

2.

import React, {Component} from 'react';
import { StyleSheet, Text, View } from 'react-native';

import { Slider } from 'react-native-elements'
import Sound from 'react-native-sound'

let mp3 = require('./sounds/guojing_xinqiang.mp3');//支持眾多格式
//如果是網絡音頻,使用 new Sound(mp3,null,error => {})
let whoosh = new Sound(mp3, (error) => {
if (error) {
return console.log('資源加載失敗', error);
}
});

export default class mySound extends Component {
constructor(props){
super(props);
this.state = {
volume: 0.5,
seconds: 0, //秒數
totalMin: '', //總分鐘
totalSec: '', //總分鐘秒數
nowMin: 0, //當前分鐘
nowSec: 0, //當前秒鐘
maximumValue: 0, //滑塊最大值
}
}
componentDidMount(){
let totalTime = whoosh.getDuration();
totalTime = Math.ceil(totalTime);
let totalMin = parseInt(totalTime/60); //總分鐘數
let totalSec = totalTime - totalMin * 60; //秒鐘數并判斷前綴是否 + '0'
totalSec = totalSec > 9 ? totalSec : '0' + totalSec;
this.setState({
totalMin,
totalSec,
maximumValue: totalTime,
})
}
componentWillUnmount(){
this.time && clearTimeout(this.time);
}
// 聲音+
_addVolume = () => {
let volume = this.state.volume;
volume += 0.1;
volume = parseFloat(volume).toFixed(1) * 1;
if(volume > 1){
return alert('目前已經是最大音量');
}
this.setState({volume: volume});
whoosh.setVolume(volume);
}
// 聲音-
_reduceVolume = () => {
let volume = this.state.volume;
volume -= 0.1;
volume = parseFloat(volume).toFixed(1) * 1;
if(volume < 0){
return alert('當前為靜音');
}
this.setState({volume: volume});
whoosh.setVolume(volume);
}
// 播放
_play = () => {
whoosh.play();
this.time = setInterval(() => {
whoosh.getCurrentTime(seconds => {
seconds = Math.ceil(seconds);
this._getNowTime(seconds)
})
},1000)
}
// 暫停
_pause = () => {
clearInterval(this.time);
whoosh.pause();
}
// 停止
_stop = () => {
clearInterval(this.time);
this.setState({
nowMin: 0,
nowSec: 0,
seconds: 0,
})
whoosh.stop();
}
_getNowTime = (seconds) => {
let nowMin = this.state.nowMin,
nowSec = this.state.nowSec;
if(seconds >= 60){
nowMin = parseInt(seconds/60); //當前分鐘數
nowSec = seconds - nowMin * 60;
nowSec = nowSec < 10 ? '0' + nowSec : nowSec;
}else{
nowSec = seconds < 10 ? '0' + seconds : seconds;
}
this.setState({
nowMin,
nowSec,
seconds
})
}
render() {
let time = this.state;
return (
<View style={styles.container}>
<Slider
// disabled //禁止滑動
maximumTrackTintColor={'#ccc'} //右側軌道的顏色
minimumTrackTintColor={'skyblue'} //左側軌道的顏色
maximumValue={this.state.maximumValue} //滑塊最大值
minimumValue={0} //滑塊最小值
value={this.state.seconds}
onSlidingComplete={(value)=>{ //用戶完成更改值時調用的回調(例如,當滑塊被釋放時)
value = parseInt(value);
this._getNowTime(value)
// 設置播放時間
whoosh.setCurrentTime(value);
}}
/>
<Text>{time.nowMin}:{time.nowSec}/{time.totalMin}:{time.totalSec}</Text>
<Text>當前音量: {this.state.volume}</Text>
<Text onPress={this._addVolume}>聲音+</Text>
<Text onPress={this._reduceVolume}>聲音-</Text>
<Text onPress={this._play}>播放</Text>
<Text onPress={this._pause}>暫停</Text>
<Text onPress={this._stop}>停止</Text>
</View>
);
}
}

const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#F5FCFF',
},
});

//這里的UI庫可以自行更換

?

轉載于https://blog.csdn.net/qq_39910762/article/details/85249897

轉載于:https://www.cnblogs.com/boonook/p/10364889.html

總結

以上是生活随笔為你收集整理的react-native-sound的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。