【问题标题】:React Native Audio Animation反应原生音频动画
【发布时间】:2017-08-05 03:20:01
【问题描述】:

我目前正在使用React Native Audio 录制音频并存储声音。我想知道是否有任何方法可以实时获取此录制的进度,以将麦克风输入映射到动画,以提供麦克风正在工作的视觉反馈。

目前,根据我的发现,包的 onProgress() 函数仅发送当前时间码。

感谢您的帮助!

【问题讨论】:

标签: animation audio react-native waveform


【解决方案1】:

我目前正在研究类似的东西。

我就是这么干的。

import React, { Component } from 'react';
import {
    Platform,
    StyleSheet,
    Text,
    View,
    TouchableOpacity,
    Button,
    LayoutAnimation,
    Image,
    ScrollView,
    Animated
} from 'react-native';
export default class App extends Component {
    state = {
        isPressed: false,
        animated: new Animated.Value(0),
        opacityA: new Animated.Value(1),
    }
    constructor(props) {
        super(props);
        this._onPress = this._onPress.bind(this);
    }
    _runAnimation() {
        const { animated, opacityA } = this.state;

        Animated.loop(
            Animated.parallel([
                Animated.timing(animated, {
                    toValue: 1,
                    duration: 1000,

                }),
                Animated.timing(opacityA, {
                    toValue: 0,
                    duration: 1000,

                })
            ])
        ).start();
    }
    _stopAnimation() {
        Animated.loop(
            Animated.parallel([
                Animated.timing(animated),
                Animated.timing(opacityA)
            ])
        ).stop();
    }
    _onPress() {
        this.setState(
            state => ({ isPressed: !state.isPressed }),
        )
    }
    _micButton() {
        const { isPressed, animated, opacityA, } = this.state;
        if (isPressed) {
            //some function
            this._runAnimation();
            return (
                <Animated.View style={{
                    width: 100,
                    height: 100,
                    borderRadius: 50,
                    backgroundColor: 'rgba(153,0,0,0.4)',
                    opacity: opacityA,
                    transform: [
                        {
                            scale: animated
                        }
                    ]
                }}>
                    {/* icon or image */}
                </Animated.View>
            );
        } else {
            //some function
            return (
                <View style={{
                    width: 100,
                    height: 100,
                    borderRadius: 50,
                    backgroundColor: 'rgba(153,0,0,0.4)',
                }}>
                    {/* icon or image */}
                </View>
            );
        }
    }






    render() {
        return (
            <View style={styles.container}>
                <TouchableOpacity onPress={this._onPress}>
                    {this._micButton()}
                </TouchableOpacity>
            </View>
        );
    }
}


const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#F5FCFF',
    },

});

希望能解决你的问题。

【讨论】:

    【解决方案2】:

    我使用expo-av。有一个APIsetOnRecordingStatusUpdate,你可以设置一个函数,定期调用记录的状态。例如,

     async startRecording(callback: onRecordingStatusUpdated) {
        this.isLoading = true
    
        await Audio.setAudioModeAsync({
          allowsRecordingIOS: true,
          interruptionModeIOS: Audio.INTERRUPTION_MODE_IOS_DO_NOT_MIX,
          playsInSilentModeIOS: true,
          shouldDuckAndroid: true,
          interruptionModeAndroid: Audio.INTERRUPTION_MODE_ANDROID_DO_NOT_MIX,
          playThroughEarpieceAndroid: true
        });
        if (this.recording !== null) {
          // only one recorder is allowed to exist
          this.recording.setOnRecordingStatusUpdate(null);
          this.recording = null;
        }
    
        const recording = new Audio.Recording();
        await recording.prepareToRecordAsync(this.recordingSettings);
        // ✨✨✨set the callback
        recording.setOnRecordingStatusUpdate(callback);
    
        this.recording = recording;
        await this.recording.startAsync(); // Will call this._updateScreenForRecordingStatus to update the screen.
        this.isLoading = false
      }
     // ???
     startRecording((status => console.log('[onRecording]', status)))
    

    【讨论】:

      猜你喜欢
      • 2018-04-17
      • 2018-05-16
      • 1970-01-01
      • 2022-06-13
      • 1970-01-01
      • 2022-11-04
      • 2017-06-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多