2016-07-22 33 views
7

我目前正在研究Ionic 2應用程序項目,該項目允許用戶錄製自己的聲音,停止錄製並播放聲音。根據離子本機,我可以看到他們提供了2個插件,即MediaPlugin和MediaCapture。我嘗試過使用MediaPlugin,但在開始記錄時遇到了問題,請停下並播放它。 有沒有人經歷過使用這個插件?我已經瀏覽了離子文檔和其他一些博客,但仍然無法完成。我對此很新,非常感謝你的努力。我很欣賞你的想法。如何在Ionic 2上錄製音頻和播放?

問候,

這是我從模擬器了,同時開始記錄日誌:

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'; 
    } 
} 

回答

0

您是否檢出了插件source中的註釋?有關於如何調整東西的筆記...有人有類似的東西here但你給了更多的細節有關的錯誤。 (我將這些評論複製到答案中)。