Flutter v1,android音视频框架
Flutter提供了一个名为video\_player的插件,可以用来播放视频。对于音频,Flutter同样提供了一个名为audio_player
的插件。
以下是使用video_player
和audio_player
插件的示例代码:
首先,在pubspec.yaml
文件中添加依赖:
dependencies:
flutter:
sdk: flutter
video_player:
audio_player:
然后,你可以使用以下代码来播放视频和音频:
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';
import 'package:audio_player/audio_player.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: VideoAudioPage(),
);
}
}
class VideoAudioPage extends StatefulWidget {
@override
_VideoAudioPageState createState() => _VideoAudioPageState();
}
class _VideoAudioPageState extends State<VideoAudioPage> {
VideoPlayerController _videoPlayerController;
AudioPlayer _audioPlayer;
@override
void initState() {
super.initState();
_videoPlayerController = VideoPlayerController.network(
'https://flutter.github.io/assets-for-api-docs/assets/videos/butterfly.mp4')
..initialize().then((_) {
// Ensure the first frame is shown after the video is initialized, even before the play button has been pressed.
setState(() {});
});
_audioPlayer = AudioPlayer();
if (null != _videoPlayerController.value.initialized) {
_audioPlayer.setUrl(_videoPlayerController.dataSource);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
评论已关闭