【问题标题】:React Native Audio Playing mp3 files with spaces in their namesReact Native Audio 播放名称中带有空格的 mp3 文件
【发布时间】:2019-08-27 12:42:38
【问题描述】:

我一直在尝试使用 Expo 构建一个响应原生的媒体播放器,以便能够在我的音乐项目中播放音频。

我已经成功地破解了一个与首选设计等一起的问题,但我仍然有一个小错误。在这里,我从 API 端点接收信息,其中包含指向存储在服务器中的文件的链接。当文件名只有一个单词时播放此音频。当名称中有空格时,文件不会播放。例如 .../musics/test.mp3 播放而 .../musics/test 32.mp3 不播放。任何关于如何在 React native 中处理这个问题的想法都将受到高度赞赏。我的播放功能

 startPlay = async (index = this.index, playing = false) => {
        const url = this.list[index].url;
        this.index = index;
        console.log(url);
        // Checking if now playing music, if yes stop that
        if(playing) {
            await this.soundObject.stopAsync();
        } else {
            // Checking if item already loaded, if yes just play, else load music before play
            if(this.soundObject._loaded) {
                await this.soundObject.playAsync();
            } else {
                await this.soundObject.loadAsync(url);
                await this.soundObject.playAsync();
            }
        }
    };

url 是文件的链接。

我在一个流媒体平台上工作,我很想得到一个类似的播放器:

类似https://hackernoon.com/building-a-music-streaming-app-using-react-native-6d0878a13ba4

但我正在使用 React 本机博览会。我在网上遇到的所有实现都是使用原生的,没有博览会。任何指向任何已经使用 expo 完成的工作的指针都会有很大的帮助,例如 packages 。

谢谢

【问题讨论】:

  • 将每个空格替换为%20
  • 我该怎么做,因为名称是从我无法控制的 API 返回的。这只是意味着我必须阅读整个名称并替换 ??一种不是有效的解决方案
  • 看看我的answer。这是添加到您提供的代码中的单行代码:const url = encodeURI(uri); 并可以选择更改变量的名称:const uri = this.list[index].url; 解释这是如何效率低下的?

标签: javascript react-native audio expo


【解决方案1】:

网址应该被编码:

const uri = this.list[index].url;
this.index = index;
const url = encodeURI(uri);
console.log(url);

uri = "../musics/test 32.mp3" 将被编码为url = "../musics/test%2032.mp3"

【讨论】:

  • 不客气,别忘了查看帖子,因为它解决了您的问题。 ✅
  • 我已经检查过了。请问您知道任何与 expo 一起使用的 react native 播放器吗?就像上面共享的屏幕上的那样
  • 实际上,您通过单击 ?(谢谢)进行了投票,但还有一个看起来像复选标记 ✔ 的附加按钮,用于检查解决问题的答案和/或最佳答案。 React Expo
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-02-17
  • 2021-12-30
  • 1970-01-01
  • 2020-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多