react native sound error in react native cli project
TypeError: resolveAssetSource is not a function
React Native module for playing sound clips on iOS, Android, and Windows.
Be warned, this software is alpha quality and may have bugs. Test on your own and use at your own risk!
Basically error is comming
after a long time wasting i have fixed the issue which one i am going to share with you. now i will explain the react native sound which one you will start from cloud URL.
1. install --- npm i react-native-sound
2. Node_module/react-native-sound/sound.js open this file and replace thise code
//var resolveAssetSource = require("react-native/Libraries/Image/resolveAssetSource"); //old
var resolveAssetSource = require('react-native/Libraries/Image/resolveAssetSource').default; //add this new line
3. then go to the android and clean ./gradlew clean
4. then Start the project npx react-native run-android
here is final code
import React from 'react';
import { View, Text, TouchableOpacity, StyleSheet } from 'react-native';
import Sound from 'react-native-sound';
Sound.setCategory('Playback');
const TestSound = () => {
const playSoundButton = () => {
const sound = new Sound(
'https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3',
null,
(error) => {
if (error) {
console.log('Sound load error:', error);
return;
}
sound.play((success) => {
if (success) {
console.log('Playback finished successfully');
} else {
console.log('Playback failed due to decoding errors');
}
sound.release();
});
}
);
}
return (
<View style={styles.container}>
<TouchableOpacity onPress={playSoundButton} style={styles.button}>
<Text style={styles.buttonText}>Play Sound</Text>
</TouchableOpacity>
</View>
);
};
const styles = StyleSheet.create({
container: { flex: 1, justifyContent: 'center', alignItems: 'center' },
button: { backgroundColor: '#007AFF', padding: 15, borderRadius: 10 },
buttonText: { color: '#fff', fontSize: 16 }
});
export default TestSound;
After clearing your build cache, you should execute a new react-native
build.
If you still experience issues, know that this is the most common build issue. See #592 and the several issues linked from it for possible resolution. A pull request with improved documentation on this would be welcome!
if you are using new version no need this manual installation X
This is not necessary if you have used react-native link
Edit android/settings.gradle
to declare the project directory:
include ':react-native-sound'
project(':react-native-sound').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-sound/android')
Edit android/app/build.gradle
to declare the project dependency:
dependencies {
...
compile project(':react-native-sound')
}
Edit android/app/src/main/java/.../MainApplication.java
to register the native module:
...
import com.zmxv.RNSound.RNSoundPackage; // <-- New
...
public class MainApplication extends Application implements ReactApplication {
...
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new RNSoundPackage() // <-- New
);
}