gpt4 book ai didi

react-native - React Native Expo 打开和关闭音频

转载 作者:行者123 更新时间:2023-12-04 02:43:10 25 4
gpt4 key购买 nike

我想在我的 React Native Expo 应用程序中打开和关闭声音,但是当我多次单击打开和关闭时它会导致声音重叠,我不确定是否需要设置音频状态?我在这里似乎找不到任何好的例子 - https://docs.expo.io/versions/latest/sdk/av/不确定我做错了什么,RN 新手。

如您所见,这是一个标题按钮,它必须在整个应用程序中发挥作用,而不仅仅是一个屏幕。

这是我的零食 - https://snack.expo.io/@roshambo/sound-toggle-on-and-off

import React, { useState } from "react";
import { HeaderButton } from "react-navigation-header-buttons";
import { Ionicons } from "@expo/vector-icons";
import { Audio } from "expo-av";

const CustomHeaderButton = props => {
const [soundIcon, setSoundIcon] = useState(false);

const soundObject = new Audio.Sound();
(async () => {
await soundObject.loadAsync(require("../assets/bglaughs.mp3"), {
volume: 0.25,
isLooping: true,
shouldPlay: true,
isMuted: true
});
})();

const toggleSound = () => {
setSoundIcon(prevState => !prevState);
soundObject.setOnPlaybackStatusUpdate(this._onPlaybackStatusUpdate);
};

_onPlaybackStatusUpdate = playbackStatus => {
if (playbackStatus.isMuted) {
playSound();
} else {
stopSound();
}
};

const stopSound = async () => {
try {
await soundObject.stopAsync();
} catch (error) {
console.log("sound couldn't pause");
}
};

const playSound = async () => {
try {
await soundObject.setIsMutedAsync(false);
await soundObject.playAsync();
} catch (error) {
console.log("sound couldn't play");
}
};

return (
<HeaderButton
{...props}
IconComponent={Ionicons}
iconSize={23}
iconName={soundIcon ? "ios-volume-high" : "ios-volume-off"}
onPress={toggleSound}
/>
);
};

export default CustomHeaderButton;

更新:

这是我基于 Oleg 的回答以及我在 redux 上的实现的工作代码。

组件

import React from "react";
import { HeaderButton } from "react-navigation-header-buttons";
import { useDispatch, useSelector } from "react-redux";

import * as soundActions from "../store/actions/sound-action";

import { Ionicons } from "@expo/vector-icons";

const CustomHeaderButton = props => {

const sound = useSelector(state => state.sound.soundState);

const dispatch = useDispatch();

return (
<HeaderButton
{...props}
IconComponent={Ionicons}
iconSize={23}
iconName={sound === "playing" ? "ios-volume-high" : "ios-volume-off"}
onPress={() => {
dispatch(soundActions.toggleSound(sound));
}}
/>
);
};

export default CustomHeaderButton;

reducer

import { TOGGLE_SOUND } from "../actions/sound-action";

import { Audio } from "expo-av";

const initialState = {
soundState: "nosound"
};

export default (state = initialState, action) => {
switch (action.type) {
case TOGGLE_SOUND:
if (state.soundState === "nosound") {
(async () => {
const { sound } = await Audio.Sound.createAsync(
require("../../assets/bglaughs.mp3"),
{
shouldPlay: true,
volume: 0.25,
isLooping: true
}
);
this.sound = sound;
})();
return {
...state,
soundState: "playing"
};
} else if (state.soundState === "playing") {
(async () => {
if (this.sound !== null) {
await this.sound.pauseAsync();
}
})();
return { ...state, soundState: "donepause" };
} else if (state.soundState === "donepause") {
(async () => {
await this.sound.playAsync();
})();
return { soundState: "playing" };
}
}
return state;
};

最佳答案

我为您的问题创建了零食答案:您必须注意创建声音的新方法并将标志循环设置为 false。

https://snack.expo.io/@djalik/sound-toggle-on-and-off

关于react-native - React Native Expo 打开和关闭音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58369129/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com