Advertisement
Guest User

Untitled

a guest
Dec 16th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.63 KB | None | 0 0
  1. import React, { ReactElement } from 'react'
  2. import styles from './styles'
  3. import { View } from 'react-native'
  4. import PlayerController from '../player_controller/PlayerController'
  5. import i18n from 'i18n-js'
  6. import PlayerProgress from '../player_progress/PlayerProgress'
  7. import Button from '../../../../components/button/Button'
  8. import {
  9. add,
  10. destroyThrottle,
  11. getPosition,
  12. pauseThrottle,
  13. playThrottle,
  14. resetThrottle,
  15. seekToThrottle,
  16. setupPlayer,
  17. stopThrottle,
  18. updateOptions,
  19. } from '../../../../service/TrackPlayerService'
  20. import { Capability, Track } from 'react-native-track-player'
  21. import { testIDs } from '../../../../testIDs'
  22. import VolumeSlider from '../volume_slider/VolumeSlider'
  23.  
  24. interface Props {
  25. componentId?: string
  26. track: Track
  27. onContinueTrainingPress?: () => void
  28. onPlayPress?: () => void
  29. }
  30.  
  31. interface State {
  32. playerState: 'playing' | 'pause'
  33. }
  34.  
  35. const initialState: State = {
  36. playerState: 'pause',
  37. }
  38. const defaultProps: Props = {
  39. track: {
  40. id: '',
  41. url: '',
  42. title: '',
  43. artist: '',
  44. },
  45. }
  46.  
  47. const SEEK_TIME_SEC: number = 15
  48.  
  49. class Player extends React.Component<Props, State> {
  50. readonly state: State = initialState
  51. static defaultProps: Props = defaultProps
  52.  
  53. async componentDidMount(): Promise<void> {
  54. const { track } = this.props
  55. try {
  56. await setupPlayer()
  57. await updateOptions({
  58. capabilities: [
  59. Capability.Pause,
  60. Capability.Play,
  61. Capability.Stop,
  62. Capability.SeekTo,
  63. ],
  64. compactCapabilities: [Capability.Pause, Capability.Play],
  65. notificationCapabilities: [],
  66. })
  67. await add(track)
  68. } catch (e) {
  69. console.log('err:: ', e.message)
  70. }
  71. }
  72.  
  73. async componentWillUnmount(): Promise<void> {
  74. try {
  75. await stopThrottle['100']()
  76. destroyThrottle['100']()
  77. } catch (e) {
  78. console.log('err:: ', e.message)
  79. }
  80. }
  81.  
  82. render(): ReactElement<any> {
  83. const { onContinueTrainingPress, track } = this.props
  84. const { duration } = track
  85. return (
  86. <View>
  87. <PlayerController
  88. playMode={this.showPlay() ? 'pause' : 'play'}
  89. onSeekFurtherPress={this.handleSeekFurtherPress}
  90. onPlayPress={this.handlePlayPress}
  91. onRepeatPress={this.handleRepeatPress}
  92. onSeekBackPress={this.handleSeekBackPress}
  93. disableSeek={this.getDisable()}
  94. />
  95. <View style={styles.bottomContainer}>
  96. <PlayerProgress
  97. track={track}
  98. disabled={this.getDisable()}
  99. duration={duration}
  100. />
  101. <View style={styles.divider} />
  102. <VolumeSlider />
  103. <View style={styles.divider} />
  104. <View style={styles.buttonContainer}>
  105. <Button
  106. text={i18n.t('Continue training')}
  107. onPress={onContinueTrainingPress}
  108. active={!this.showPlay()}
  109. testID={testIDs.listenToEpisode.continueTraining}
  110. />
  111. </View>
  112. </View>
  113. </View>
  114. )
  115. }
  116.  
  117. showPlay = (): boolean => {
  118. const { playerState } = this.state
  119. return playerState === 'playing'
  120. }
  121.  
  122. getDisable = (): boolean => {
  123. const { playerState } = this.state
  124. return playerState === 'pause'
  125. }
  126.  
  127. handlePlayPress = async (): Promise<void> => {
  128. const { onPlayPress } = this.props
  129. onPlayPress && onPlayPress()
  130. try {
  131. const { playerState } = this.state
  132. if (playerState === 'playing') {
  133. await pauseThrottle['500']()
  134. this.setState({
  135. playerState: 'pause',
  136. })
  137. } else {
  138. await playThrottle['500']()
  139. this.setState({
  140. playerState: 'playing',
  141. })
  142. }
  143. } catch (e) {
  144. console.log('err:: ', e)
  145. }
  146. }
  147.  
  148. handleRepeatPress = async (): Promise<void> => {
  149. const { track } = this.props
  150. try {
  151. await stopThrottle['2000']()
  152. await resetThrottle['2000']()
  153. await add(track)
  154. await playThrottle['2000']()
  155. this.setState({
  156. playerState: 'playing',
  157. })
  158. } catch (e) {
  159. console.log('err:: ', e)
  160. }
  161. }
  162.  
  163. handleSeekBackPress = async (): Promise<any> => {
  164. try {
  165. const position: number = await getPosition()
  166. await seekToThrottle['1000'](position - SEEK_TIME_SEC)
  167. } catch (e) {
  168. console.log('err:: ', e)
  169. }
  170. }
  171.  
  172. handleSeekFurtherPress = async (): Promise<any> => {
  173. try {
  174. const {
  175. track: { duration },
  176. } = this.props
  177. const position: number = await getPosition()
  178. await seekToThrottle['1000'](
  179. position + SEEK_TIME_SEC > duration ? duration : position + SEEK_TIME_SEC,
  180. )
  181. } catch (e) {
  182. console.log('err:: ', e)
  183. }
  184. }
  185. }
  186.  
  187. export default Player
  188.  
  189. "react-native-track-player": "2.0.0-rc13"
  190.  
  191. import React, { ReactElement, useEffect, useState } from 'react'
  192. import styles from './styles'
  193. import { Platform, Text, View } from 'react-native'
  194. import { colors } from '../../../../assets/colors'
  195. import Slider from '@react-native-community/slider'
  196. import {
  197. getSoundDuration,
  198. normalizeInternalProgress,
  199. normalizeProgress,
  200. } from '../../../../utils/utils'
  201. import { Event, Track, useProgress, useTrackPlayerEvents } from 'react-native-track-player'
  202. import {
  203. add,
  204. playThrottle,
  205. resetThrottle,
  206. seekToThrottle,
  207. setupPlayer,
  208. stopThrottle,
  209. } from '../../../../service/TrackPlayerService'
  210. import { testIDs } from '../../../../testIDs'
  211.  
  212. interface Props {
  213. duration: number
  214. disabled: boolean
  215. track: Track
  216. }
  217. const defaultProps: Props = {
  218. duration: 0,
  219. disabled: true,
  220. track: {
  221. id: '',
  222. url: '',
  223. title: '',
  224. artist: '',
  225. },
  226. }
  227.  
  228. let timeoutId: NodeJS.Timeout = null
  229.  
  230. const PlayerProgress = ({ duration, disabled, track }: Props): ReactElement<any> => {
  231. const { position } = useProgress(500)
  232. Platform.OS === 'ios' &&
  233. useTrackPlayerEvents([Event.PlaybackQueueEnded], async () => {
  234. await stopThrottle['2000']()
  235. await resetThrottle['2000']()
  236. await add(track)
  237. await playThrottle['2000']()
  238. })
  239. const [internalProgress, setInternalProgress] = useState<number>(0)
  240. const [isSliding, setIsSliding] = useState<boolean>(false)
  241. useEffect(() => {
  242. const setup = async (): Promise<any> => {
  243. try {
  244. await setupPlayer()
  245. } catch (e) {
  246. console.log('e:: ', e)
  247. }
  248. }
  249. setup().catch()
  250. }, [])
  251. const handleSlidingComplete = async (value: number): Promise<any> => {
  252. try {
  253. await seekToThrottle['0'](normalizeInternalProgress(duration, value))
  254. } catch (e) {
  255. console.log('e:: ', e)
  256. }
  257. clearTimeout(timeoutId)
  258. timeoutId = setTimeout(() => setIsSliding(false), 2000)
  259. }
  260. const handleSlidingStart = (): void => setIsSliding(true)
  261. const handleValueChange = (value: number): void => setInternalProgress(value)
  262.  
  263. return (
  264. <View style={styles.container}>
  265. <Slider
  266. disabled={disabled}
  267. value={isSliding ? internalProgress : normalizeProgress(duration, position)}
  268. minimumTrackTintColor={colors.mainBrown}
  269. maximumTrackTintColor={colors.peach_5}
  270. thumbTintColor={colors.mainBrown}
  271. onSlidingComplete={handleSlidingComplete}
  272. onSlidingStart={handleSlidingStart}
  273. onValueChange={handleValueChange}
  274. testID={testIDs.listenToEpisode.progress}
  275. />
  276. <View style={styles.durationContainer}>
  277. <Text style={styles.duration}>{getSoundDuration(position, duration)}</Text>
  278. <Text style={styles.duration}>
  279. {getSoundDuration(duration - position, duration)}
  280. </Text>
  281. </View>
  282. </View>
  283. )
  284. }
  285. PlayerProgress.defaultProps = defaultProps
  286.  
  287. export default PlayerProgress
  288.  
  289. import React, { ReactElement, useEffect, useState } from 'react'
  290. import styles from './styles'
  291. import { Text, View } from 'react-native'
  292. import i18n from 'i18n-js'
  293. import Slider from '@react-native-community/slider'
  294. import { testIDs } from '../../../../testIDs'
  295. import { colors } from '../../../../assets/colors'
  296. // @ts-ignore
  297. import SystemSetting from 'react-native-system-setting'
  298.  
  299. interface Props {}
  300. const defaultProps: Props = {}
  301.  
  302. const VolumeSlider = ({ }: Props): ReactElement<any> => {
  303. const [volume, setVolume] = useState<number>(0)
  304. const listener = SystemSetting.addVolumeListener(({ value }: { value: number }) =>
  305. setVolume(value),
  306. )
  307. useEffect(() => {
  308. const setDeviceVolume = async (): Promise<any> => {
  309. const deviceVolume: number = await SystemSetting.getVolume('music')
  310. setVolume(deviceVolume)
  311. }
  312. setDeviceVolume().catch()
  313. })
  314. useEffect(() => SystemSetting.removeVolumeListener(listener), [])
  315. const handleVolumeValueChange = (volume: number): void =>
  316. SystemSetting.setVolume(volume, {
  317. type: 'music',
  318. })
  319.  
  320. return (
  321. <View style={styles.volumeContainer}>
  322. <Text style={styles.volume}>{i18n.t('Volume')}</Text>
  323. <Slider
  324. testID={testIDs.listenToEpisode.volume}
  325. value={volume}
  326. minimumTrackTintColor={colors.mainBrown}
  327. maximumTrackTintColor={colors.peach_5}
  328. thumbTintColor={colors.mainBrown}
  329. onSlidingComplete={handleVolumeValueChange}
  330. style={styles.volumeSlider}
  331. />
  332. </View>
  333. )
  334. }
  335. VolumeSlider.defaultProps = defaultProps
  336.  
  337. export default VolumeSlider
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement