javascript - Stop Audio expo-av - React Native Expo - Stack Overflow

I am using Audio from expo-av in react-native-expo.I can run the audio using loadAsync method on Audio

I am using Audio from expo-av in react-native-expo.

I can run the audio using loadAsync method on Audio.sound contructor.

Although when I try to stop the voice it doesn't stop the voice.

Following is the minimalistic code snippet. Is this the correct way to stop the sound. I am not able to find the solution for this.

import React, {useState,  useEffect} from 'react';
import {  Button } from 'react-native';
import { Audio } from 'expo-av';

export default function App() {
  const [audioStatus, setAudioStatus] = useState(false)
  const sound = new Audio.Sound();
  
  useEffect(()=>{
    (async () => {
            console.log('status', audioStatus)
            if (audioStatus) {
                await sound.loadAsync('.mp3')
                try {
                    await sound.playAsync()
                } catch (e) {
                    console.log(e)
                }
            }else {
                await sound.stopAsync()
                await sound.unloadAsync()
            }
          })()
  },[audioStatus])
  
  return (
      <Button color={audioStatus ? 'red' : 'green'} title={'play'} onPress={()=>setAudioStatus(!audioStatus)} />
  );
}

Thanks in Advance

I am using Audio from expo-av in react-native-expo.

I can run the audio using loadAsync method on Audio.sound contructor.

Although when I try to stop the voice it doesn't stop the voice.

Following is the minimalistic code snippet. Is this the correct way to stop the sound. I am not able to find the solution for this.

import React, {useState,  useEffect} from 'react';
import {  Button } from 'react-native';
import { Audio } from 'expo-av';

export default function App() {
  const [audioStatus, setAudioStatus] = useState(false)
  const sound = new Audio.Sound();
  
  useEffect(()=>{
    (async () => {
            console.log('status', audioStatus)
            if (audioStatus) {
                await sound.loadAsync('https://www.soundhelix./examples/mp3/SoundHelix-Song-1.mp3')
                try {
                    await sound.playAsync()
                } catch (e) {
                    console.log(e)
                }
            }else {
                await sound.stopAsync()
                await sound.unloadAsync()
            }
          })()
  },[audioStatus])
  
  return (
      <Button color={audioStatus ? 'red' : 'green'} title={'play'} onPress={()=>setAudioStatus(!audioStatus)} />
  );
}

Thanks in Advance

Share Improve this question asked Nov 11, 2020 at 11:26 nikhil024nikhil024 4798 silver badges22 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

This is a React problem. Each time your ponent renders, the sound variable is being reset to a new, not-yet-loaded Audio.Sound object. Here, sound should be part of the state of the ponent. You can change the line:

const sound = new Audio.Sound();

to

const [sound, setSound] = useState(new Audio.Sound());

and it will work.

Expo Snack

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745215488a4616994.html

相关推荐

  • javascript - Stop Audio expo-av - React Native Expo - Stack Overflow

    I am using Audio from expo-av in react-native-expo.I can run the audio using loadAsync method on Audio

    15小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信