在Ionic 3上录制和播放音频

时间:2017-07-20 19:03:43

标签: javascript ios ionic-framework ionic3

我在iOS上遇到了一个奇怪的问题。

我正在使用Ionic Native Media插件录制音频并尝试使用HTML5 Web Audio API(WavesurferJS或HowlerJS)播放录音。 我录制音频后,如果我立即尝试播放录音,音频将无法播放,但如果我关闭应用程序然后重新打开它,音频将播放正常。

HowlerJS抛出的错误是“解码音频时出错”。我的想法是Native Media插件还没有完全释放文件,因此没有授权HowlerJS读取音频数据。

感谢任何想法或方向。

2 个答案:

答案 0 :(得分:0)

https://github.com/ionic-team/ionic-native/blob/master/src/%40ionic-native/plugins/media/index.ts - 在评论中提到的东西..

 * Some hints if you are using iOS and recording doesn't work:
 * 1.) Try to use a absolute file path but remove beginning "file://".
 * Then it looks like: `/var/mobile/Containers/Data/Application/AF438B8B-7724-4FBB-8E69-083463224FC4/tmp/my_file.m4a`
 * Example: `this.media.create(this.file.tempDirectory.replace(/^file:\/\//, '') + 'my_file.m4a')`
 * 2.) If that's not working, too, create the file before using.
 * Example:
 * ```typescript
 * import { Media, MediaObject } from '@ionic-native/media';
 * import { File } from '@ionic-native/file';
 *
 * ...
 *
 * constructor(private media: Media, private file: File) { }
 *
 * ...
 *
 * this.file.createFile(this.file.tempDirectory, 'my_file.m4a', true).then(() => {
 *   let file = this.media.create(this.file.tempDirectory.replace(/^file:\/\//, '') + 'my_file.m4a');
 *   file.startRecord();
 *   window.setTimeout(() => file.stopRecord(), 10000);
 * });
 * ```
 *
 * You can find the reasons here: https://github.com/ionic-team/ionic-native/issues/1452#issuecomment-299605906
 * @classes
 * MediaObject
 * @interfaces
 * MediaError
 */

答案 1 :(得分:0)

使用Cordova插件媒体:

$ ionic cordova plugin add cordova-plugin-media
$ npm install --save @ionic-native/media

来自离子doc的有用文章:

https://ionicframework.com/docs/native/media/