尝试使用离子中的媒体和文件进行录制时的低质量音频文件

Low quality audio file when trying to record using media and file in ionic

我正在尝试使用媒体 (https://ionicframework.com/docs/native/media) 在 android 中录制音频 但是当我回放时,录制的音频质量很差,有噪音, 这是离子论坛中问题的link:https://forum.ionicframework.com/t/low-quality-audio-file-when-trying-to-record-using-media-and-file/191952

这是我的代码:

import { Component } from '@angular/core';
import { Media, MediaObject } from '@ionic-native/media/ngx';
import { File } from '@ionic-native/file/ngx';
import { Platform } from '@ionic/angular';

@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
})
export class HomePage {


  filePath: string;
  recordedAudio: MediaObject;
  recording: Boolean = false;
  constructor(
    private media: Media,
    private file: File,
    public platform: Platform
  ) { }

  startRecord() {
    if (this.platform.is('android')) {

      console.log('Android');
      console.log(this.file.externalRootDirectory);
      this.filePath = this.file.externalRootDirectory + 'my_file.3gp';
      console.log(this.filePath)
      this.file.createFile(this.file.externalRootDirectory, 'my_file.3gp', true).then(() => {
        this.recordedAudio = this.media.create(this.file.externalRootDirectory.replace(/^file:\/\//, '') + 'my_file.3gp');
        this.recordedAudio.startRecord();
        window.setTimeout(() => this.recordedAudio.stopRecord(), 50000);
      });
    }
    this.recording = true;
  }

  stopRecord() {
    if (this.platform.is('android')) {
      this.recordedAudio.stopRecord();
      this.recordedAudio.release();
    }

  }

  playSelectedTrack() {
    // use AudioProvider to control selected track 
    if (this.platform.is('android')) {
      this.recordedAudio.play();
    }
  }

  pauseSelectedTrack() {
    // use AudioProvider to control selected track 
    if (this.platform.is('android')) {
      this.recordedAudio.pause();
    }
  }
}

这可能是因为音频编码。 3gp 听起来总是很糟糕。试试 m4amp3m4a 适用于少数 android 部手机并且质量更好。

这来自 Cordova Github Repo:Android 设备以 AAC ADTS 文件格式录制音频。指定的文件应以 .aac 扩展名结尾。

您还可以使用 Media-Capture 录制音频。

Github Repo: Media-Capture