Giter Club home page Giter Club logo

Comments (9)

dmwallace avatar dmwallace commented on July 21, 2024 120

I found the same thing after a recent upgrade to the module. It appears you have to call .play() now inside the callback function passed to the Sound constructor. e.g.

old code (no longer works... if you console.log(sound) you will see that sound._loaded == false)

let sound = new Sound('path/to/file.wav', Sound.MAIN_BUNDLE, (error) => {
            if (error) {
                console.log('failed to load the sound', error);
            }
        });
        sound.play(); // no longer works... sound is not loaded yet

New code (works):

let sound = new Sound('path/to/file.wav', Sound.MAIN_BUNDLE, (error) => {
            if (error) {
                console.log('failed to load the sound', error);
            } else {
                sound.play(); // have to put the call to play() in the onload callback
            }
        });

from react-native-sound.

rgomezp avatar rgomezp commented on July 21, 2024 3

This should be made more clear in documentation

from react-native-sound.

Cspeisman avatar Cspeisman commented on July 21, 2024

Im running into the same exact issue

from react-native-sound.

CodeTitanian avatar CodeTitanian commented on July 21, 2024

Same here, only works with @dmwallace solution.

from react-native-sound.

dmwallace avatar dmwallace commented on July 21, 2024

@zmxv Is this an intended change in the design or a legit bug?

from react-native-sound.

zmxv avatar zmxv commented on July 21, 2024

Please upgrade to the latest version and check again. I've also published a demo project at https://github.com/zmxv/react-native-sound-demo, which has been tested with the latest version of RN on both iOS and Android.

from react-native-sound.

richiec avatar richiec commented on July 21, 2024

I got this too on the new codebase, using the demo code in the notes.

From what I can see, it's most likely because calling the initialisation code right before play() means the loading of the sound hasn't completed before play is called for the first time. This results in no sound, but also no error. You will likely get this if, for example, the whole basic usage code is placed within a single touch handler.

Placing all initialization code inside componentWillMount() seems to work well, but in practice there's no guarantee. The only way to be certain of handling the error is to test isLoaded() before calling play().

I have submitted a pull request to update the notes briefly to this effect.

from react-native-sound.

AlessioMTX avatar AlessioMTX commented on July 21, 2024

@dmwallace how do you pause the audio calling the play inside the callback? I'm struggling with it since I get that the sound variable is undefined

from react-native-sound.

n-ii-ma avatar n-ii-ma commented on July 21, 2024

I found the same thing after a recent upgrade to the module. It appears you have to call .play() now inside the callback function passed to the Sound constructor. e.g.

old code (no longer works... if you console.log(sound) you will see that sound._loaded == false)

let sound = new Sound('path/to/file.wav', Sound.MAIN_BUNDLE, (error) => {
            if (error) {
                console.log('failed to load the sound', error);
            }
        });
        sound.play(); // no longer works... sound is not loaded yet

New code (works):

let sound = new Sound('path/to/file.wav', Sound.MAIN_BUNDLE, (error) => {
            if (error) {
                console.log('failed to load the sound', error);
            } else {
                sound.play(); // have to put the call to play() in the onload callback
            }
        });

This is no solution. It is a bug.
This way the moment the Sound instance is created, the audio plays!

from react-native-sound.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.