Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 我能';t正确使用Expo实例中的音频。我能';t暂停或更新播放状态_Reactjs_React Native_Audio_Expo - Fatal编程技术网

Reactjs 我能';t正确使用Expo实例中的音频。我能';t暂停或更新播放状态

Reactjs 我能';t正确使用Expo实例中的音频。我能';t暂停或更新播放状态,reactjs,react-native,audio,expo,Reactjs,React Native,Audio,Expo,当我尝试暂停或更新由Audio.sound()创建的声音对象的状态时,一个错误指示声音未加载 错误:无法完成操作,因为未加载声音。 我不明白,因为我从加载文件并播放它的同一个对象调用了该方法 这是我的密码: import React,{useState,useffect}来自“React”; 从“@expo/vector icons”导入{Feather}”; 进口{ 样式表, 文本, 看法 按钮 平面列表, 可触摸不透明度, 形象 }从“反应本族语”; 从“expo av”导入{Audio};

当我尝试暂停或更新由
Audio.sound()
创建的声音对象的状态时,一个错误指示声音未加载

错误:无法完成操作,因为未加载声音。

我不明白,因为我从加载文件并播放它的同一个对象调用了该方法

这是我的密码:

import React,{useState,useffect}来自“React”;
从“@expo/vector icons”导入{Feather}”;
进口{
样式表,
文本,
看法
按钮
平面列表,
可触摸不透明度,
形象
}从“反应本族语”;
从“expo av”导入{Audio};
const imageLocation=“../../assets/BiblioRequest”;
const soundLocation=“../../sounds/bibliotheque”;
导出默认的BiblioRequestScreen=({navigation})=>{
const[playing,setPlaying]=useState(false);
const[soundPlaying,setSoundPlaying]=使用状态(false);
const soundObject=新的Audio.Sound();
常量播放音频=异步(键、声音)=>{
控制台日志(按键、播放、声音播放);
试一试{
如果(玩){
等待soundObject.pauseAsync();
布景播放(假);
}否则{
布景播放(真实);
等待soundObject.loadAsync(
需要(soundLocation+“/Presentationpiano.mp3”)
);
等待soundObject.PlaySync();
}
}捕获(错误){
console.log(错误);
}
};
返回(
(
播放音频(item.key,item.soundPath)
//item.key==播放?设置播放(false):设置播放(item.key)
}
style={style.item}
>
{item.name}
)}
/>
);
};

谢谢你的帮助

尝试移动
const soundObject=new Audio.Sound()退出
图书检索
功能,如下所示:

  import React, { useState, useEffect } from "react";
import { Feather } from "@expo/vector-icons";
import {
  StyleSheet,
  Text,
  View,
  Button,
  FlatList,
  TouchableOpacity,
  Image
} from "react-native";
import { Audio } from "expo-av";

const imageLocation = "./../assets/bibliotheque";
const soundLocation = "./../sounds/bibliotheque";

const soundObject = new Audio.Sound();

export default BibliothequeScreen = ({ navigation }) => {
  const [playing, setPlaying] = useState(false);
  const [soundPlaying, setSoundPlaying] = useState(false);

  const playAudio = async (key, sound) => {
    console.log(key, playing, soundPlaying);
    try {
      if (playing) {
        await soundObject.pauseAsync();
        setPlaying(false);
      } else {
        setPlaying(true);
        await soundObject.loadAsync(
          require(soundLocation + "/Presentationpiano.mp3")
        );
        await soundObject.playAsync();
      }
    } catch (error) {
      console.log(error);
    }
  };

  return (
    <View style={styles.container}>
      <FlatList
        data={sound}
        renderItem={({ item }) => (
          <View style={styles.itemContainer}>
            <TouchableOpacity
              onPress={
                () => playAudio(item.key, item.soundPath)
                // item.key == playing ? setPlaying(false) : setPlaying(item.key)
              }
              style={styles.item}
            >
              <View style={{ flex: 1, flexDirection: "row" }}>
                <View style={styles.imageContainer}>
                  <Image source={item.path} style={styles.image}></Image>
                </View>
                <Text style={styles.text}>{item.name}</Text>
              </View>
              <Feather
                name={playing && item.key === soundPlaying ? "pause" : "play"}
                size={35}
                color='#3D425C'
              ></Feather>
            </TouchableOpacity>
          </View>
        )}
      />
    </View>
  );
};
import React,{useState,useffect}来自“React”;
从“@expo/vector icons”导入{Feather}”;
进口{
样式表,
文本,
看法
按钮
平面列表,
可触摸不透明度,
形象
}从“反应本族语”;
从“expo av”导入{Audio};
const imageLocation=“../../assets/BiblioRequest”;
const soundLocation=“../../sounds/bibliotheque”;
const soundObject=新的Audio.Sound();
导出默认的BiblioRequestScreen=({navigation})=>{
const[playing,setPlaying]=useState(false);
const[soundPlaying,setSoundPlaying]=使用状态(false);
常量播放音频=异步(键、声音)=>{
控制台日志(按键、播放、声音播放);
试一试{
如果(玩){
等待soundObject.pauseAsync();
布景播放(假);
}否则{
布景播放(真实);
等待soundObject.loadAsync(
需要(soundLocation+“/Presentationpiano.mp3”)
);
等待soundObject.PlaySync();
}
}捕获(错误){
console.log(错误);
}
};
返回(
(
播放音频(item.key,item.soundPath)
//item.key==播放?设置播放(false):设置播放(item.key)
}
style={style.item}
>
{item.name}
)}
/>
);
};

如果你想知道答案,那就行了。我不知道我是怎么错过的。。。我找到了另一种方法,用useState函数声明声音对象,如下所示:
const[soundObject1,setSoundObject1]=useState(new Audio.sound())它起作用了,但我认为这不是一个好的做法。我相信如果使用ref:
const podcastPlayer=useRef(new Audio.Sound())
,效果会稍微好一点。我相信setState会触发重新播放,在本例中,我假设您不希望在设置声音变量时出现这种情况。这是一个令人惊奇的知识绿洲,存在着所有类型的状态管理问题(例如播放/暂停问题,声音在应该时“未加载”(因此仍然在屏幕上播放模糊))
useState(new Audio.sound())
。将其移到功能外后工作。好奇
useRef(newaudio.Sound())
是否更好。