React Native - SetTimeout() 不起作用。t.apply 不是一个函数。

3

我正在尝试延迟一段时间后播放音频。

_onPlayPausePressed = () => {
    if (this.sound != null) {
      if (this.state.isPlaying) {
        this.sound.pauseAsync();
      } else {
        setTimeout(this.sound.playAsync(), 2000);
      }
    }
  };

然而它返回了错误:t.apply不是一个函数。(在't.apply(void 0,o)'中,'t.apply'未定义) 我尝试过Oleg更新的方法。第一次它起作用了,但之后再运行就不行了。以下是我代码的更多细节:

//import the array with sound details:{id, name desc, sound}
import { soundArray } from "./CreateRecord";
...
export default class CurrentRecord extends React.Component {
  constructor(props) {
    super(props);
    this.currentSound = [];
    this.recording = null;
    this.sound = null;
    this.isSeeking = false;
    this.shouldPlayAtEndOfSeek = false;
    this.state = {
      haveRecordingPermissions: false,
      isLoading: false,
      isPlaybackAllowed: false,
      muted: false,
      soundPosition: null,
      soundDuration: null,
      recordingDuration: null,
      shouldPlay: false,
      isPlaying: false,
      isRecording: false,
      fontLoaded: false,
      shouldCorrectPitch: true,
      volume: 1.0,
      rate: 1.0,
      isModalVisible: false
    };
    this.recordingSettings = JSON.parse(
      JSON.stringify(Audio.RECORDING_OPTIONS_PRESET_LOW_QUALITY)
    );
  }

//load the audio when the component mount
  componentDidMount() {
    this.loadAudio();

    (async () => {
      await Font.loadAsync({
        "cutive-mono-regular": require("../../assets/fonts/CutiveMono-Regular.ttf")
      });
      this.setState({ fontLoaded: true });
    })();
    this._askForPermissions();
  }

//load the sound
  async loadAudio() {
    const { navigation } = this.props;
    const id = navigation.getParam("id");
    this.sound = new Audio.Sound();
    for (let i = 0; i < soundArray.length; i++) {
      if (soundArray[i].id === id) {
        this.currentSound = soundArray[i];
        console.log(this.currentSound);

        break;
      }
    }
    try {
      await this.sound.loadAsync({
        uri: this.currentSound.sound /* url for your audio file */
      });
      await this.sound.setOnPlaybackStatusUpdate(
        this._updateScreenForSoundStatus
      );
    } catch (e) {
      console.log("ERROR Loading Audio", e);
    }
  }

//change screen based on the status
  _updateScreenForSoundStatus = status => {
    if (status.isLoaded) {
      this.setState({
        soundDuration: status.durationMillis,
        soundPosition: status.positionMillis,
        shouldPlay: status.shouldPlay,
        isPlaying: status.isPlaying,
        rate: status.rate,
        muted: status.isMuted,
        volume: status.volume,
        shouldCorrectPitch: status.shouldCorrectPitch,
        isPlaybackAllowed: true
      });
    } else {
      this.setState({
        soundDuration: null,
        soundPosition: null,
        isPlaybackAllowed: false
      });
      if (status.error) {
        console.log(`FATAL PLAYER ERROR: ${status.error}`);
      }
    }
  };

  _askForPermissions = async () => {
    const response = await Permissions.askAsync(Permissions.AUDIO_RECORDING);
    this.setState({
      haveRecordingPermissions: response.status === "granted"
    });
  };

//here's where i want to delay the audio file. 
  _onPlayPausePressed = () => {
    if (this.sound != null) {
      if (this.state.isPlaying) {
        this.sound.pauseAsync();
      } else {
        setTimeout(() => this.sound.playAsync(), 2000);
      }
    }
  };

  _onStopPressed = () => {
    if (this.sound != null) {
      this.sound.stopAsync();
    }
  };

  _onMutePressed = () => {
    if (this.sound != null) {
      this.sound.setIsMutedAsync(!this.state.muted);
    }
  };

  _onVolumeSliderValueChange = value => {
    if (this.sound != null) {
      this.sound.setVolumeAsync(value);
    }
  };

