小编Ale*_*der的帖子

Toggle 播放和暂停音乐 React Native

我尝试仅使用一个按钮来播放和暂停音乐。当按下按钮时,音乐开始播放,但是当尝试暂停音乐时,出现了问题。我做错了什么?

我收到以下错误:

可能未处理的 Promise Rejection (id:0): TypeError: Sound.pauseAsync 不是函数。

这是我的音频功能

    const { sound } = await Audio.Sound.createAsync(
      { uri: props.songURL }
    );
      await Audio.setIsEnabledAsync(true);
      setSound(songs);
      setSongs(userSongPosts)
    if (isPlaying === true) {
        try {
            await sound.pauseAsync();
            setIsPlaying(false);
          } catch (error) {
            console.error(error);
          }
    } else {
        try {
            await sound.playAsync(); 
            setIsPlaying(true);
          } catch (error) {
            console.error(error);
          }
    }
  }

Run Code Online (Sandbox Code Playgroud)

这是我的按钮

        <TouchableOpacity
              style={styles.profilContainer}
              onPress={() => playSound() }>
            <Text>{isPlaying ? 'Pause' : 'Play'}</Text>
            <View style={styles.buttonView}>
            <Text style={styles.textButton}>{props.submitText}</Text>
            <MatetrialCommunityIcons
              style={styles.iconStyles}
              name={props.icon}
            />
Run Code Online (Sandbox Code Playgroud)

media-player react-native expo

1
推荐指数
1
解决办法
2057
查看次数

标签 统计

expo ×1

media-player ×1

react-native ×1