Hi there,
Just trying to implement a basic example with your component. Everything seems to be fine until I call play() and then nothing happens. I've tried delaying the call with setTimeout() but no luck.
Here's my code:
componentDidMount: function() {
var snd_testShort = new Sound('mymp3.mp3', Sound.MAIN_BUNDLE, (error) => {
if (error) {
console.log('snd_testShort - failed to load the sound', error);
} else {
//all the file details come back and are logged...
console.log('snd_testShort - duration in seconds: ' + snd_testShort._duration +
' number of channels: ' + snd_testShort._numberOfChannels);
//the below line fires
console.log("snd_testShort - play attempt");
//I get nothing back from the `play()` call - I can breakpoint in XCode and the play method there fires without issue
snd_testShort.play((test) => {
console.log("snd_testShort - TEST");
if (test) {
console.log('snd_testShort - successfully finished playing');
} else {
console.log('snd_testShort - playback failed due to audio decoding errors');
}
});
}
});
As stated, in XCode I can step through the following in RNSound (seemingly) without issue:
RCT_EXPORT_METHOD(play:(nonnull NSNumber*)key withCallback:(RCTResponseSenderBlock)callback) {
AVAudioPlayer* player = [self playerForKey:key];
if (player) {
[[self callbackPool] setObject:[callback copy] forKey:key];
[player play];
}
}
Im running into the same exact issue
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
}
});
Same here, only works with @dmwallace solution.
@zmxv Is this an intended change in the design or a legit bug?
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.
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.
This should be made more clear in documentation
Most helpful comment
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)
New code (works):