【问题标题】:How to play sound in React Native?如何在 React Native 中播放声音?
【发布时间】:2019-05-13 05:08:27
【问题描述】:

我想在 React Native 中播放声音。

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

在我尝试this one 之前,在事件上做出反应本机播放声音并制作如下代码:

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

这是我放音频的地方:

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

项目结构

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

【问题讨论】:

  • 我认为您是在加载之前播放声音。

标签: javascript android react-native react-native-sound


【解决方案1】:

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

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

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

【讨论】:

  • 谢谢@Mohammed Ashfaq,你的回答很完美,但我用我的代码做了一个简单的回答。
  • @mohammed 如何在加载视图时播放声音,以及如何在音乐列表选择中播放多个声音。
  • @Chandni。请检查此链接以播放音乐列表中的多个声音。 Link
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    试试这个代码来播放声音:

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

    https://github.com/zmxv/react-native-sound/issues/89#issuecomment-276678935 解决了这个问题

    【讨论】:

      【解决方案4】:

      对于 iOS,对我有用的是:

      1. 检查我的资源文件是否正在 xCode 中播放
        • 打开 xCode
        • 选择文件
        • 在 xCode 播放器中点击播放
      2. 播放声音后(某些文件有问题,可能是编码问题),
        • 将文件添加为资源(确保处于构建阶段)
      3. 在 xCode 中编译项目并从那里运行它
        • 注意:每次更改资源或期望新资源时,您都需要通过 xCode 或 npm run ios 编译您的 Native 应用程序

      这是我的代码:

      const sound = new Sound(
          "myFile.mp3",
          Sound.MAIN_BUNDLE,
          error => {
            if (error) {
              console.log("failed to load the sound", error);
              return;
            }
            sound.play(() => sound.release());
          }
        );
      // The play dispatcher
      sound.play();
      

      【讨论】:

      • 这一行让我与众不同:sound.play(() =&gt; sound.release());
      猜你喜欢
      • 2020-10-18
      • 2019-04-07
      • 1970-01-01
      • 2021-07-16
      • 1970-01-01
      • 2019-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多