addVoice.js
4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/**
* Created by Cassie on 2018/05/14
*/
import React, { Component } from 'react';
import {
StyleSheet,
View,
Text,
TouchableOpacity,
Image,
Platform,
InteractionManager,
NativeModules,
PermissionsAndroid
} from 'react-native';
import {AudioRecorder,AudioUtils} from 'react-native-audio';
import {zoomW,zoomH} from '../../utils/getSize';
const cancle = require('../../img/cancle.png');
const ok = require('../../img/ok.png');
const record = require('../../img/record.png');
const pauseRecord = require('../../img/pauseRecord.png');
export default class AddVoice extends Component {
static navigationOptions = ({ navigation, screenProps }) => ({
header:null
});
constructor(props){
super(props);
this.state = {
currentTime:'00:00',
recording:false,
stoppedRecording:false,
finished:false,
audioPath:AudioUtils.DocumentDirectoryPath + '/test.aac',
canRecord:true
};
};
componentWillMount(){
};
componentDidMount(){
AudioRecorder.prepareRecordingAtPath(this.state.audioPath,{
SampleRate:22050,
Channels:1,
AudioQuality:"Low",
AudioEncoding:"aac",
AudioEncodingBitRate:32000
});
AudioRecorder.onProgress = (data) => {
this.setState({
currentTime:this.formatTime(Math.floor(data.currentTime))
});
};
AudioRecorder.onFinished = (data) => {
console.log(data.audioFileURL)
global.videoList.push(data.audioFileURL);
InteractionManager.runAfterInteractions(() => {this.props.navigation.goBack();})
};
};
/*时间转换*/
formatTime(seconds) {
return[
parseInt(seconds/60%60),
parseInt(seconds%60)
].join(":").replace(/\b(\d)\b/g,"0$1");
};
/*开始录音*/
onRecord(){
AudioRecorder.startRecording();
this.setState({
canRecord:false
});
};
/*暂停录音*/
pauseRecord(){
AudioRecorder.pauseRecording();
this.setState({
canRecord:true
});
};
/*完成录音*/
async doneRecord(){
if(!this.state.canRecord){
this.pauseRecord();
}
if(this.state.currentTime != '00:00'){
AudioRecorder.stopRecording();
}
}
render(){
return(
<View style={styles.background}>
<Text style={{fontSize:24,color:'#333',fontWeight:'bold',marginTop:(140/zoomH)}}>录音时间</Text>
<Text style={{fontSize:48,color:'#00be3c',marginTop:(30/zoomH)}}>{this.state.currentTime}</Text>
<View style={styles.videoSetting}>
<TouchableOpacity activeOpacity={0.8} onPress={() => InteractionManager.runAfterInteractions(() => {this.props.navigation.goBack();})}>
<Image source={cancle} style={{width:(50/zoomH),height:(50/zoomH)}} resizeMode="contain" />
</TouchableOpacity>
{this.state.canRecord && <TouchableOpacity activeOpacity={0.8} onPress={() => this.onRecord()}>
<Image source={record} style={{width:(120/zoomH),height:(120/zoomH)}} />
</TouchableOpacity>}
{!this.state.canRecord && <TouchableOpacity activeOpacity={0.8} onPress={() => this.pauseRecord()}>
<Image source={pauseRecord} style={{width:(120/zoomH),height:(120/zoomH)}} />
</TouchableOpacity>}
<TouchableOpacity activeOpacity={0.8} onPress={() => this.doneRecord()}>
<Image source={ok} style={{width:(50/zoomH),height:(50/zoomH)}} resizeMode="contain"/>
</TouchableOpacity>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
background:{
flex:1,
backgroundColor:'#fff',
display:'flex',
alignItems:'center'
},
videoSetting:{
width:(300/zoomW),
marginTop:(250/zoomH),
display:'flex',
flexDirection:'row',
justifyContent:'space-between',
alignItems:'center'
}
});