mic-recorder-to-mp3不适用于移动设备

时间:2019-03-26 13:34:06

标签: reactjs redux

我正在尝试同时为Web和移动设备实施音频记录,并已使用mic-recorder-to-mp3这个库进行音频记录,它在台式机浏览器中效果很好,但不适用于android和ios移动浏览器。它没有给出任何错误,并尝试了不同的库,但对我不起作用。

https://www.npmjs.com/package/mic-recorder-to-mp3 使用了这个库

import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import MicRecorder from 'mic-recorder-to-mp3';

class App extends Component {
constructor(props){
  super(props)
  this.state={
   recorder: new MicRecorder({ bitRate: 128 }),
   record: true,
   currentAudio: "",
   currentAudioObject: "",
   currentAudioName: '',
   currentAudioExtension: 'mp3',
   isRecording: false,
   recordAudio: false
  }
 }

 startRecording = () => {
  this.setState({
   record: true
  });
 }

  stopRecording = () => {
   this.state.recorder
   .stop()
   .getMp3()
   .then(([buffer, blob]) => {
    this.setState({ record: false });
    const file = new File(buffer, 'Recorded_audio.mp3', {
      type: blob.type,
      lastModified: Date.now(),
    });
    let src = URL.createObjectURL(file);
    this.setState(prev => {
      prev.currentAudio = src;
      prev.currentAudioObject = file;
      prev.currentAudioName = 'Recorded_audio';
      prev.currentAudioExtension = 'mp3';
      prev.isRecording = true;
      prev.recordAudio = true;
      return prev;
    });
  });
 };

 startRecorder(){
  this.state.recorder
  .start()
  .then(() => {
   this.setState({ record: true });
  })
  .catch(e => {
   console.error(e);
 });
}
 render() {
   return (
    <div className="App">
     {this.state.record ? null:<audio
      controls
       src={this.state.currentAudio}>
        Your browser does not support the
        <code>audio</code> element.
      </audio>}
      <a
        className="App-link"
        rel="noopener noreferrer"
        onClick={this.startRecorder.bind(this)}
      >
        Rec
      </a>
      <button onClick={this.stopRecording} 
      type="button">Stops</button>
  </div>
 );
 }
 }

 export default App;

先谢谢您

1 个答案:

答案 0 :(得分:0)

是否必须使用ogg base64文件

示例base64 ogg音频文件。

data:application/ogg;base64