如何在Ionic 2上录制音频和播放?

时间:2016-07-22 07:43:02

标签: audio typescript ionic-framework audio-recording ionic2

我目前正在开发一个Ionic 2应用程序项目,该项目允许用户录制自己的声音,停止录制并播放该声音。根据离子原生,我可以看到它们提供了2个插件,即MediaPlugin和MediaCapture。我已经尝试过使用MediaPlugin但是我在启动记录时遇到了问题,停止并播放它。 有没有人使用过这个插件?我已经浏览了离子文档和其他一些博客,但我仍然无法做到。我对此非常陌生,非常感谢你的努力。我很感激你的想法。

此致

这是我在开始录制时从模拟器获得的日志:

I/MPEG4Writer(  401): limits: 2147483647/0 bytes/us, bit rate: 12200 bps and the estimated moov size 3072 bytes
D/Genyd   (   56): Received Set Clipboard
D/Genymotion(   56): Received Set Clipboard
D/dalvikvm(  379): GC_CONCURRENT freed 717K, 13% free 6011K/6864K, paused 0ms+1ms, total 10ms
E/genymotion_audio(  401): get_next_buffer() pcm_read error -1
W/PluginManager( 1116): THREAD WARNING: exec() call to Media.startRecordingAudio blocked the main thread for 10037ms. Plugin should use CordovaInterface.getThreadPool().
E/genymotion_audio(  401): get_next_buffer() pcm_read error -16
E/genymotion_audio(  401): get_next_buffer() pcm_read error -16
E/genymotion_audio(  401): get_next_buffer() pcm_read error -16
E/genymotion_audio(  401): get_next_buffer() pcm_read error -16
E/genymotion_audio(  401): get_next_buffer() pcm_read error -16
E/genymotion_audio(  401): get_next_buffer() pcm_read error -16
I/MPEG4Writer(  401): setStartTimestampUs: 10031588
I/MPEG4Writer(  401): Earliest track starting time: 10031588
E/genymotion_audio(  401): get_next_buffer() pcm_read error -16
E/genymotion_audio(  401): get_next_buffer() pcm_read error -16
E/genymotion_audio(  401): get_next_buffer() pcm_read error -16
E/genymotion_audio(  401): get_next_buffer() pcm_read error -16
E/genymotion_audio(  401): get_next_buffer() pcm_read error -16
E/genymotion_audio(  401): get_next_buffer() pcm_read error -16

这是我在停止录制时从模拟器获得的日志:

 I/MPEG4Writer(  401): Received total/0-length (42/0) buffers and encoded 42 frames. - audio
    I/MPEG4Writer(  401): Audio track drift time: 0 us
    D/MPEG4Writer(  401): Stopping Audio track source
    E/genymotion_audio(  401): get_next_buffer() pcm_read error -16
    D/MPEG4Writer(  401): Audio track stopped
    D/MPEG4Writer(  401): Stopping writer thread
    D/MPEG4Writer(  401): 0 chunks are written in the last batch
    D/MPEG4Writer(  401): Writer thread stopped
    I/MPEG4Writer(  401): The mp4 file will not be streamable.
    D/MPEG4Writer(  401): Stopping Audio track
    D/AudioPlayer( 1116): renaming /storage/emulated/0/tmprecording.3gp to /storage/emulated/0/../Documents/undefined-.wav
    E/AudioPlayer( 1116): FAILED renaming /storage/emulated/0/tmprecording.3gp to /storage/emulated/0/../Documents/undefined-.wav
    W/PluginManager( 1116): THREAD WARNING: exec() call to Media.stopRecordingAudio blocked the main thread for 135ms. Plugin should use CordovaInterface.getThreadPool().

这是我的home.ts代码:

import {Component} from '@angular/core';
import {NavController, Platform, Page, Events} from 'ionic-angular';
import {MediaPlugin} from 'ionic-native';

@Component({
  templateUrl: 'build/pages/home/home.html'
})
export class HomePage {
    private _platform: Platform;
    private _fileRecord: MediaPlugin;
    private _pathFile: string;
    private _nameFile: string;
  constructor(private navCtrl: NavController, platform: Platform) {
     this._platform = platform;
  }
  public startRecord(): void {
     this._pathFile = this.getPathFileRecordAudio();
     this._fileRecord = new MediaPlugin(this._pathFile);
     this._fileRecord.startRecord();
  }

  public stopRecord(): void {
     this._fileRecord.stopRecord();
  }

  private startPlay(): void {
     this._fileRecord = new MediaPlugin(this._pathFile);
     this._fileRecord.play();
  }

  private getPathFileRecordAudio(): string {
     let path: string = (this._platform.is('ios') ? '../Library/NoCloud/': '../Documents/');
     return path + this._nameFile + '-' + '.wav';
  }
 }

1 个答案:

答案 0 :(得分:0)

您是否在source中查看了该插件的评论?有关于如何调整事物的说明......有人有类似的here,但你提供了更多有关错误的详细信息。 (我将评论复制到答案中)。