flutterandroid-studio

Flutter audioplayers error - No audio is played when clicking the audio button


I am trying to use the flutter audioplayers package to play audio files and am getting this error. While following an online flutter course , but I don't know how to find the issue.

V/MediaPlayer(27127): resetDrmState:  mDrmInfo=null mDrmProvisioningThread=null mPrepareDrmInProgress=false mActiveDrmScheme=false
V/MediaPlayer(27127): cleanDrmObj: mDrmObj=null mDrmSessionId=null
V/MediaPlayer(27127): resetDrmState:  mDrmInfo=null mDrmProvisioningThread=null mPrepareDrmInProgress=false mActiveDrmScheme=false
V/MediaPlayer(27127): cleanDrmObj: mDrmObj=null mDrmSessionId=null
V/MediaPlayer(27127): resetDrmState:  mDrmInfo=null mDrmProvisioningThread=null mPrepareDrmInProgress=false mActiveDrmScheme=false
V/MediaPlayer(27127): cleanDrmObj: mDrmObj=null mDrmSessionId=null

Here is the code I'm using to test the audioplayers package.

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

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

class XylophoneApp extends StatelessWidget {
void playSound(int soundNumber) {
  final player = AudioPlayer();
  player.setSource(AssetSource('note$soundNumber.wav'));
}

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: SafeArea(
          child: Center(
            child: TextButton(
              onPressed: () {
                playSound(1);
              },
              child: Text('Click Me'),
            ),
          ),
        ),
      ),
    );
  }
}

Solution

  • Try this code

    import 'package:flutter/material.dart';
    import 'package:audioplayers/audioplayers.dart';
    
    void main() => runApp(XylophoneApp());
    
    
    
    class XylophoneApp extends StatelessWidget {
    void playSound(int soundNumber) {
      final player = AudioPlayer();
      player.play('note$soundNumber.wav');
    }
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
            body: SafeArea(
              child: Center(
                child: TextButton(
                  onPressed: () {
                    playSound(1);
                  },
                  child: Text('Click Me'),
                ),
              ),
            ),
          ),
        );
      }
    }