如何在 React Native 中播放声音?

Tri*_*Tri 9 javascript android react-native react-native-sound

我想在 React Native 中播放声音。

我曾尝试在zmxv/react-native-sound 中阅读这里,但作为像我这样的初学者,该文档让我很困惑如何在 React Native 代码中应用它。

在我尝试使用此方法在事件上制作本机播放声音并制作如下代码之前:

import React, { Component } from 'react'
import { StyleSheet, Text, View, TouchableOpacity } from 'react-native'
const Sound = require('react-native-sound')


export default class MovieList extends Component {

    handlePress() {
        // Play some sound here
        let hello = new Sound('motorcycle.mp3', Sound.MAIN_BUNDLE, (error) => {
            if (error) {
              console.log(error)
            }
          })

          hello.play((success) => {
            if (!success) {
              console.log('Sound did not play')
            }
          })
    }

    render() {
        const { movie } = this.props
        return (
            <TouchableOpacity onPress={this.handlePress.bind(this)}>
                <View>
                      <Text>Start</Text>
                </View>
            </TouchableOpacity>
        )
    }
}
Run Code Online (Sandbox Code Playgroud)

这就是我放音频的地方:

MyProject/android/app/src/main/res/raw/motorcycle.mp3

项目结构

项目结构

那么,我的代码有什么问题?

Moh*_*faq 11

这将预加载声音,当您按下播放按钮时,它将播放它。

export default class MovieList extends Component {
    componentDidMount(){
      this.hello = new Sound('whoosh.mp3', Sound.MAIN_BUNDLE, (error) => {
        if (error) {
          console.log('failed to load the sound', error);
          return;
        }
      });
    }
    
    
    handlePress() {
      this.hello.play((success) => {
        if (!success) {
          console.log('Sound did not play')
        }
      })
    }

    render() {
        const { movie } = this.props
        return (
            <TouchableOpacity onPress={this.handlePress.bind(this)}>
                <View>
                      <Text>Start</Text>
                </View>
            </TouchableOpacity>
        )
    }
}
Run Code Online (Sandbox Code Playgroud)

如果您想从声音列表中播放音轨,请查看此要点以获取详细代码。


Tri*_*Tri 10

非常感谢谁回答了这个问题,但我已经用这个简单的方法解决了这个问题:

import React, { Component } from 'react'
import { Text, View, TouchableOpacity } from 'react-native'
import Sound from 'react-native-sound';

export default class MovieList extends Component {

    sound = new Sound('motorcycle.mp3');

    playSound = () => {
        this.sound.play()
    }

    render() {
        return (
            <View>
                <TouchableOpacity onPress={this.playSound}>
                    <View>
                        <Text>Start</Text>
                    </View>
                </TouchableOpacity>
            </View>
        )
    }
}
Run Code Online (Sandbox Code Playgroud)


Vis*_*dav 5

尝试使用以下代码来播放声音:

setTimeout(() => {
     var sound = new Sound("motorcycle.mp3",Sound.MAIN_BUNDLE, (error) => {
                     /* ... */
     });

     setTimeout(() => {
         sound.play((success) => {
                  /* ... */
         });
    }, 100);
}, 100);
Run Code Online (Sandbox Code Playgroud)

它很hacky并通过https://github.com/zmxv/react-native-sound/issues/89#issuecomment-276678935解决