import React, { useState, useEffect, useRef } from 'react'; import { Play, Pause, RotateCcw, Volume2, VolumeX, Cpu, Brain, Zap, Smartphone, Globe, Sparkles, X, MessageSquare, HelpCircle, Loader2 } from 'lucide-react'; const InteractiveVideoLectureMobile = () => { // --- 原有 State --- const [isPlaying, setIsPlaying] = useState(false); const [currentSlide, setCurrentSlide] = useState(0); const [progress, setProgress] = useState(0); const [audioSupported, setAudioSupported] = useState(true); const [hasStarted, setHasStarted] = useState(false); const [voice, setVoice] = useState(null); // --- 新增 AI State --- const [showAIModal, setShowAIModal] = useState(false); const [aiResponse, setAiResponse] = useState(null); const [isAiLoading, setIsAiLoading] = useState(false); const [aiMode, setAiMode] = useState(null); // 'explain' or 'quiz' const synth = useRef(null); const progressInterval = useRef(null); // Gemini API Key (Runtime Environment will provide the key if empty) const apiKey = ""; const slides = [ { id: 0, title: "Google AI 2025", subtitle: "新架構・新思維・新作為", script: "歡迎收看 Google AI 2025 年度總覽。今年,Google 用「新架構、新思維、新作為」三大支柱,重新定義了人工智慧的未來。", duration: 12000, color: "bg-blue-900", gradient: "bg-gradient-to-br from-blue-900 to-slate-900", icon: }, { id: 1, title: "新架構 Architecture", subtitle: "TPU Trillium & Gemini 3", script: "首先是新架構。隨著第六代 TPU Trillium 晶片的發布,配合 Gemini 3 的混合專家模型,Google 成功將 AI 的運算效率提升了近五倍。", duration: 14000, color: "bg-indigo-900", gradient: "bg-gradient-to-br from-indigo-900 to-purple-900", icon: }, { id: 2, title: "新思維 New Thinking", subtitle: "Agentic AI & Deep Think", script: "再來是新思維。AI 不再只是被動問答,而是能主動解決問題的「代理人」。Gemini 引入的「深度思考模式」,讓電腦學會了像人類一樣深思熟慮。", duration: 14000, color: "bg-emerald-900", gradient: "bg-gradient-to-br from-emerald-900 to-teal-900", icon: }, { id: 3, title: "產品生態 Ecosystem", subtitle: "Pixel 10 & NotebookLM", script: "最後是產品生態。從 Pixel 10 手機實現「斷網也能用的 AI」,到 NotebookLM 將文件瞬間轉化為 Podcast,Google 正在全方位賦能您的生活。", duration: 14000, color: "bg-amber-900", gradient: "bg-gradient-to-br from-amber-900 to-orange-900", icon: }, { id: 4, title: "總結 Summary", subtitle: "The Future is Agentic", script: "總結來說,2025 年是 AI 從「對話」走向「行動」的關鍵年。掌握未來,從現在開始。謝謝收看。", duration: 10000, color: "bg-gray-900", gradient: "bg-gradient-to-br from-gray-900 to-black", icon: } ]; // 初始化檢測 useEffect(() => { if (typeof window !== 'undefined' && 'speechSynthesis' in window) { synth.current = window.speechSynthesis; const loadVoices = () => { const voices = synth.current.getVoices(); const zhVoice = voices.find(v => v.lang.includes('zh') || v.lang.includes('cmn')); if (zhVoice) setVoice(zhVoice); }; loadVoices(); if (synth.current.onvoiceschanged !== undefined) { synth.current.onvoiceschanged = loadVoices; } } else { setAudioSupported(false); } return () => { if (synth.current) synth.current.cancel(); if (progressInterval.current) clearInterval(progressInterval.current); }; }, []); // 播放邏輯 useEffect(() => { if (!hasStarted) return; // 如果開啟 AI Modal,暫停播放 if (showAIModal) { if (synth.current) synth.current.pause(); if (progressInterval.current) clearInterval(progressInterval.current); return; } else { if (synth.current && isPlaying) synth.current.resume(); } if (isPlaying) { if (audioSupported && synth.current && !synth.current.speaking) { synth.current.cancel(); const utterance = new SpeechSynthesisUtterance(slides[currentSlide].script); if (voice) utterance.voice = voice; utterance.rate = 1.0; synth.current.speak(utterance); } const startTime = Date.now() - (progress / 100 * slides[currentSlide].duration); const duration = slides[currentSlide].duration; if (progressInterval.current) clearInterval(progressInterval.current); progressInterval.current = setInterval(() => { const elapsed = Date.now() - startTime; const newProgress = Math.min((elapsed / duration) * 100, 100); setProgress(newProgress); if (elapsed > duration) { if (currentSlide < slides.length - 1) { setCurrentSlide(prev => prev + 1); setProgress(0); } else { setIsPlaying(false); clearInterval(progressInterval.current); if (audioSupported && synth.current) synth.current.cancel(); } } }, 100); } else { if (audioSupported && synth.current) synth.current.cancel(); if (progressInterval.current) clearInterval(progressInterval.current); } }, [isPlaying, currentSlide, hasStarted, audioSupported, voice, showAIModal]); const handleStart = () => { setHasStarted(true); setIsPlaying(true); if (audioSupported && synth.current) { const dummy = new SpeechSynthesisUtterance(""); synth.current.speak(dummy); } }; const handlePlayPause = () => { if (isPlaying) { setIsPlaying(false); } else { if (currentSlide === slides.length - 1 && progress >= 95) { setCurrentSlide(0); setProgress(0); } setIsPlaying(true); } }; const handleManualSlide = (index) => { setIsPlaying(false); if (audioSupported && synth.current) synth.current.cancel(); setCurrentSlide(index); setProgress(0); setTimeout(() => setIsPlaying(true), 100); }; // --- Gemini API Call Function --- const callGemini = async (mode) => { setAiMode(mode); setIsAiLoading(true); setAiResponse(null); // 暫停播放 setIsPlaying(false); if (synth.current) synth.current.cancel(); const slideContext = ` Title: ${slides[currentSlide].title} Subtitle: ${slides[currentSlide].subtitle} Script: ${slides[currentSlide].script} `; let prompt = ""; if (mode === 'explain') { prompt = ` You are an expert AI Tutor explaining a lecture about Google AI 2025. Please provide a concise but deeper explanation (3 bullet points) about the following slide content. Focus on the technical or strategic significance. Respond in Traditional Chinese (繁體中文). Slide Content: ${slideContext} `; } else if (mode === 'quiz') { prompt = ` You are an expert AI Tutor. Generate ONE multiple-choice quiz question based on the following slide content. Provide the question, 4 options (A, B, C, D), and then the correct answer with a short explanation. Format cleanly. Respond in Traditional Chinese (繁體中文). Slide Content: ${slideContext} `; } try { const response = await fetch( `https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-preview-09-2025:generateContent?key=${apiKey}`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ contents: [{ parts: [{ text: prompt }] }], }), } ); const data = await response.json(); if (data.error) { throw new Error(data.error.message); } const text = data.candidates?.[0]?.content?.parts?.[0]?.text; setAiResponse(text || "無法產生內容,請稍後再試。"); } catch (error) { console.error("Gemini API Error:", error); setAiResponse("連線錯誤,請檢查網路或稍後再試。"); } finally { setIsAiLoading(false); } }; if (!hasStarted) { return (

Google AI 2025 教材

{audioSupported ? "即將開始播放互動式簡報。請開啟您的手機音量。" : "偵測到您的瀏覽器不支援語音,將以「字幕模式」播放。"}

); } return (
{/* 1. 播放器主畫面 */}
{/* 背景層 */}
{/* 內容層 */}
{slides[currentSlide].icon}

{slides[currentSlide].title}

{slides[currentSlide].subtitle}

{slides[currentSlide].script}

{/* AI 按鈕 (浮動於畫面右下) */}
{/* 控制列 */}
{audioSupported ? : } {audioSupported ? "語音開啟" : "靜音模式"}
{/* 2. 下方分鏡選單 */}
{slides.map((slide, index) => ( ))}

{audioSupported ? "提示:若無聲音,請檢查手機靜音開關。" : "提示:您的瀏覽器暫不支援 Web Speech,已切換至字幕模式。"}

{/* --- AI 助教 Modal --- */} {showAIModal && (
{/* Header */}

AI 隨身助教 (Gemini)

{/* Content Area */}
{!aiResponse && !isAiLoading && (

針對 「{slides[currentSlide].title}」,您想做什麼?

)} {isAiLoading && (

Gemini 正在思考中...

)} {aiResponse && (
{aiMode === 'explain' ? : } {aiMode === 'explain' ? "深度解析" : "隨堂測驗"}
{aiResponse}
)}
Powered by Google Gemini 2.5 Flash
)}
); }; export default InteractiveVideoLectureMobile;