0

in my flutter application I want to make a audio player here is my code but the problem is that when I click in icon button nothing happen and the audio don't start how can I fix this

import 'dart:async';
import 'dart:ui';
import 'package: flutter/cupertino.dart';
import 'package: flutter/material.dart';
import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
import 'package:google_fonts/google_fonts.dart';

class CalmForest extends StatefulWidget {
  const CalmForest({super.key});

  @override
  State<CalmForest> createState() => _CalmForestState();
}

class _CalmForestState extends State<CalmForest> {
  //const CalmForest({super.key});
  final AudioPlayer player = AudioPlayer();
  void playAudio() async {
    final audioPlayer = AudioPlayer();
    final audioSource = AssetSource('assets/audio/Forest.mp3');
    await audioPlayer.play(audioSource);
  }

the audio is from assets

I am using audioplayers: ^6.0.0 for playing audio


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        width: double.infinity,
        height: double.infinity,
        decoration: BoxDecoration(
          image: DecorationImage(
            image: AssetImage('assets/forest.png'),
            fit: BoxFit.cover,
          ),
        ),
        child: BackdropFilter(
          filter: ImageFilter.blur(sigmaX: 8, sigmaY: 8),
          child: ClipRect(
            child: Container(
              color: Colors.black.withOpacity(0.5),
              child: Stack(
                children: [
                  Center(
                    child: Container(
                      width: 250,
                      height: 300,
                      decoration: BoxDecoration(
                          image: DecorationImage(
                              image: AssetImage('assets/forest.png'))),
                    ),
                  ),
                  SizedBox(
                    height: 10,
                  ),
                  Align(
                    alignment: Alignment.bottomCenter,
                    child: Column(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        SizedBox(
                          height: 10,
                        ),
                        Row(
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            Icon(Icons.graphic_eq,
                                size: 30, color: Colors.white),
                            SizedBox(
                              width: 25,
                            ),
                            Text(
                              "Forest",
                              style: GoogleFonts.merriweather(
                                color: Colors.white,
                                fontSize: 25,
                              ),
                            ),
                            SizedBox(
                              width: 25,
                            ),
                            Icon(Icons.graphic_eq,
                                size: 30, color: Colors.white),
                          ],
                        ),
                        IconButton(
                            onPressed: () {
                              playAudio();
                            },
                            icon: Icon(
                              Icons.play_circle,
                              size: 60,
                              color: Colors.white,
                            )),
                        SizedBox(
                          height: 120,
                        ),
                      ],
                    ),
                  )
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}

how can I make the sound play can you help me this is the whole probleme

3
  • did u turn on volumn on your simulator/emulator? Commented Apr 21 at 6:34
  • yes i did but not working Commented Apr 21 at 19:21
  • Try replacing final audioSource = AssetSource('assets/audio/Forest.mp3'); await audioPlayer.play(audioSource); With following await audioPlayer.setSource(AssetSource('assets/audio/Forest.mp3')); await audioPlayer.resume(); Commented Apr 22 at 8:29

1 Answer 1

0

https://pub.dev/packages/audio_waveforms

You can use this package to play the audio:

this blog will guide you to how to play audio using this package

https://blog.logrocket.com/audio-waveforms-flutter-app/#playing-audio

Not the answer you're looking for? Browse other questions tagged or ask your own question.