0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ChatGPTを使用したアプリ開発記【動画投稿機能の実装】

Last updated at Posted at 2025-05-04

🔧 ① 動画用のステートを追加

App.js
const [video, setVideo] = useState(null);

📝 ② フォームに動画アップロード欄を追加

App.js
<input type="file" accept="video/*" onChange={(e) => setVideo(e.target.files[0])} />
部分 解説
type="file" ファイル選択用の入力フォーム(画像・動画など)を表示します。
accept="video/*" ユーザーが選べるファイルの種類を動画ファイル(例: mp4, webm, mov)に制限します。* はすべての動画形式を許可するワイルドカードです。
onChange={(e) => setVideo(e.target.files[0])} ファイルが選択されたときに呼び出されるイベントハンドラです。 e.target.files は選ばれたファイルのリストで、.files[0] はその中の最初の1つ(1ファイル目)を取得します。それを setVideo() に渡して、React の video 状態に保存します。

🚀 ③ handleSubmit 関数を修正

App.js
 let videoUrl = null;
  if (video) {
    const videoRef = ref(storage, `videos/${video.name}_${Date.now()}`);
    const snapshot = await uploadBytes(videoRef, video);
    videoUrl = await getDownloadURL(snapshot.ref);
  }
    const newPost = {
    text,
    time: formattedTime,
    likes: 0,
    user: user?.email,
    imageUrl,
    videoUrl,
  };

  setPosts([newPost, ...posts]);
  setText('');
  setImage(null);
  setVideo(null);
  };

🖼️ ④ 投稿一覧で動画を表示する

App.js
{post.videoUrl && (
  <video controls style={{ maxWidth: '100%' }}>
    <source src={post.videoUrl} type="video/mp4" />
    お使いのブラウザは video タグをサポートしていません
  </video>
)}

スクリーンショット 2025-05-05 8.09.44.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?