import React from 'react'; import { SafeAreaView, ScrollView, StatusBar, StyleSheet, Text, Image, TouchableOpacity, View, DeviceEventEmitter } from 'react-native'; import Animated, { withSpring, withTiming, useAnimatedStyle, useSharedValue, useValue, Easing } from 'react-native-reanimated'; import Slider from '@react-native-community/slider'; import { colors, getLocalTime, myConfirm, onePx, px2dp, toastShort } from '../../utils'; import Sound from 'react-native-sound'; import { BjTypeVoice } from '../../utils/types'; import * as Progress from 'react-native-progress'; import RNFS from 'react-native-fs'; interface Props { index: number voice: BjTypeVoice onConfirmToDelete: Function } interface State { } const KitVoiceRow = (props: Props) => { const { voice: { duration = 0, audioPath = "", recordEndDate = null, name = "", uploadProgress = 0 } } = props; const [playing, setPlaying] = React.useState(false); const isPlayingRef = React.useRef(false); const [playDuration, setPlayDuration] = React.useState(0); //实时播放进度 const playDurationRef = React.useRef(null); const active = React.useRef(false); const voiceRowActiveChangeListener = React.useRef(null); const playerInstance = React.useRef(null); const delayTimer = React.useRef(null); const delaySetTimer = React.useRef(null); const beforeSlidePlayingStatus = React.useRef(false); const offsetY = useSharedValue(0); const topValue = useSharedValue(-28); const heightValue = px2dp(72); React.useEffect(() => { voiceRowActiveChangeListener.current = DeviceEventEmitter.addListener('on-voice-row-active-opened', (a) => { if (a.index !== props.index && active.current) { offsetY.value = px2dp(0); topValue.value = px2dp(-28); active.current = false; setPlayDuration(0); handleStopPlay(); if (playerInstance.current) { playerInstance.current = null; } } }); return () => { delayTimer.current && clearTimeout(delayTimer.current); delaySetTimer.current && clearTimeout(delaySetTimer.current); if (playing) { playerInstance.current && playerInstance.current.pause(); } voiceRowActiveChangeListener.current && voiceRowActiveChangeListener.current.remove(); } }, []); const containerAnimateStyle = useAnimatedStyle(() => { return { height: withTiming(offsetY.value + heightValue), }; }); const controlerAnimateStyle = useAnimatedStyle(() => { return { top: withTiming(topValue.value), //top: 72 }; }); const switchRowActive = () => { if (active.current) { offsetY.value = px2dp(0); topValue.value = px2dp(-28) handleStopPlay(); } else { offsetY.value = px2dp(100) topValue.value = px2dp(72); DeviceEventEmitter.emit('on-voice-row-active-opened', { index: props.index }) } active.current = !active.current; }; const switchVoicePlaying = () => { if (playing) { handleStopPlay() } else { handleStartPlay() } } const handleStopPlay = () => { setPlaying(false); if (playerInstance.current) { playerInstance.current.pause(); delayTimer.current && clearTimeout(delayTimer.current) } }; const handleStartPlay = () => { setPlaying(true); delayTimer.current && clearTimeout(delayTimer.current) if (!playerInstance.current) { //如果没有播放器实例 const audioPathList = audioPath.split('quick_audio'); console.log("audioPathList", audioPath, audioPathList) let _audioPath = RNFS.DocumentDirectoryPath + '/quick_audio' + audioPathList[1]; // alert(_audioPath) playerInstance.current = new Sound(_audioPath, '', (err) => { if (err) { toastShort('加载音频失败') return console.log(err) } startRecordingTime(); if (playDuration) { playerInstance.current.setCurrentTime(Number(Number(playDuration / 1000).toFixed(2))); } playerInstance.current && playerInstance.current.play(success => { if (success) { console.warn('success - 播放成功') // toastShort('播放完毕') delayTimer.current && clearTimeout(delayTimer.current) delaySetTimer.current = setTimeout(() => { setPlayDuration(0); setPlaying(false); }, 200); } else { console.warn('fail - 播放失败') toastShort('播放失败') } }) }) } else { startRecordingTime(); playerInstance.current && playerInstance.current.play(success => { if (success) { console.warn('success - 播放成功1') // toastShort('播放完毕'); delayTimer.current && clearTimeout(delayTimer.current) delaySetTimer.current = setTimeout(() => { setPlayDuration(0); setPlaying(false); }, 200); } else { console.warn('fail - 播放失败') toastShort('播放失败') } }) } }; const startRecordingTime = () => { delayTimer.current = setInterval(() => { playerInstance.current.getCurrentTime(seconds => { let ms = seconds * 1000; console.log("startRecordingTime", ms, duration) if (ms >= (duration - 500)) { console.log("需要结束了") setPlayDuration(duration) clearTimeout(delayTimer.current) return false }; setPlayDuration(ms) }) }, 50) } const adaptDuration = () => { let min = Math.floor(duration / 60000); let sec = Math.ceil(duration / 1000 % 60); if (min < 10) { //@ts-ignore min = '0' + min }; if (sec < 10) { //@ts-ignore sec = '0' + sec; } return `${min}:${sec}` }; const adaptDurationTime = () => { let min = Math.floor(playDuration / 60000); let sec = Math.ceil((playDuration / 1000 % 60)); if (min < 10) { //@ts-ignore min = '0' + min }; if (sec < 10) { //@ts-ignore sec = '0' + sec; } return `${min}:${sec}` } const adaptLastDurationTime = () => { const lastTime = duration - playDuration; let min = Math.floor(lastTime / 60000); let sec = Math.ceil((lastTime / 1000 % 60)); if (min < 10) { //@ts-ignore min = '0' + min }; if (sec < 10) { //@ts-ignore sec = '0' + sec; } return `${min}:${sec}` }; const adpatProgress = () => { return Number(Number(playDuration / duration).toFixed(2)) } const _onSlidingStart = () => { // console.log("onSlidingStart"); if (playing) { beforeSlidePlayingStatus.current = true; } else { beforeSlidePlayingStatus.current = false; } handleStopPlay(); } const _onSlidingComplete = () => { // console.log("_onSlidingComplete"); playerInstance.current && playerInstance.current.setCurrentTime(Number(Number(playDuration / 1000).toFixed(2))); if (beforeSlidePlayingStatus.current) { handleStartPlay(); } }; const handleChangeProgress = (progress) => { // console.log("handleChangeProgress-progress", progress); // console.log('aaa', duration * progress) setPlayDuration(duration * progress) }; const deleteVoice = () => { let comfirmData = { title: `确认删除录音文件-${name}?`, confirmText: "确认", confirmFun: confirmToDeleteVoice }; myConfirm(comfirmData) }; const confirmToDeleteVoice = () => { props.onConfirmToDelete && props.onConfirmToDelete(props.index) } return ( {name} {getLocalTime('YY:MM:DD HH:MM', recordEndDate)} {adaptDuration()} {adaptDurationTime()} -{adaptLastDurationTime()} { playing ? : } { uploadProgress !== 0 && uploadProgress < 1 ? <> 正在上传... : null } ) } const styles = StyleSheet.create({ voiceRow: { position: 'relative', width: px2dp(330), backgroundColor: "#fff", height: px2dp(72), overflow: 'hidden', marginBottom: 1, }, voiceRowContent: { width: '100%', height: px2dp(72), paddingLeft: px2dp(10), paddingRight: px2dp(16), paddingVertical: px2dp(12), justifyContent: 'space-between', alignItems: 'flex-start', backgroundColor: '#fff' }, voiceOptionRow: { width: '100%', height: px2dp(60), flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', paddingHorizontal: px2dp(16), paddingBottom: px2dp(8) }, voiceName: { fontSize: 16, color: colors.blackColor, }, voiceRowInfo: { width: '100%', flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', // backgroundColor: 'red' }, voiceRowInfoText: { fontSize: 14, color: '#aaa' }, voiceProgressInfo: { width: '100%', height: px2dp(16), flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', paddingLeft: px2dp(16), paddingRight: px2dp(18), marginTop: px2dp(-14) }, voiceProgressInfoText: { fontSize: 12, color: '#7f7f7f' }, voiceControl: { position: 'absolute', top: px2dp(-72), left: 0, width: '100%', height: px2dp(100), // backgroundColor: 'red', zIndex: -1 }, voiceProgress: { width: '100%', justifyContent: 'center', alignItems: 'center' }, deleteIcon: { width: px2dp(18), height: px2dp(18), tintColor: '#7f7f7f' }, optionBtn: { paddingVertical: px2dp(6), paddingHorizontal: px2dp(6) }, optionMainBtn: { width: px2dp(42), height: px2dp(42), justifyContent: 'center', alignItems: 'center', // backgroundColor: 'red' }, optionIcon: { width: px2dp(30), height: px2dp(30), tintColor: '#656565' }, optionMiniIcon: { width: px2dp(24), height: px2dp(24), tintColor: '#656565' }, voiceOptionRowContent: { width: px2dp(160), flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center' }, progressBarContainer: { position: 'absolute', bottom: 0, left: 0, width: '100%', height: px2dp(2), // backgroundColor: 'red', zIndex: 999 }, progressBarTextContainer: { position: 'absolute', bottom: 0, left: 0, // width: '100%', height: px2dp(18), // backgroundColor: '#fff', zIndex: 1000, paddingLeft: px2dp(10) }, progressBar: { borderWidth: 0, } }) export default KitVoiceRow;