  _trySetRate = async (rate, shouldCorrectPitch) => {
    if (this.sound != null) {
      try {
        await this.sound.setRateAsync(rate, shouldCorrectPitch);
      } catch (error) {
        // Rate changing could not be performed, possibly because the client's Android API is too old.
      }
    }
  };

  _onRateSliderSlidingComplete = async value => {
    this._trySetRate(value * RATE_SCALE, this.state.shouldCorrectPitch);
  };

  _onPitchCorrectionPressed = async value => {
    this._trySetRate(this.state.rate, !this.state.shouldCorrectPitch);
  };

  _onSeekSliderValueChange = value => {
    if (this.sound != null && !this.isSeeking) {
      this.isSeeking = true;
      this.shouldPlayAtEndOfSeek = this.state.shouldPlay;
      this.sound.pauseAsync();
    }
  };

  _onSeekSliderSlidingComplete = async value => {
    if (this.sound != null) {
      this.isSeeking = false;
      const seekPosition = value * this.state.soundDuration;
      if (this.shouldPlayAtEndOfSeek) {
        this.sound.playFromPositionAsync(seekPosition);
      } else {
        this.sound.setPositionAsync(seekPosition);
      }
    }
  };

  _getSeekSliderPosition() {
    if (
      this.sound != null &&
      this.state.soundPosition != null &&
      this.state.soundDuration != null
    ) {
      return this.state.soundPosition / this.state.soundDuration;
    }
    return 0;
  }

  _getMMSSFromMillis(millis) {
    const totalSeconds = millis / 1000;
    const seconds = Math.floor(totalSeconds % 60);
    const minutes = Math.floor(totalSeconds / 60);

    const padWithZero = number => {
      const string = number.toString();
      if (number < 10) {
        return "0" + string;
      }
      return string;
    };
    return padWithZero(minutes) + ":" + padWithZero(seconds);
  }

  _getPlaybackTimestamp() {
    if (
      this.sound != null &&
      this.state.soundPosition != null &&
      this.state.soundDuration != null
    ) {
      return `${this._getMMSSFromMillis(
        this.state.soundPosition
      )} / ${this._getMMSSFromMillis(this.state.soundDuration)}`;
    }
    return "";
  }

  _getRecordingTimestamp() {
    if (this.state.recordingDuration != null) {
      return `${this._getMMSSFromMillis(this.state.recordingDuration)}`;
    }
    return `${this._getMMSSFromMillis(0)}`;
  }
2个回答

0

更改为

_onPlayPausePressed = () => {
    if (this.sound != null) {
      if (this.state.isPlaying) {
       this.sound.pauseAsync();
          } else {
         setTimeout(()=>this.sound.playAsync(), 2000);
      }
    }
  };

嗨,我尝试了。但是声音无法再播放了。 - William Pham
答案已更新,请再试一次,如果还不行,请发布更多的代码。 - Oleg
它只会在第一次运行,之后就不会再运行了。我在下面贴了一些额外的代码。 - William Pham
你在哪里更新了 this.state.isPlaying 的状态?在你的代码中加入一些日志或者发布 Expo Snack。 - Oleg
我认为那就是我的错误所在。当我在另一个标签页中创建声音文件时,我做了同样的事情,但它完美地运行了。我不知道为什么会这样。你有建议在哪里更新它吗? - William Pham
https://github.com/WilliamP1998/Audio-Recorder_App 我尝试在 Expo Snack 上上传,但失败了,所以这是我的代码存储库,如果您需要运行它。如何运行它的说明详细列在 readme.txt 中。 - William Pham

0

移除函数的执行。在playAsync中使用箭头函数(()=> {})。

_onPlayPausePressed = async () => {
  if (this.sound != null) {
    if (this.state.isPlaying) {
      await this.sound.pauseAsync();
    } else {
      setTimeout(() => {
        await this.sound.playAsync()
      }, 2000);
    }
  }
};

嗨,如果我这样做,播放按钮就不能运行了。 - William Pham
playAsync()是播放音频的Expo函数。 - William Pham
您可以在此处查找更多信息:https://docs.expo.io/versions/latest/sdk/audio/ - William Pham
@minhphạm 请尝试修复。 - Jiml

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接