gpt4 book ai didi

javascript - 在自定义 React Native/Expo 音频播放器组件中启用清理?

转载 作者:行者123 更新时间:2023-11-29 23:26:59 25 4
gpt4 key购买 nike

我在 React Native 中有一个音频播放器(使用 Expo 的音频 API),效果很好。这是我的 AudioPlayer 类:

class AudioPlayer extends React.Component {
constructor(props) {
super(props);
this.state = {
isPlaying: false,
position: 0,
timeLeft: ""
};
this.loadAudio = this.loadAudio.bind(this);
this.toggleAudioPlayback = this.toggleAudioPlayback.bind(this);
this.getStatus = this.getStatus.bind(this);
}
componentDidMount() {
this.loadAudio();
this.interval = setInterval(() => this.getStatus(), 800);
}
componentWillUnmount() {
clearInterval(this.interval);
this.soundObject.stopAsync();
}
async loadAudio() {
this.soundObject = new Audio.Sound();
try {
await this.soundObject.loadAsync(this.props.source);
} catch (e) {
console.log('ERROR Loading Audio', e);
}
this.getStatus();
}
toggleAudioPlayback() {
this.setState({
isPlaying: !this.state.isPlaying,
}, () => (this.state.isPlaying
? this.soundObject.playAsync()
: this.soundObject.pauseAsync()));
}
getStatus = async () => {
var status = await this.soundObject.getStatusAsync();
var percentage = (status["positionMillis"] / status["durationMillis"] * 100);
var remainingTime = msToTime(status["durationMillis"] - status["positionMillis"]);
this.setState({position: percentage, timeLeft: remainingTime});
}
render() {
return (
<View style={{flex: 1, flexDirection: 'row', alignItems: 'center', backgroundColor: 'rgba(0, 0, 0, 0.7)', borderRadius: 5, padding: 5, paddingRight: 12}}>
<Entypo name={this.state.isPlaying ? "controller-paus" : "controller-play"} color='#dedede' size={25} onPress={this.toggleAudioPlayback} />
<View style={{flex: 1, height: 4, backgroundColor: '#d6d6d6', alignItems: 'center', flexDirection: 'row'}}>
<View style={{position: 'absolute', width: `${this.state.position}%`, left: 0, height: 4, backgroundColor: orangeColor}} />
<View style={{position: 'absolute', left: `${this.state.position}%`, borderRadius: 100/2, width: 10, height: 10, backgroundColor: orangeColor}} />
</View>
<View style={{width: 5}} />
<Text style={{color: 'white', width: 83, textAlign: 'right'}}>-{this.state.timeLeft}</Text>
</View>
)
}
}

看起来像这样: Audio Player

这很好用,但我希望点击圆圈可以移动它并选择播放音频的位置。

谢谢,埃里克

最佳答案

您可以使用 react-native 附带的 slider 组件,而不是您自己的实现。请参阅文档 here .实现应该是直截了当的。

关于javascript - 在自定义 React Native/Expo 音频播放器组件中启用清理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48779466/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com