Attempt to fix audio issue

This commit is contained in:
Zhiyuan Zheng 2021-09-15 22:52:21 +02:00
parent 371830ff19
commit 1880dd62d1
3 changed files with 47 additions and 17 deletions

View File

@ -50,8 +50,6 @@
</dict> </dict>
</dict> </dict>
</dict> </dict>
<key>NSMicrophoneUsageDescription</key>
<string>$(PRODUCT_NAME) DOES NOT need microphone permission. Please reject this request.</string>
<key>NSCameraUsageDescription</key> <key>NSCameraUsageDescription</key>
<string>Allow $(PRODUCT_NAME) to capture photo or video and attach it to your toot</string> <string>Allow $(PRODUCT_NAME) to capture photo or video and attach it to your toot</string>
<key>NSLocationWhenInUseUsageDescription</key> <key>NSLocationWhenInUseUsageDescription</key>

View File

@ -1,8 +1,14 @@
import Button from '@components/Button' import Button from '@components/Button'
import { StyleConstants } from '@utils/styles/constants' import { StyleConstants } from '@utils/styles/constants'
import { Video } from 'expo-av' import { Video } from 'expo-av'
import React, { useCallback, useRef, useState } from 'react' import React, { useCallback, useEffect, useRef, useState } from 'react'
import { Pressable, StyleSheet, View } from 'react-native' import {
AppState,
AppStateStatus,
Pressable,
StyleSheet,
View
} from 'react-native'
import { Blurhash } from 'react-native-blurhash' import { Blurhash } from 'react-native-blurhash'
import attachmentAspectRatio from './aspectRatio' import attachmentAspectRatio from './aspectRatio'
import analytics from '@components/analytics' import analytics from '@components/analytics'
@ -45,15 +51,43 @@ const AttachmentVideo: React.FC<Props> = ({
videoPlayer.current?.setOnPlaybackStatusUpdate(props => { videoPlayer.current?.setOnPlaybackStatusUpdate(props => {
if (props.isLoaded) { if (props.isLoaded) {
setVideoLoaded(true) setVideoLoaded(true)
} if (props.positionMillis) {
// @ts-ignore setVideoPosition(props.positionMillis)
if (props.positionMillis) { }
// @ts-ignore
setVideoPosition(props.positionMillis)
} }
}) })
}, [videoLoaded, videoPosition]) }, [videoLoaded, videoPosition])
const appState = useRef(AppState.currentState)
useEffect(() => {
AppState.addEventListener('change', _handleAppStateChange)
return () => {
AppState.removeEventListener('change', _handleAppStateChange)
}
}, [])
const _handleAppStateChange = async (nextAppState: AppStateStatus) => {
if (appState.current.match(/active/) && nextAppState.match(/inactive/)) {
await videoPlayer.current?.pauseAsync()
} else if (
gifv &&
appState.current.match(/background/) &&
nextAppState.match(/active/)
) {
await videoPlayer.current?.setIsMutedAsync(true)
await videoPlayer.current?.playAsync()
}
appState.current = nextAppState
}
const playerStatus = useRef<any>(null)
useEffect(() => {
videoPlayer.current?.setOnPlaybackStatusUpdate(playbackStatus => {
playerStatus.current = playbackStatus
})
}, [])
return ( return (
<View <View
style={[ style={[
@ -83,16 +117,15 @@ const AttachmentVideo: React.FC<Props> = ({
posterStyle: { resizeMode: 'cover' } posterStyle: { resizeMode: 'cover' }
})} })}
useNativeControls={false} useNativeControls={false}
onFullscreenUpdate={event => { onFullscreenUpdate={async event => {
if ( if (
event.fullscreenUpdate === event.fullscreenUpdate ===
Video.FULLSCREEN_UPDATE_PLAYER_DID_DISMISS Video.FULLSCREEN_UPDATE_PLAYER_DID_DISMISS
) { ) {
if (gifv) { if (gifv) {
videoPlayer.current?.setIsLoopingAsync(true) await videoPlayer.current?.pauseAsync()
videoPlayer.current?.playAsync()
} else { } else {
videoPlayer.current?.pauseAsync() await videoPlayer.current?.pauseAsync()
} }
} }
}} }}
@ -108,7 +141,7 @@ const AttachmentVideo: React.FC<Props> = ({
}} }}
/> />
) : null ) : null
) : !gifv ? ( ) : !gifv || (gifv && playerStatus.current === false) ? (
<Button <Button
round round
overlay overlay

View File

@ -5,9 +5,8 @@ const audio = () => {
log('log', 'audio', 'setting audio playback default options') log('log', 'audio', 'setting audio playback default options')
Audio.setAudioModeAsync({ Audio.setAudioModeAsync({
playsInSilentModeIOS: true, playsInSilentModeIOS: true,
interruptionModeIOS: Audio.INTERRUPTION_MODE_IOS_DO_NOT_MIX, interruptionModeIOS: Audio.INTERRUPTION_MODE_IOS_DUCK_OTHERS,
interruptionModeAndroid: Audio.INTERRUPTION_MODE_ANDROID_DO_NOT_MIX, interruptionModeAndroid: Audio.INTERRUPTION_MODE_ANDROID_DUCK_OTHERS
shouldDuckAndroid: true
}) })
} }