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>
<key>NSMicrophoneUsageDescription</key>
<string>$(PRODUCT_NAME) DOES NOT need microphone permission. Please reject this request.</string>
<key>NSCameraUsageDescription</key>
<string>Allow $(PRODUCT_NAME) to capture photo or video and attach it to your toot</string>
<key>NSLocationWhenInUseUsageDescription</key>

View File

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

View File

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