0

I'm pretty new to Flutter and wanna try pogram an app just for fun and personal use. So I wanna play a MP3 and on my research I found the audioplayers package. All tutorials I could found was from really old versions and I could'nt figure out how to play a simple Audiofile from the phone. I startet with a FilePicker and select a file. Next step was to create an AudioPlayer and play the file, but whatever I've tried it won't work.

final AudioPlayer player = AudioPlayer();
File? selectedFile;

...

Future<File?> selectFile() async {
    FilePickerResult? result = await FilePicker.platform.pickFiles(
      type: FileType.audio,
      allowedExtensions: ['mp3', 'wav'], // Erlaubte Audio-Dateiformate
    );

    if (result != null) {
      setState(() {
        selectedFile = File(result.files.single.path!);
      });
      return selectedFile;
    }
  }

  void playSelectedFile() async {
    if (selectedFile != null) {
      int result = await player.play(DeviceFileSource(selectedFile!));
   
    }
  }

So thats the code. The main problem is that I don't really understand the audioplayers package and the audioPlayer.play(...) function, especially what format the data in the brackets needs and the different types. audioplayers on git: https://github.com/bluefireteam/audioplayers/blob/main/getting_started.md

Maybe someone can help :3

I've tried many variations to play a little Sound or MP3, but nothing was working. The main problem is to understand the package.

  • Whenever you specify a package, it's good to leave a link for it. Because if I search in [Pub.dev](https://pub.dev/) for audio players, there are a ton of possible packages. PS: Welcome to StackOverflow! – Canilho Aug 11 '23 at 14:22
  • 1
    I thought the github would be enough. Here's the Link: https://pub.dev/packages/audioplayers/example – trukeOderSo Aug 11 '23 at 15:15
  • It is... My mistake.... I thought it was something else because the link is to a read-me file :D – Canilho Aug 11 '23 at 15:24

1 Answers1

0

According to the Github page of the project you've mentioned, you can either reference the source by URL or its actual path.

Notice that if you chose to use the path, you have to add the mp3 files as Assets of your project, meaning you cannot access any other files that are not immediately accessible from your app.

The assets are defined in your pubspeck.yalm file,

flutter: 
    assets:
      - songs/file_1.mp3
      - songs/file_2.mp3
      - songs/file_3.mp3

Assets can be called like this within Flutter

static const url = 'asset:///songs/file_1.mp3';

There are workarounds if you want to play music files from the local storage of your phone. For that, you'll need some file management package, to be able to read files from your device and to set the permission READ_EXTERNAL_STORAGE on Android, and the equivalent for iOs.

Canilho
  • 944
  • 5
  • 11
  • Ok, there comes the next Question: Where do I safe the MP3-File ? I've tried to create a assets folder in the project folder, but that wasn't working :/ – trukeOderSo Aug 11 '23 at 15:24
  • if you create a folder called "assets" on the root of your project (not under lib), then in the `pubspec.yalm` you add the path to your mp3 files: `assets/file_1.mp3` This must be the exact path... Be sure it exists. If the Path is incorrect, when you build the app, you should get an error saying the asset was not found. When you need to call an asset in your app, you might need to call the full URL again, in this case, `assets/file_1.mp3`. (I'm not sure if you can also call it only by the name) I've added more content to my answer, check if that solves your issue. – Canilho Aug 11 '23 at 15:33
  • 1
    Oh thanks, the shit is working. Thanks very much! Have a nice weekend :) – trukeOderSo Aug 11 '23 at 15:38