2

在我的应用程序中,我有一个功能可以在收到消息时播放声音,它会通过播放声音文件来通知用户,该应用程序在 Android 中使用react-native-sound正常工作,但在 iOS 中我不断收到此错误:

在此处输入图像描述

我用于初始化声音的代码:

import Sound from "react-native-sound"

// Load the sound file 'whoosh.mp3' from the app bundle
// See notes below about preloading sounds within initialization code below.
var message_received = new Sound(require("../../res/audio/Page_Turn.mp3"), Sound.MAIN_BUNDLE, (error) => {
    if (error) {
      console.log('failed to load the sound', error);
      return;
    }
    // loaded successfully
    console.log('duration in seconds: ' + message_received.getDuration() + 'number of channels: ' + message_received.getNumberOfChannels());
});

我在初始化声音文件后调用的方法:

message_received.play()

有谁知道如何解决这个问题?

4

2 回答 2

3
  1. 如果使用require(filepath),则不需要Sound.MAIN_BUNDLE,第二个参数是回调:

    新声音(需要(“../../res/audio/Page_Turn.mp3”),(错误)=> {})

  2. play初始化成功后应该调用函数

    var message_received = new Sound(require("../../res/audio/Page_Turn.mp3"), (error) => { if (error) { console.log('failed to load the sound', error) ; return; } // 加载成功 message_received.play() });

  3. 请记住将您的 .mp3 文件添加(引用)到 xcode 中,重新运行react-native run-ios

此代码示例包含许多用例:https ://github.com/zmxv/react-native-sound-demo/blob/master/main.js

于 2019-11-14T02:47:01.307 回答
1

来自react-native-sound文档:

iOS:打开 Xcode 并将您的声音文件添加到项目中(右键单击项目并选择 Add Files to [PROJECTNAME])。

然后你可以调用它

new Sound('Page_Turn.mp3',

于 2019-07-12T15:48:56.647 回答