如何在Ionic 2上录制音频并进行回放?

9
我目前正在开发一个Ionic 2应用程序项目,允许用户录制自己的声音,停止录制并播放该声音。根据ionic-native,我可以看到它们提供了两个插件,分别是MediaPlugin和MediaCapture。我已经尝试使用MediaPlugin,但在开始录制、停止录制和播放时遇到了问题。有人使用过这个插件吗?我已经阅读了ionic文档和其他一些博客,但仍然无法解决问题。我对此非常陌生,感谢您的努力。我欣赏你们所有的想法。
问候,
这是我从仿真器中获得的日志,当我开始录制时:
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个回答

1

你有没有查看插件中的评论?有关如何调整事物的注释...有人在这里遇到了类似的问题,但你提供了更多与错误相关的细节。(我将评论复制到了那个答案中)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接