播放和暫停影片
如何使用 video_player 外掛。
播放影片是應用開發中的常見任務,Flutter 應用也不例外。為了播放影片,Flutter 團隊提供了 video_player 外掛。你可以使用 video_player 外掛來播放儲存在檔案系統中的影片、資原始檔(asset)中的影片,或來自網際網路的影片。
在 iOS 上,video_player 外掛使用 AVPlayer 來處理播放。在 Android 上,它使用 ExoPlayer。
本示例演示瞭如何使用 video_player 軟體包透過以下步驟播放網際網路影片,並實現基本的播放和暫停控制:
- 新增
video_player依賴。 - 為你的應用新增許可權。
- 建立並初始化
VideoPlayerController。 - 顯示影片播放器。
- 播放和暫停影片。
1. 新增 video_player 依賴
#
此示例依賴於一個 Flutter 外掛:video_player。首先,將此依賴項新增到你的專案中。
要新增 video_player 軟體包作為依賴項,請執行 flutter pub add
flutter pub add video_player
2. 為你的應用新增許可權
#接下來,更新你的 android 和 ios 配置,以確保你的應用擁有從網際網路流式傳輸影片的正確許可權。
Android
#在 <application> 定義之後,將以下許可權新增到 AndroidManifest.xml 檔案中。AndroidManifest.xml 檔案位於 <專案根目錄>/android/app/src/main/AndroidManifest.xml。
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application ...>
</application>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
iOS
#對於 iOS,將以下內容新增到位於 <專案根目錄>/ios/Runner/Info.plist 的 Info.plist 檔案中。
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
macOS
#如果你使用基於網路的影片,請新增 com.apple.security.network.client 授權(entitlement)。
Web
#Flutter Web 不支援 dart:io,因此請避免在該外掛中使用 VideoPlayerController.file 建構函式。使用此建構函式會嘗試建立 VideoPlayerController.file,這會丟擲 UnimplementedError。
不同的 Web 瀏覽器可能具有不同的影片播放能力,例如支援的格式或自動播放功能。檢視 video_player_web 軟體包以獲取更多 Web 相關資訊。
VideoPlayerOptions.mixWithOthers 選專案前無法在 Web 上實現。如果你在 Web 上使用此選項,它將被靜默忽略。
3. 建立並初始化 VideoPlayerController
#
現在你已經安裝了 video_player 外掛並配置了正確的許可權,接下來建立一個 VideoPlayerController。VideoPlayerController 類允許你連線到不同型別的影片並控制播放。
在播放影片之前,你還必須 initialize(初始化)控制器。這會建立與影片的連線併為播放做好準備。
要建立並初始化 VideoPlayerController,請執行以下操作:
- 建立一個帶有配套
State類的StatefulWidget - 在
State類中新增一個變數來儲存VideoPlayerController - 在
State類中新增一個變數來儲存由VideoPlayerController.initialize返回的Future - 在
initState方法中建立並初始化控制器 - 在
dispose方法中銷燬控制器
class VideoPlayerScreen extends StatefulWidget {
const VideoPlayerScreen({super.key});
@override
State<VideoPlayerScreen> createState() => _VideoPlayerScreenState();
}
class _VideoPlayerScreenState extends State<VideoPlayerScreen> {
late VideoPlayerController _controller;
late Future<void> _initializeVideoPlayerFuture;
@override
void initState() {
super.initState();
// Create and store the VideoPlayerController. The VideoPlayerController
// offers several different constructors to play videos from assets, files,
// or the internet.
_controller = VideoPlayerController.networkUrl(
Uri.parse(
'https://flutter.github.io/assets-for-api-docs/assets/videos/butterfly.mp4',
),
);
_initializeVideoPlayerFuture = _controller.initialize();
}
@override
void dispose() {
// Ensure disposing of the VideoPlayerController to free up resources.
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
// Complete the code in the next step.
return Container();
}
}
4. 顯示影片播放器
#現在,顯示影片。video_player 外掛提供了 VideoPlayer 元件來顯示由 VideoPlayerController 初始化的影片。預設情況下,VideoPlayer 元件會盡可能佔用所有可用空間。這通常對影片來說並不理想,因為影片需要以特定的寬高比顯示,例如 16x9 或 4x3。
因此,將 VideoPlayer 元件包裹在 AspectRatio 元件中,以確保影片具有正確的比例。
此外,你必須在 _initializeVideoPlayerFuture() 完成後顯示 VideoPlayer 元件。使用 FutureBuilder 來顯示載入旋轉圖示,直到控制器初始化完成。注意:初始化控制器不會自動開始播放。
// Use a FutureBuilder to display a loading spinner while waiting for the
// VideoPlayerController to finish initializing.
FutureBuilder(
future: _initializeVideoPlayerFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
// If the VideoPlayerController has finished initialization, use
// the data it provides to limit the aspect ratio of the video.
return AspectRatio(
aspectRatio: _controller.value.aspectRatio,
// Use the VideoPlayer widget to display the video.
child: VideoPlayer(_controller),
);
} else {
// If the VideoPlayerController is still initializing, show a
// loading spinner.
return const Center(child: CircularProgressIndicator());
}
},
)
5. 播放和暫停影片
#預設情況下,影片以暫停狀態開始。要開始播放,請呼叫 VideoPlayerController 提供的 play() 方法。要暫停播放,請呼叫 pause() 方法。
在本示例中,嚮應用新增一個 FloatingActionButton,根據當前狀態顯示播放或暫停圖示。當用戶點選該按鈕時,如果影片當前已暫停,則播放影片;如果影片正在播放,則暫停影片。
FloatingActionButton(
onPressed: () {
// Wrap the play or pause in a call to `setState`. This ensures the
// correct icon is shown.
setState(() {
// If the video is playing, pause it.
if (_controller.value.isPlaying) {
_controller.pause();
} else {
// If the video is paused, play it.
_controller.play();
}
});
},
// Display the correct icon depending on the state of the player.
child: Icon(
_controller.value.isPlaying ? Icons.pause : Icons.play_arrow,
),
)
完整示例
#import 'dart:async';
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';
void main() => runApp(const VideoPlayerApp());
class VideoPlayerApp extends StatelessWidget {
const VideoPlayerApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'Video Player Demo',
home: VideoPlayerScreen(),
);
}
}
class VideoPlayerScreen extends StatefulWidget {
const VideoPlayerScreen({super.key});
@override
State<VideoPlayerScreen> createState() => _VideoPlayerScreenState();
}
class _VideoPlayerScreenState extends State<VideoPlayerScreen> {
late VideoPlayerController _controller;
late Future<void> _initializeVideoPlayerFuture;
@override
void initState() {
super.initState();
// Create and store the VideoPlayerController. The VideoPlayerController
// offers several different constructors to play videos from assets, files,
// or the internet.
_controller = VideoPlayerController.networkUrl(
Uri.parse(
'https://flutter.github.io/assets-for-api-docs/assets/videos/butterfly.mp4',
),
);
// Initialize the controller and store the Future for later use.
_initializeVideoPlayerFuture = _controller.initialize();
// Use the controller to loop the video.
_controller.setLooping(true);
}
@override
void dispose() {
// Ensure disposing of the VideoPlayerController to free up resources.
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Butterfly Video')),
// Use a FutureBuilder to display a loading spinner while waiting for the
// VideoPlayerController to finish initializing.
body: FutureBuilder(
future: _initializeVideoPlayerFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
// If the VideoPlayerController has finished initialization, use
// the data it provides to limit the aspect ratio of the video.
return AspectRatio(
aspectRatio: _controller.value.aspectRatio,
// Use the VideoPlayer widget to display the video.
child: VideoPlayer(_controller),
);
} else {
// If the VideoPlayerController is still initializing, show a
// loading spinner.
return const Center(child: CircularProgressIndicator());
}
},
),
floatingActionButton: FloatingActionButton(
onPressed: () {
// Wrap the play or pause in a call to `setState`. This ensures the
// correct icon is shown.
setState(() {
// If the video is playing, pause it.
if (_controller.value.isPlaying) {
_controller.pause();
} else {
// If the video is paused, play it.
_controller.play();
}
});
},
// Display the correct icon depending on the state of the player.
child: Icon(
_controller.value.isPlaying ? Icons.pause : Icons.play_arrow,
),
),
);
}
}