无法播放录制的Android音频文件

时间:2020-07-29 16:35:48

标签: javascript android react-native audio react-native-android

我已经实现了库react-native-audio-recorder-player,并且能够记录音频。但是,每当我尝试从录制的路径播放音频时,都会收到错误消息。我在做什么错,我该如何解决?这似乎是一个相当普通的库,但是没有太多可用信息。另外,我已经要求并被授予所有权限来记录音频和对外部存储设备进行读取/写入。 这是我的代码,它记录音频然后分别播放音频,这些功能是在按自定义按钮时触发的。

onStartRecord = async () => {
const path = Platform.select({
  ios: 'hello.m4a',
  android: 'sdcard/hello.mp4',
});
console.log(path);
//Result is the URL of the file
const result = await audioRecorderPlayer.startRecorder(path);
audioRecorderPlayer.addRecordBackListener((e) => {
  console.log(e);
  this.setState({
    recordSecs: e.current_position,
    recordTime: audioRecorderPlayer.mmssss(Math.floor(e.current_position)),
  });
  return;
});
console.log(`Result: ${result}`);
};

onStartPlay = async () => {
console.log('onStartPlay');
const path = Platform.select({
  ios: 'hello.m4a',
  android: 'sdcard/hello.mp4',
});
console.log(path);
const msg = await this.audioRecorderPlayer.startPlayer(path);
console.log(msg);
this.audioRecorderPlayer.addPlayBackListener((e) => {
  if (e.current_position === e.duration) {
    console.log('finished');
    this.audioRecorderPlayer.stopPlayer();
  }
  this.setState({
    currentPositionSec: e.current_position,
    currentDurationSec: e.duration,
    playTime: this.audioRecorderPlayer.mmssss(
      Math.floor(e.current_position),
    ),
    duration: this.audioRecorderPlayer.mmssss(Math.floor(e.duration)),
  });
  return;
});
};

运行音频onStartPlay()之后,这是我收到的错误:

可能的未处理的承诺拒绝(id:0): TypeError:undefined不是对象(评估'_this.audioRecorderPlayer.startPlayer') _callee3 $ @ http://10.0.2.2:8081 / index.bundle?platform = android&dev = true&minify = false:99040:76 tryCatch @ http://10.0.2.2:8081 / index.bundle?platform = android&dev = true&minify = false:24976:23 invoke @ http://10.0.2.2:8081 / index.bundle?platform = android&dev = true&minify = false:25149:32 tryCatch @ http://10.0.2.2:8081 / index.bundle?platform = android&dev = true&minify = false:24976:23 invoke @ http://10.0.2.2:8081 / index.bundle?platform = android&dev = true&minify = false:25049:30 http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:25079:19 tryCallTwo @ http://10.0.2.2:8081 / index.bundle?platform = android&dev = true&minify = false:27040:9 doResolve @ http://10.0.2.2:8081 / index.bundle?platform = android&dev = true&minify = false:27204:25 Promise @ http://10.0.2.2:8081 / index.bundle?platform = android&dev = true&minify = false:27063:14 callInvokeWithMethodAndArg @ http://10.0.2.2:8081 / index.bundle?platform = android&dev = true&minify = false:25078:33 enqueue @ http://10.0.2.2:8081 / index.bundle?platform = android&dev = true&minify = false:25083:157 异步@http://10.0.2.2:8081 / index.bundle?platform = android&dev = true&minify = false:25100:69 _callee3 @ http://10.0.2.2:8081 / index.bundle?platform = android&dev = true&minify = false:99032:42 onPress @ http://10.0.2.2:8081 / index.bundle?platform = android&dev = true&minify = false:99154:31 _performTransitionSideEffects @ http://10.0.2.2:8081 / index.bundle?platform = android&dev = true&minify = false:55035:22 _receiveSignal @ http://10.0.2.2:8081 / index.bundle?platform = android&dev = true&minify = false:54977:45 onResponderRelease @ http://10.0.2.2:8081 / index.bundle?platform = android&dev = true&minify = false:54886:34 invokeGuardedCallbackImpl @ http://10.0.2.2:8081 / index.bundle?platform = android&dev = true&minify = false:9186:21 invokeGuardedCallback @ http://10.0.2.2:8081 / index.bundle?platform = android&dev = true&minify = false:9280:42 invokeGuardedCallbackAndCatchFirstError @ http://10.0.2.2:8081 / index.bundle?platform = android&dev = true&minify = false:9284:36 executeDispatch @ http://10.0.2.2:8081 / index.bundle?platform = android&dev = true&minify = false:9356:48 executeDispatchesInOrder @ http://10.0.2.2:8081 / index.bundle?platform = android&dev = true&minify = false:9376:26 executeDispatchesAndRelease @ http://10.0.2.2:8081 / index.bundle?platform = android&dev = true&minify = false:10451:35 forEach @ [本地代码] forEachAccumulated @ http://10.0.2.2:8081 / index.bundle?platform = android&dev = true&minify = false:9518:22 runEventsInBatch @ http://10.0.2.2:8081 / index.bundle?platform = android&dev = true&minify = false:10475:27 runExtractedPluginEventsInBatch @ http://10.0.2.2:8081 / index.bundle?platform = android&dev = true&minify = false:10554:25 http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:10530:42 batchedUpdates $ 1 @ http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:21179:20 batchedUpdates @ http://10.0.2.2:8081 / index.bundle?platform = android&dev = true&minify = false:10437:36 _receiveRootNodeIDEvent @ http://10.0.2.2:8081 / index.bundle?platform = android&dev = true&minify = false:10529:23 receiveTouches @ http://10.0.2.2:8081 / index.bundle?platform = android&dev = true&minify = false:10582:34 __callFunction @ http://10.0.2.2:8081 / index.bundle?platform = android&dev = true&minify = false:2765:36 http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:2497:31 __guard @ http://10.0.2.2:8081 / index.bundle?platform = android&dev = true&minify = false:2719:15 callFunctionReturnFlushedQueue @ http://10.0.2.2:8081 / index.bundle?platform = android&dev = true&minify = false:2496:21 callFunctionReturnFlushedQueue @ [本地代码]

0 个答案:

没有答案