Home > Mobile >  How to open audio file from file manager and play that audio file using Flutter app
How to open audio file from file manager and play that audio file using Flutter app

Time:12-06

I'm new in flutter development and I got stuck in a program where I want to play an audio file by selecting it from file manager.i am able to open audio file from file manager but I don't know ho to play that audio file. Please help me out with this.

CodePudding user response:

You can use enter image description here

and add a reference in your yaml file and get dependencies

  assets:
    - assets/audio/
import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/material.dart';

class AudioManagerExample extends StatefulWidget {
  const AudioManagerExample({required this.audioName, Key? key}) : super(key: key);

  final String audioName;

  @override
  _AudioManagerExampleState createState() => _AudioManagerExampleState();
}

class _AudioManagerExampleState extends State<AudioManagerExample> {
  AudioPlayer audioPlayer = AudioPlayer();
  late AudioCache audioCache;

  @override
  void initState() {
    audioCache = AudioCache(fixedPlayer: audioPlayer);
    audioCache.load('audio/${widget.audioName}.mp3');
    super.initState();
  }

  @override
  void dispose() {
    audioCache.clearAll();
    audioPlayer.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        ElevatedButton(
          onPressed: () {
            setState(() {
              audioCache.play('audio/${widget.audioName}.mp3');
            });
          },
          child: Text('Play ${widget.audioName}'),
        ),
        ElevatedButton(
          onPressed: () {
            setState(() {
              audioPlayer.pause();
            });
          },
          child: Text('Pause ${widget.audioName}'),
        ),
        ElevatedButton(
          onPressed: () {
            setState(() {
              audioPlayer.stop();
            });
          },
          child: Text('Stop ${widget.audioName}'),
        ),
      ],
    );
  }
}


  • Related