skip to Main Content

I record a audio using record_mp3: ^3.0.0 (this) package. Like below, How to play it using audioplayers package (this).

Below – How I record audio –

void startRecord() async {
    bool hasPermission = await checkPermission();
    if (hasPermission) {
      statusText = "Recording...";
      recordFilePath = await getFilePath();
      isComplete = false;
      RecordMp3.instance.start(recordFilePath, (type) {
        statusText = "Record error--->$type";
        setState(() {});
      });
    } else {
      statusText = "No microphone permission";
    }
    setState(() {});
  }



void stopRecord() {
    bool s = RecordMp3.instance.stop();
    if (s) {
      statusText = "Record complete";
      isComplete = true;
      setState(() {});
    }
  }

Future<String> getFilePath() async {
    Directory storageDirectory = await getApplicationDocumentsDirectory();
    String sdPath = storageDirectory.path + "/record";
    var d = Directory(sdPath);
    if (!d.existsSync()) {
      d.createSync(recursive: true);
    }
    return sdPath + "/test_${i++}.mp3";
  }

after recording it, I want to play it. I used this method, (They given in example)

void play() {
    if (recordFilePath != null && File(recordFilePath).existsSync()) {
      AudioPlayer audioPlayer = AudioPlayer();
      audioPlayer.play(recordFilePath, isLocal: true);
    }
  }

Now, It’s not valid. show syntax error

  • The argument type ‘String’ can’t be assigned to the parameter type ‘Source’
  • The named parameter ‘isLocal’ isn’t defined.

How to play the audio I have recorded.

2

Answers


  1. Chosen as BEST ANSWER

    We can play it like this,

    void play() async {
        if (File(recordFilePath).existsSync()) {
          await audioPlayer.play(DeviceFileSource(recordFilePath));
        }
      }
    

  2. The error you’re encountering is because the audioplayers package has likely undergone some changes or updates since the version you’re using, and the play method signature has changed. To play audio using the audioplayers package, you should use the audioplayers API correctly.

    Here’s how you can play the audio you’ve recorded using the updated audioplayers package:

    First, make sure to add the audioplayers package to your pubspec.yaml file and update it to the latest version:

    import 'package:audioplayers/audioplayers.dart';
    
     void play() {
      if (recordFilePath != null && File(recordFilePath).existsSync()) {
          AudioPlayer audioPlayer = AudioPlayer();
          audioPlayer.play(recordFilePath, isLocal: true); // Use isLocal to 
          specify that the file is local
       }
     }
    

    Make sure to import the audioplayers package and use the AudioPlayer class from it.

    By specifying isLocal: true, you indicate that the audio file is a local file on the device.

    Now, your code should work without any syntax errors, and it should play the recorded audio using the audioplayers package.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search