Could not make audio playback

2019-09-14 17:00发布

问题:

I am trying to record audio and save it to a specific folder and make some playback functionality with that saved audio. i could record and save it, but i could not save that audio file to a directory and could not able to make playback could someone help me

import { Component } from '@angular/core';
import {File} from 'ionic-native';
import { NavController,AlertController  } from 'ionic-angular';
import { MediaPlugin } from 'ionic-native';

 declare var cordova;
 declare var cordovaFile; 

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})


export class HomePage {

    media: MediaPlugin = new MediaPlugin('../Library/NoCloud/recording.wav');

    constructor(public navCtrl: NavController, 
                public alertCtrl: AlertController) {

  }
    createFolder(){
        console.log('inside folder creation');
        File.createDir(cordova.file.externalRootDirectory, "Audio_folder", false)
              .then(function (success) {
                console.log("folder creation sucess",success);
              }, function (error) {
                 console.log("folder creation error",error);
              });
    }

    startRecording() {
        try {           
            this.media.startRecord();           
            console.log('started to record');
        }       
        catch (e) {
            this.showAlert('Could not start recording.');
        }
    }
stopRecording() {
        try {
            this.media.stopRecord();
            console.log("media time",this.media.getDuration());
            console.log('record stopped');      
        }
        catch (e) {
            this.showAlert('Could not stop recording.');
        }


    }

startPlayback() { 
        try {
            this.media.play();
            console.log('in start play back function');
            console.log('in start play back function',this.media.play);
        }
        catch (e) {
            this.showAlert('Could not play recording.');
        }
    }

    stopPlayback() {console.log('playback stopped');
        try {
            this.media.stop();
        }
        catch (e) {
            this.showAlert('Could not stop playing recording.');
        }
    }
    showAlert(message) {
        let alert = this.alertCtrl.create({
            title: 'Error',
            subTitle: message,
            buttons: ['OK']
        });
        alert.present();
    }

}

Currently i could able to make a folder into my mobile internal storage and i could able to see it using my file explorer and the recorded audio file is also visible in my internal storage, but i could not make a playback i get an error code 1 NOT_FOUND_ERR. And if i try to get duration i get -1 in my console And i am testing it in android device Thanks for your help in advance

回答1:

Audio recorder on android use:

public media: MediaPlugin;

 startRecording() {
     try { 
         this.media = new MediaPlugin(“recording.mp3”);       
         this.media.startRecord();          
         console.log('started to record');
         }    
   catch (e) {
         this.showAlert('Could not start recording.');
         }
 }


回答2:

I give an answer here to your main question. I think if you will follow those guides your application will works.

The getDuration() function will always show you -1 if you check it without starting the playback. So, if you want to check the duration of music, you have to start playback.

var durationInterval = window.setInterval(() => {
  if (this.mediaPlugin) {
    this.durationOfMusic = this.mediaPlugin.getDuration().toFixed(2);
  } 

  if (this.durationOfMusic > -1) {        
    window.clearInterval(durationInterval);
  }
}, 1);