Home > Software engineering >  How can I solve this issue of non working audio-player in Flutter
How can I solve this issue of non working audio-player in Flutter

Time:01-16

I am facing a problem with getting the audio-player to work in Flutter.

My simple app consists of a bunch of keys with different colors, and each one of them is supposed to play some audio file whenever clicked (The audio files are local and bundled with the app inside an assets folder which is at the same level as the lib folder, and also the audio files have an extension of .wav).

I am using the audioplayers 2.0.0 library and defining an instance of the AudioPlayer and storing it in a variable, then calling the AudioPlayer with the method play and because my audios are local, I am wrapping my audio file inside AssetSource().

But, whenever I click any of the keys nothing get played and there is this message in the console (This time when clicking the first key):

E/flutter ( 5043): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] 
Unhandled Exception: MissingPluginException(No implementation found for method getTemporaryDirectory on channel plugins.flutter.io/path_provider) 
E/flutter ( 5043): #0      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:294:7) E/flutter ( 5043): <asynchronous suspension> 
E/flutter ( 5043): #1      getTemporaryDirectory (package:path_provider/path_provider.dart:55:24) 
E/flutter ( 5043): <asynchronous suspension> 
E/flutter ( 5043): #2      AudioCache.fetchToMemory (package:audioplayers/src/audio_cache.dart:78:27) 
E/flutter ( 5043): <asynchronous suspension> 
E/flutter ( 5043): #3      AudioCache.load (package:audioplayers/src/audio_cache.dart:101:31) 
E/flutter ( 5043): <asynchronous suspension> 
E/flutter ( 5043): #4      AudioPlayer.setSourceAsset (package:audioplayers/src/audioplayer.dart:249:17) 
E/flutter ( 5043): <asynchronous suspension> 
E/flutter ( 5043): #5      AudioPlayer.play (package:audioplayers/src/audioplayer.dart:131:5) 
E/flutter ( 5043): <asynchronous suspension> 
E/flutter ( 5043): 

Here is my code in the main file:

import 'package:flutter/material.dart';
import 'package:audioplayers/audioplayers.dart';


void main() => runApp(MyApp());

class MyApp extends StatelessWidget {

  Widget buildKey(int noteToPlay, Color keyColor) {
    return Expanded(
      child: Container(
        color: keyColor,
        child: TextButton(
          child: Text('This is the button #$noteToPlay',
            style: TextStyle(
              fontSize: 25,
              color: Colors.white,
            ),
          ),
          onPressed: () async{
            final player = AudioPlayer();
            player.play(AssetSource('assets_note$noteToPlay.wav'));
          },
        ),
      ),
    );
  }


  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
          body: SafeArea(
            child: Column(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: [
                  buildKey(1, Colors.red),
                  buildKey(2, Colors.orange),
                  buildKey(3, Colors.yellow),
                  buildKey(4, Colors.green),
                  buildKey(5, Colors.teal),
                  buildKey(6, Colors.blue),
                  buildKey(7, Colors.purple),
                ]
            ),
          )
      ),
    );
  }
}

I read about the audioplayers library and tried to figure out the problem, but came to no conclusions, hope you can help me

CodePudding user response:

The relevant part is that you are receiving a MissingPluginException, which means that flutter cannot find a package implementation. Make sure that you included all the packages you rely on in your pubspec.yaml file.

Afterwards, delete your app from the device/simulator and reinstall again. This will load all the plugin-channels to native code

  • Related