Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 545 Vote(s) - 3.44 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to play a custom sound in Flutter?

#1
I was able to play a simple sound with this line of code:

SystemSound.play(SystemSoundType.click);

How can I play a customized sound?

Let's say a short mp3
Reply

#2
Thanks for checking out Flutter!

Flutter SDK today (as of May 5, 2017) doesn't have _built-in_ support to play and control arbitrary audio. However, we designed our plugin system to support it.

This plugin adds audio support to Flutter:

[To see links please register here]


From the plugin's 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();
});
}
}

Reply

#3
Simple solution for playing a file already defined in assets is using AudioCache.
Library:

[To see links please register here]

.
[More about AudioCache][1]
After adding library to `pubspec.yaml`, import required class:

import 'package:audioplayers/audio_cache.dart';

add an asset in the same file and place the file with sound to assets folder (if you don't have this folder, create it)

assets:
- assets/sound_alarm.mp3

then add this code:

static AudioCache player = new AudioCache();
const alarmAudioPath = "sound_alarm.mp3";
player.play(alarmAudioPath);

An example [here][2]


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#4
[Answer updated: this approach doesn't work, see comments]
You can use the [video_player][1] plugin maintained by the Flutter team. It can reproduce many kinds of media across platforms, including sound files. More specifically, you may want to use the the VideoPlayerController class.

eg. <pre><code> _controller = VideoPlayerController.network('https://www.example.com/soundsFile.wav');
_controller.play();
</pre></code>


[1]:

[To see links please register here]

Reply

#5
### Null-safe code:

1. Add dependency to your `pubspec.yaml` file,

```
dependencies:
audioplayers: ^0.19.0
```


2. Add audio file path to your `pubspec.yaml` file.

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

3. Run `flutter pub get`


**Full code:**

```dart
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'),
),
);
}
}
```
Reply

#6
The audioplayers works (from [

[To see links please register here]

][1]):

(1) Add the library to your pubspec.yaml: `audioplayers: ^0.15.1`

(2) In pubspec.yaml under `flutter` add the reference to your assets file:

flutter
assets:
- assets/yes.mp3
MAKE SURE it is under the assets folder. It does not work when it is in a subfolder. For example, something like: - assets/sounds/yes.mp3 will not work. Just put your audio file in the assets folder, not in its subfolder

(3) import the library in your app as: `import package:audioplayers/audioplayers.dart;`

(4) then define this function:

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) call the function whenever you need to play a sound: `await playLocalAsset();`


[1]:

[To see links please register here]

Reply

#7
You can use just_audio package.

To play sound from a local file...
Follow the steps :-
1. Run flutter pub add just_audio in your terminal
2. import 'package:just_audio/just_audio.dart'; -> Import package in your file
3. AudioPlayer player = AudioPlayer(); -> Create the object
4. player.setAsset('path_to_your_audiofile'); -> Set the path to your audio asset
5. player.play(); -> Play the audio

Here is a sample method implementing this :-



void playSampleSound() async {
AudioPlayer player = AudioPlayer();
await player.setAsset('assets/audio/sample_audio.mp3');
player.play();
}

Thanks
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through