-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
re #4
- Loading branch information
Showing
11 changed files
with
159 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
1.7 | ||
1.8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { GoogleLanguage } from '@vocably/model'; | ||
import React, { FC, useCallback, useEffect, useState } from 'react'; | ||
import { Alert, Pressable, StyleProp, ViewStyle } from 'react-native'; | ||
import { useTheme } from 'react-native-paper'; | ||
import Sound from 'react-native-sound'; | ||
import Icon from 'react-native-vector-icons/MaterialCommunityIcons'; | ||
|
||
type PlaySound = FC<{ | ||
text: string; | ||
language: GoogleLanguage; | ||
size?: number; | ||
style?: StyleProp<ViewStyle>; | ||
}>; | ||
|
||
export const PlaySound: PlaySound = ({ | ||
text, | ||
language, | ||
size = 16, | ||
style = {}, | ||
}) => { | ||
const theme = useTheme(); | ||
|
||
const [isPlaying, setIsPlaying] = useState(false); | ||
const [loadedAudio, setLoadedAudio] = useState<Sound | null>(null); | ||
|
||
const playSound = useCallback(() => { | ||
const audio = | ||
loadedAudio ?? | ||
new Sound( | ||
`https://translate.google.com/translate_tts?ie=UTF-8&q=${text}&tl=${language}&client=tw-ob`, | ||
'', | ||
(error) => { | ||
if (error === null) { | ||
setLoadedAudio(audio); | ||
} | ||
if (error) { | ||
setIsPlaying(false); | ||
Alert.alert( | ||
'Error: The sound could not be played', | ||
`Something went wrong during the sound playback. The sound playback is a new feature, and it might have problems. Could you please try to play the sound one more time?` | ||
); | ||
} | ||
} | ||
); | ||
|
||
setIsPlaying(true); | ||
}, [text, language, setIsPlaying, setLoadedAudio, loadedAudio]); | ||
|
||
useEffect(() => { | ||
if (isPlaying && loadedAudio) { | ||
loadedAudio.play(() => { | ||
setIsPlaying(false); | ||
}); | ||
} | ||
}, [isPlaying, loadedAudio, setIsPlaying]); | ||
|
||
return ( | ||
<Pressable | ||
disabled={isPlaying} | ||
style={({ pressed }) => [ | ||
{ | ||
opacity: pressed ? 0.25 : 0.5, | ||
}, | ||
style, | ||
]} | ||
onPress={playSound} | ||
> | ||
<Icon | ||
name={isPlaying ? 'volume-medium' : 'play-circle'} | ||
style={{ | ||
color: theme.colors.onBackground, | ||
}} | ||
size={size} | ||
/> | ||
</Pressable> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters