dart 如何在Flutter中播放自定义声音?

lc8prwob  于 2023-04-03  发布在  Flutter
关注(0)|答案(6)|浏览(220)

我可以用这行代码播放一个简单的声音:

SystemSound.play(SystemSoundType.click);

如何播放自定义声音?
比如说一小段mp3

wj8zmpe1

wj8zmpe11#

播放已在资源中定义的文件的简单解决方案是使用AudioCache. Library:https://pub.dartlang.org/packages/audioplayers . More about AudioCache将库添加到pubspec.yaml后,导入所需的类:

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

62o28rlo

62o28rlo2#

感谢您的评分
Flutter SDK今天(截至2017年5月5日)没有 * 内置 * 支持播放和控制任意音频。但是,我们设计了我们的插件系统来支持它。
此插件为Flutter添加音频支持:https://pub.dartlang.org/packages/audioplayer
从插件的README:

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();
    });
  }
}
ig9co6j1

ig9co6j13#

音频播放器的工作原理(来自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中,添加对assets文件的引用:

flutter
    assets:
       - assets/yes.mp3

请确保它位于assets文件夹下。如果它位于子文件夹中,则不起作用。例如:- 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();

gudnpqoy

gudnpqoy4#

空安全代码:

1.将依赖项添加到pubspec.yaml文件,

dependencies:
  audioplayers: ^0.19.0

1.将音频文件路径添加到pubspec.yaml文件。

flutter: 
  assets:
    - assets/audio/my_audio.mp3

1.运行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'),
      ),
    );
  }
}
swvgeqrz

swvgeqrz5#

[答案更新:这种方法不起作用,请参阅评论]您可以使用Flutter团队维护的video_player插件。它可以跨平台再现多种媒体,包括声音文件。更具体地说,您可能希望使用VideoPlayerController类。
例如

_controller = VideoPlayerController.network('https://www.example.com/soundsFile.wav');
   _controller.play();
ztmd8pv5

ztmd8pv56#

你可以使用just_audio包。
要从本地文件播放声音...请按照以下步骤操作:-
1.在终端运行flutter pub add just_audio
1.导入“软件包:just_audio/just_audio. dart”;- 〉在文件中导入包

  1. System. out. println();- 〉创建对象
    1.播放器.setAsset('path_to_your_audiofile');- 〉设置音频资源的路径
  2. player.play (); -〉播放音频
    下面是一个实现此功能的示例方法:
void playSampleSound() async {
     AudioPlayer player = AudioPlayer();
    await player.setAsset('assets/audio/sample_audio.mp3');
    player.play();
  }

谢谢

相关问题