質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
React Native

React Nativeは、ネイティブモバイルアプリ(iOS/Android)を作成できるJavaScriptフレームワークです。Reactと同じ設計のため、宣言的なコンポーネントでリッチなUIを開発することが可能です。

Q&A

解決済

1回答

1436閲覧

ExpoのVideo componentのrefの型を知りたいです

ap2c9w

総合スコア40

React Native

React Nativeは、ネイティブモバイルアプリ(iOS/Android)を作成できるJavaScriptフレームワークです。Reactと同じ設計のため、宣言的なコンポーネントでリッチなUIを開発することが可能です。

0グッド

0クリップ

投稿2022/05/30 02:55

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;

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

typescript

1import { View} from 'react-native'; 2const video = React.useRef<Video>(null);

で解決いたしました。

ExpoのVideoのマニュアルにFor more advanced examples, check out the Playlist example, and the custom VideoPlayer controls component that wraps <Video>, adds custom controls and use the <Video> API extensively. The VideoPlayer controls is used in this app.とありまして、custom VideoPlayer controls componentの先のソースを見たところ、

typescript

1let playbackInstance: Video | null = null

とありました。
そこで

typescript

1const video = React.useRef<Video>(null);

としたところ、エラーが消えました。

投稿2022/05/30 05:17

ap2c9w

総合スコア40

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問