Reactjs React Native-SetTimeout()不工作。t、 apply不是一个函数
我正在尝试在延迟一段时间后播放音频Reactjs React Native-SetTimeout()不工作。t、 apply不是一个函数,reactjs,react-native,expo,Reactjs,React Native,Expo,我正在尝试在延迟一段时间后播放音频 _onPlayPausePressed = () => { if (this.sound != null) { if (this.state.isPlaying) { this.sound.pauseAsync(); } else { setTimeout(this.sound.playAsync(), 2000); } } }; 但是,它返回错误:t.apply不
_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)}`;
}
//导入包含声音详细信息的数组:{id,name desc,sound}
从“/CreateRecord”导入{soundArray};
...
导出默认类CurrentRecord扩展React.Component{
建造师(道具){
超级(道具);
this.currentSound=[];
this.recording=null;
this.sound=null;
this.isseek=false;
this.shouldlplayatendofseek=false;
此.state={
haveRecordingPermissions:false,
孤岛加载:false,
isPlaybackAllowed:false,
沉默:错,
声音位置:空,
soundDuration:null,
recordingDuration:空,
应该说:错,
isplay:false,
以色列记录:错,
错误:,
应该正确:正确,
卷:1.0,
比率:1.0,
isModalVisible:错误
};
this.recordingSettings=JSON.parse(
stringify(音频、录音、选项、预设、低质量)
);
}
//安装组件时加载音频
componentDidMount(){
这是loadAudio();
(异步()=>{
等待Font.loadAsync({
“cutive mono regular”:要求(../../assets/fonts/CutiveMono regular.ttf)
});
this.setState({fontLoaded:true});
})();
这是我的许可证;
}
//加载声音
异步加载音频(){
const{navigation}=this.props;
const id=navigation.getParam(“id”);
this.sound=新的Audio.sound();
for(设i=0;i{
如果(状态为isLoaded){
这是我的国家({
soundDuration:status.durationMillis,
声音位置:status.position毫秒,
shouldPlay:status.shouldPlay,
isplay:status.isplay,
比率:status.rate,
静音:status.isMuted,
卷:status.volume,
shouldCorrectPitch:status.shouldCorrectPitch,
isPlaybackAllowed:true
});
}否则{
这是我的国家({
soundDuration:null,
声音位置:空,
isPlaybackAllowed:false
});
if(status.error){
log(`FATAL PLAYER ERROR:${status.ERROR}`);
}
}
};
_askForPermissions=async()=>{
const response=wait Permissions.askAsync(Permissions.AUDIO_RECORDING);
这是我的国家({
haveRecordingPermissions:response.status==“已授予”
});
};
//这是我想延迟音频文件的地方。
_onPlayPausePressed=()=>{
如果(this.sound!=null){
if(this.state.isPlaying){
this.sound.pauseAsync();
}否则{
setTimeout(()=>this.sound.playanc(),2000);
}
}
};
_OnStopped=()=>{
如果(this.sound!=null){
this.sound.stopAsync();
}
};
_onMutePressed=()=>{
如果(this.sound!=null){
this.sound.setIsMutedAsync(!this.state.muted);
}
};
_onVolumeSliderValueChange=值=>{
如果(this.sound!=null){
this.sound.setVolumeAsync(值);
}
};
_trySetRate=async(速率,shouldCorrectPitch)=>{
如果(this.sound!=null){
试一试{
等待这个.sound.setRateAsync(rate,shouldCorrectPitch);
}捕获(错误){
//无法执行速率更改,可能是因为客户端的Android API太旧。
}
}
};
_onRateSliderSlidingComplete=异步值=>{
此._trySetRate(值*速率_刻度,此.state.shouldCorrectPitch);
};
_onPitchCorrectionPressed=异步值=>{
this.\u trySetRate(this.state.rate,!this.state.shouldCorrectPitch);
};
_onSeekSliderValueChange=值=>{
if(this.sound!=null&&!this.isseek){
this.isseek=true;
this.shouldlplayatendofseek=this.state.shouldlplay;
this.sound.pauseAsync();
}
};
_onSeekSliderSlidingComplete=异步值=>{
如果(this.sound!=null){
this.isseek=false;
const seekPosition=value*this.state.soundDuration;
如果(此.应显示为Endofseek){
this.sound.playFromPositionAsync(参见位置);
}否则{
this.sound.setPositionAsync(参见位置);
}
}
};
_getSeekSliderPosition(){
如果(
这个.声音!=null&&
this.state.soundPosition!=null&&
this.state.soundDuration!=null
) {
返回this.state.soundPosition/this.state.soundDuration;
}
返回0;
}
_getMMSSFromMillis(毫秒){
const totalSeconds=毫秒/1000;
常数秒=数学地板(总秒数%60);
常数分钟=数学楼层(总秒/60);
const padWithZero=number=>{
常量字符串=number.toString();
如果(数字<10){
返回“0”+字符串;
}
返回字符串;
};
返回零位焊盘(分钟)+“:”+零位焊盘(秒);
}
_getPlaybackTimestamp(){
如果(
这个.声音!=null&&
this.state.soundPosition!=null&&
this.state.soundDuration!=null
) {
返回`${this.\u getMMSSFromMillis(
这个州的声音位置
)}/${this.\u getMMSSFromMillis(this.state.soundDuration)}`;
}
返回“”;
}
_getRecordingTimestamp(){
if(this.state.recordingDuration!=null){
返回`${this.\u getMMSSFromMillis(this.s
_onPlayPausePressed = () => {
if (this.sound != null) {
if (this.state.isPlaying) {
this.sound.pauseAsync();
} else {
setTimeout(()=>this.sound.playAsync(), 2000);
}
}
};
_onPlayPausePressed = async () => {
if (this.sound != null) {
if (this.state.isPlaying) {
await this.sound.pauseAsync();
} else {
setTimeout(() => {
await this.sound.playAsync()
}, 2000);
}
}
};