Authentication 是否可以下载音频文件并使用React Native Expo播放?
我的音频文件托管在服务器上,我希望我的应用程序在验证后可以访问这些文件。用户发送包含身份验证令牌的GET请求,服务器返回二进制音频数据 据我所知,没有办法将这个“blob”作为音频文件保存到文件系统中。fetch in react native的当前实现不支持Blob: 。。。expo也不支持理想的react native fetch blob库: 此外,我看不到从服务器流式传输音频文件的方法。所包含的允许从url(例如)流式传输音频,但我看不到任何方式将授权标头附加到请求(例如“授权”:“承载者[我的令牌]”)Authentication 是否可以下载音频文件并使用React Native Expo播放?,authentication,react-native,api-design,fetch-api,expo,Authentication,React Native,Api Design,Fetch Api,Expo,我的音频文件托管在服务器上,我希望我的应用程序在验证后可以访问这些文件。用户发送包含身份验证令牌的GET请求,服务器返回二进制音频数据 据我所知,没有办法将这个“blob”作为音频文件保存到文件系统中。fetch in react native的当前实现不支持Blob: 。。。expo也不支持理想的react native fetch blob库: 此外,我看不到从服务器流式传输音频文件的方法。所包含的允许从url(例如)流式传输音频,但我看不到任何方式将授权标头附加到请求(例如“授权”:“承载
有没有办法实现这一点,或者下载并保存音频blob,或者从请求中包含授权标头的url流式传输?我可以把我的项目从世博会中分离出来,但我想把它作为最后的手段。是的,是的。您需要使用expo公开的模块来完成此操作。以下是从给定URL加载和播放音频文件时必须遵循的步骤。我还复制了我的组件的代码,它也在为我做同样的事情
- 加载expo公开的音频模块
从“expo”导入{Audio}
- 从中创建一个新的声音对象
soundObject=new Audio.Sound()
- 异步加载您的文件
等待this.soundObject.loadAsync({uri:this.props.source})
- 加载后,使用播放加载的文件
this.soundObject.playanc()
import React,{Component}来自'React';
从“react native”导入{View,TouchableNativeFeedback};
从“expo”导入{Audio};
类AudioPlayer扩展组件{
建造师(道具){
超级(道具);
this.state={isPlaying:false};
this.loadAudio=this.loadAudio.bind(this);
this.toggleAudioPlayback=this.toggleAudioPlayback.bind(this);
}
componentDidMount(){
这是loadAudio();
}
组件将卸载(){
this.soundObject.stopAsync();
}
异步加载音频(){
this.soundObject=新的Audio.Sound();
试一试{
等待this.soundObject.loadAsync({uri:this.props.source/*音频文件的url*/});
}捕获(e){
console.log('加载音频时出错',e);
}
}
toggleAudioPlayback(){
这是我的国家({
isPlaying:!this.state.isPlaying,
},()=>(this.state.isplay
?this.soundObject.playancy()
:this.soundObject.stopAsync());
}
render(){
返回(
{this.props.children}
);
}
}
导出默认音频播放器;
我想出来了。我应该在componentdidmount中使用异步加载声音。万一有人遇到这个问题
componentDidMount() {
this.loadAudio();
}
//async function to load the audio
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);
}
}
componentDidMount(){
这是loadAudio();
}
//用于加载音频的异步函数
异步加载音频(){
const{navigation}=this.props;
const id=navigation.getParam(“id”);
this.sound=新的Audio.sound();
for(设i=0;i
如果您的意思是从网络加载声音,请检查此模块:()我在Expo repo上发现了一个关于授权标题的问题:另外,您链接的blob support PR今天刚刚登陆,但我不确定它何时会稳定发布React Native。可能是一到两个月。@SedricHeidarizarei我认为如果不分离,在世博会内是行不通的。@dikaiosune谢谢你开刊。我曾考虑过在本期中引用的Base64编码解决方案中获取音频:但也没有办法将其输入到我可以看到的Expo的音频功能中。理想情况下,我希望能够缓存音频客户端,以防止重复调用服务器。