我成功实现了一项音频服务,以使用@ ionic-native / media插件处理多个级联的音频文件的播放,并且在Mac / Windows / Chrome上选中该功能后,在浏览器上也可以正常工作。但是,在iOS / Chrome / Safari上,播放列表中的第一个文件会播放,但后续文件不会播放。我做了很多研究,但没有收到任何错误或信息……在iOS上,mediaObject的第二个实例不起作用,并且确保代码得到执行,直到相关行-this.mediaObject.play()-对于那个例子
这是我的音频服务:
import {Injectable} from '@angular/core';
import {Media, MediaObject} from "@ionic-native/media";
@Injectable()
export class AudioService {
mediaObject: MediaObject;
playlist: Array<string> = [];
counter:number = 0;
constructor(
private media: Media
) {}
preparePlayAudio(src: string) {
if (typeof this.mediaObject !== 'undefined') {
this.mediaObject.pause();
this.mediaObject.release();
this.mediaObject = undefined;
}
this.mediaObject = this.media.create(src);
this.playAudio();
}
next() {
this.counter += 1;
if (typeof this.playlist[this.counter] !== 'undefined') {
this.preparePlayAudio(this.playlist[this.counter]);
}
}
playAudio() {
this.mediaObject.play();
this.mediaObject.onStatusUpdate.subscribe(status =>
{
if (status.toString()=="1") { //player start
}
if (status.toString()=="4") { // player end running
this.next()
}
});
}
play(playlist: string[]): void {
this.counter = 0;
this.playlist = playlist;
this.preparePlayAudio(playlist[this.counter]);
}
}
我叫
audioService.play(['audio1.mp3', 'audio2.mp3']);
版本:离子版本3.9.2 –离子本机媒体:4.7.0