无法播放音频 [英] Could not make audio playback

查看:163
本文介绍了无法播放音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试录制音频并将其保存到特定文件夹,并使用保存的音频制作一些播放功能。我可以录制并保存它,但我无法将该音频文件保存到目录中,并且无法播放有人可以帮助我

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();
    }

}

目前我能够制作一个文件夹进入我的移动内部存储,我可以使用我的文件浏览器看到它,录制的音频文件也在我的内部存储中可见,但我无法播放我得到错误代码1 NOT_FOUND_ERR。
如果我试图获得持续时间我在我的控制台获得-1
我在Android设备中测试它
感谢你提前帮助

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

推荐答案

我给出答案这里到你的主要问题。我想如果您按照这些指南,您的应用程序将有效。

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

如果您检查它, getDuration()函数将始终显示-1没有开始播放。
所以,如果你想检查音乐的持续时间,你必须开始播放。

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);    

这篇关于无法播放音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