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

标签 javascript reactjs react-native jsx expo

我在 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/

相关文章:

javascript - 如何创建 javascript 类或模块,我需要在页面上有 2 个实例

javascript - HTML 选择,用文本嵌入对象?

javascript - REACT - 如果特定输入字段为空,我该如何发出警告?

reactjs - 为什么 npm install react-native 不起作用?

javascript - 销售点系统

javascript - Node.js 缓冲区到类型化数组

reactjs - React setter 函数未按预期更新状态

javascript - Firebase -- 批量删除子节点

android - React Native ScrollView 与成长中的 child

javascript - React Native - React.createElement 不是函数