javascript - Play sound in iOS device in react-native - Stack Overflow

In my app I have a feature that plays a sound when a message is received it will notify the user by pla

In my app I have a feature that plays a sound when a message is received it will notify the user by playing a sound file, the app works using react-native-sound fine in Android but in iOS I keep getting this error:

Code I am using for initializing the sound:

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());
});

The method that I call after initializing sound file:

message_received.play()

Does anyone know how to solve this?

In my app I have a feature that plays a sound when a message is received it will notify the user by playing a sound file, the app works using react-native-sound fine in Android but in iOS I keep getting this error:

Code I am using for initializing the sound:

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());
});

The method that I call after initializing sound file:

message_received.play()

Does anyone know how to solve this?

Share Improve this question asked Jul 10, 2019 at 17:20 0x01Brain0x01Brain 8182 gold badges14 silver badges29 bronze badges 6
  • 1 from react-native-sound doc: iOS: Open Xcode and add your sound files to the project (Right-click the project and select Add Files to [PROJECTNAME]). Then you can call it like new Sound('Page_Turn.mp3', – Ilario Commented Jul 10, 2019 at 19:42
  • I did that and still getting same error. – 0x01Brain Commented Jul 10, 2019 at 19:46
  • the error appears when you do .play() or when you init the sound? – Ilario Commented Jul 10, 2019 at 19:49
  • 1 @llario your solution worked thanks! – 0x01Brain Commented Jul 12, 2019 at 4:14
  • 1 Great! Can you accept my answer please? :) – Ilario Commented Jul 12, 2019 at 15:49
 |  Show 1 more ment

2 Answers 2

Reset to default 3
  1. If you use require(filepath), you don't need Sound.MAIN_BUNDLE, the second param is callback:

    new Sound(require("../../res/audio/Page_Turn.mp3"), (error) => {})

  2. play function should be called after initialising successfully

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

  3. Remember to add(references) your .mp3 files into xcode, re-run react-native run-ios.

This code example contains many use-cases: https://github./zmxv/react-native-sound-demo/blob/master/main.js

From react-native-sound doc:

iOS: Open Xcode and add your sound files to the project (Right-click the project and select Add Files to [PROJECTNAME]).

Then you can call it

new Sound('Page_Turn.mp3',

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744334557a4569049.html

相关推荐

  • javascript - Play sound in iOS device in react-native - Stack Overflow

    In my app I have a feature that plays a sound when a message is received it will notify the user by pla

    8天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信