ExpoのVideo componentのUsageをTypeScriptで書き直しました。
最下段に記載しましたのソースの
typescript
1video.current.pauseAsync(); 2video.current.playAsync();
の部分で、2つのESlintによるエラーがでます。
エラーは以下のとおりです。
- プロパティ 'pauseAsync' は型 'never' に存在しません。ts(2339)
- Unsafe call of an
any
typed value.eslint@typescript-eslint/no-unsafe-call
おそらく、
const video = React.useRef(null);
のuseRefに型を設定すればよいと思ったのですが、どんな型を設定すればよいかわかりませんでした。
Webの場合はuseRef<HTMLVideoElement>(null)としておりました。
typescript
1import * as React from 'react'; 2import { View, StyleSheet, Button } from 'react-native'; 3import { Video, AVPlaybackStatus, ResizeMode } from 'expo-av'; 4const VideoFrame = () => { 5 const video = React.useRef(null); 6 const [status, setStatus] = React.useState<AVPlaybackStatus>(); 7 const handlePress = () => { 8 if (video.current == null) { 9 return; 10 } 11 if (status?.isLoaded) { 12 if (status.isPlaying) { 13 // eslint-disable-next-line @typescript-eslint/no-unsafe-call 14 video.current.pauseAsync(); 15 } else { 16 video.current.playAsync(); 17 } 18 } 19 // status?.isLoaded && 20 // ? video.current.pauseAsync() 21 // : video.current.playAsync(); 22 }; 23 return ( 24 <View style={styles.container}> 25 <Video 26 ref={video} 27 style={styles.video} 28 source={{ 29 uri: 'https://d23dyxeqlo5psv.cloudfront.net/big_buck_bunny.mp4', 30 }} 31 useNativeControls 32 resizeMode={ResizeMode.CONTAIN} 33 // resizeMode="contain" 34 isLooping 35 onPlaybackStatusUpdate={(videoStatus) => setStatus(() => videoStatus)} 36 /> 37 <View style={styles.buttons}> 38 <Button 39 title={status?.isLoaded && status.isPlaying ? 'Pause' : 'Play'} 40 onPress={handlePress} 41 /> 42 </View> 43 </View> 44 ); 45}; 46const styles = StyleSheet.create({ 47 container: { 48 flex: 1, 49 justifyContent: 'center', 50 backgroundColor: '#ecf0f1', 51 }, 52 video: { 53 alignSelf: 'center', 54 width: 320, 55 height: 200, 56 }, 57 buttons: { 58 flexDirection: 'row', 59 justifyContent: 'center', 60 alignItems: 'center', 61 }, 62}); 63export default VideoFrame;
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。