8
Flutter(十) 音频+视频播放 - 柳云居士
source link: https://www.cnblogs.com/anywherego/p/17376509.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
在Flutter中,我们有各种插件可供使用,从而实现音频和视频的播放功能。
例如,可以使用“text_to_speech”插件来将文字转换为语音,使用内置的“video_player”插件轻松地实现视频播放,或者使用“audioplayers”插件实现音频播放。
对于仅需要简单播放器功能的情况,也可以使用第三方插件“chewie”来实现。
一、文字转语音 - text_to_speech
text_to_speech官网
pubspec.yaml
dependencies:
text_to_speech: ^0.2.3
TextToSpeech tts = TextToSpeech();
tts.setRate(0.3); // 语速
tts.setPitch(0.5); // 语调
tts.setLanguage('en-US'); // 语言
tts.speak('This is test'); // 播放文字
Android配置
安卓还需要在App的AndroidManifest.xml
中添加<queries>
:
<manifest>
<application>
...
</application>
<queries>
<intent>
<action android:name="android.intent.action.TTS_SERVICE" />
</intent>
</queries>
</manifest>
二、播放音频 - audioplayers
audioplayers官网
pubspec.yaml
dependencies:
audioplayers: ^4.0.1
class _ExampleAppState extends State<ExampleApp> {
final player = AudioPlayer();
...
void play() {
player.play(AssetSource('audio/test.mp3'));
}
...
}
三、播放视频 - video_player & chewie
播放视频需要使用到官方提供的video_player和第三方播放器chewie
video_player官网
chewie官网
pubspec.yaml
dependencies:
video_player: ^2.6.1
chewie: ^1.4.0
class _ExampleAppState extends State<ExampleApp> {
late VideoPlayerController videoPlayerController;
late ChewieController cheController;
@override
void initState() {
// 资源控制器
videoPlayerController = VideoPlayerController.asset('assets/video/test.mp4');
// 视图控制器
cheController = ChewieController(
videoPlayerController: videoPlayerController,
aspectRatio: 16 / 9,
autoInitialize: true,
autoPlay: false,
looping: false,
);
super.initState();
}
...
// 播放器展示
Container(
height: 200,
child: Chewie(
controller: cheController,
),
),
...
}
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK