如何在 Flutter 中播放自定义声音?
How to play a custom sound in Flutter?
我可以用这行代码播放简单的声音:
SystemSound.play(SystemSoundType.click);
如何播放自定义声音?
让我们说一个简短的 mp3
感谢您查看 Flutter!
目前(截至 2017 年 5 月 5 日)的 Flutter SDK 没有内置支持播放和控制任意音频。然而,我们设计了我们的插件系统来支持它。
此插件为 Flutter 添加了音频支持:https://pub.dartlang.org/packages/audioplayer
来自插件的自述文件:
Future play() async {
final result = await audioPlayer.play(kUrl);
if (result == 1) setState(() => playerState = PlayerState.playing);
}
// add a isLocal parameter to play a local file
Future playLocal() async {
final result = await audioPlayer.play(kUrl);
if (result == 1) setState(() => playerState = PlayerState.playing);
}
Future pause() async {
final result = await audioPlayer.pause();
if (result == 1) setState(() => playerState = PlayerState.paused);
}
Future stop() async {
final result = await audioPlayer.stop();
if (result == 1) {
setState(() {
playerState = PlayerState.stopped;
position = new Duration();
});
}
}
播放已在资产中定义的文件的简单解决方案是使用 AudioCache。
图书馆:https://pub.dartlang.org/packages/audioplayers。
More about AudioCache
将库添加到 pubspec.yaml
后,需要导入 class:
import 'package:audioplayers/audio_cache.dart';
在同一个文件中添加一个资产,并将带有声音的文件放入资产文件夹(如果没有此文件夹,请创建它)
assets:
- assets/sound_alarm.mp3
然后添加此代码:
static AudioCache player = new AudioCache();
const alarmAudioPath = "sound_alarm.mp3";
player.play(alarmAudioPath);
一个例子here
[答案已更新:此方法无效,请参阅评论]
您可以使用 Flutter 团队维护的 video_player 插件。它可以跨平台复制多种媒体,包括声音文件。更具体地说,您可能想要使用 VideoPlayerController class.
例如。
_controller = VideoPlayerController.network('<a href="https://www.example.com/soundsFile.wav" rel="nofollow noreferrer">https://www.example.com/soundsFile.wav</a>');
_controller.play();
</pre>
音频播放器有效(来自 https://medium.com/@bennett4/adding-custom-sound-effects-to-a-flutter-mobile-app-41594f1f3305):
(1) 将库添加到您的 pubspec.yaml:audioplayers: ^0.15.1
(2) 在 flutter
下的 pubspec.yaml 中添加对资产文件的引用:
flutter
assets:
- assets/yes.mp3
确保它在资产文件夹下。它在子文件夹中时不起作用。例如,类似:- assets/sounds/yes.mp3 将不起作用。只需将您的音频文件放在 assets 文件夹中,而不是其子文件夹中
(3) 在您的应用中将库导入为:import package:audioplayers/audioplayers.dart;
(4) 然后定义这个函数:
Future<AudioPlayer> playLocalAsset() async {
AudioCache cache = new AudioCache();
//At the next line, DO NOT pass the entire reference such as assets/yes.mp3. This will not work.
//Just pass the file name only.
return await cache.play("yes.mp3");
}
(5) 每当需要播放声音时调用函数:await playLocalAsset();
空安全代码:
将依赖项添加到您的 pubspec.yaml
文件,
dependencies:
audioplayers: ^0.19.0
将音频文件路径添加到您的 pubspec.yaml
文件。
flutter:
assets:
- assets/audio/my_audio.mp3
运行 flutter pub get
完整代码:
class HomePage extends StatelessWidget {
final AudioCache _audioCache = AudioCache(
prefix: 'audio/',
fixedPlayer: AudioPlayer()..setReleaseMode(ReleaseMode.STOP),
);
@override
Widget build(BuildContext context) {
return Scaffold(
body: ElevatedButton(
onPressed: () => _audioCache.play('my_audio.mp3'),
child: Text('Play Audio'),
),
);
}
}
我可以用这行代码播放简单的声音:
SystemSound.play(SystemSoundType.click);
如何播放自定义声音?
让我们说一个简短的 mp3
感谢您查看 Flutter!
目前(截至 2017 年 5 月 5 日)的 Flutter SDK 没有内置支持播放和控制任意音频。然而,我们设计了我们的插件系统来支持它。
此插件为 Flutter 添加了音频支持:https://pub.dartlang.org/packages/audioplayer
来自插件的自述文件:
Future play() async {
final result = await audioPlayer.play(kUrl);
if (result == 1) setState(() => playerState = PlayerState.playing);
}
// add a isLocal parameter to play a local file
Future playLocal() async {
final result = await audioPlayer.play(kUrl);
if (result == 1) setState(() => playerState = PlayerState.playing);
}
Future pause() async {
final result = await audioPlayer.pause();
if (result == 1) setState(() => playerState = PlayerState.paused);
}
Future stop() async {
final result = await audioPlayer.stop();
if (result == 1) {
setState(() {
playerState = PlayerState.stopped;
position = new Duration();
});
}
}
播放已在资产中定义的文件的简单解决方案是使用 AudioCache。
图书馆:https://pub.dartlang.org/packages/audioplayers。
More about AudioCache
将库添加到 pubspec.yaml
后,需要导入 class:
import 'package:audioplayers/audio_cache.dart';
在同一个文件中添加一个资产,并将带有声音的文件放入资产文件夹(如果没有此文件夹,请创建它)
assets:
- assets/sound_alarm.mp3
然后添加此代码:
static AudioCache player = new AudioCache();
const alarmAudioPath = "sound_alarm.mp3";
player.play(alarmAudioPath);
一个例子here
[答案已更新:此方法无效,请参阅评论] 您可以使用 Flutter 团队维护的 video_player 插件。它可以跨平台复制多种媒体,包括声音文件。更具体地说,您可能想要使用 VideoPlayerController class.
例如。
_controller = VideoPlayerController.network('<a href="https://www.example.com/soundsFile.wav" rel="nofollow noreferrer">https://www.example.com/soundsFile.wav</a>');
_controller.play();
</pre>
音频播放器有效(来自 https://medium.com/@bennett4/adding-custom-sound-effects-to-a-flutter-mobile-app-41594f1f3305):
(1) 将库添加到您的 pubspec.yaml:audioplayers: ^0.15.1
(2) 在 flutter
下的 pubspec.yaml 中添加对资产文件的引用:
flutter
assets:
- assets/yes.mp3
确保它在资产文件夹下。它在子文件夹中时不起作用。例如,类似:- assets/sounds/yes.mp3 将不起作用。只需将您的音频文件放在 assets 文件夹中,而不是其子文件夹中
(3) 在您的应用中将库导入为:import package:audioplayers/audioplayers.dart;
(4) 然后定义这个函数:
Future<AudioPlayer> playLocalAsset() async {
AudioCache cache = new AudioCache();
//At the next line, DO NOT pass the entire reference such as assets/yes.mp3. This will not work.
//Just pass the file name only.
return await cache.play("yes.mp3");
}
(5) 每当需要播放声音时调用函数:await playLocalAsset();
空安全代码:
将依赖项添加到您的
pubspec.yaml
文件,dependencies: audioplayers: ^0.19.0
将音频文件路径添加到您的
pubspec.yaml
文件。flutter: assets: - assets/audio/my_audio.mp3
运行
flutter pub get
完整代码:
class HomePage extends StatelessWidget {
final AudioCache _audioCache = AudioCache(
prefix: 'audio/',
fixedPlayer: AudioPlayer()..setReleaseMode(ReleaseMode.STOP),
);
@override
Widget build(BuildContext context) {
return Scaffold(
body: ElevatedButton(
onPressed: () => _audioCache.play('my_audio.mp3'),
child: Text('Play Audio'),
),
);
}
}