直接用 MediaPlayer.setDataSource("https://www.youtube.com/watch?v=...") 會失敗,因 YouTube 網頁 URL 返回 HTML 而非視頻流。解決方案:提取真實流媒體 URL(複雜、易失效)或喚起 YouTube App/瀏覽器播放。
String url = "https://www.youtube.com/watch?v=SgGhtjKWLOE";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent); // 系統選擇 YouTube App 或瀏覽器播放
// 僅支持直接視頻流 URL(如 .mp4, .m3u8)
String directUrl = "https://example.com/video.mp4";
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(directUrl);
mp.prepareAsync(); // 異步準備
mp.setOnPreparedListener(MediaPlayer::start);
不折騰直鏈,官方 API 或喚起 App 才是正道。