if (position) {
                    yield this.seekTo({ position });
                }
                (_a = this.audio) === null || _a === void 0? void 0 : _a.removeEventListener('canplay', canPlayListener);
            });
            if (this.audio) {
                this.audio.addEventListener('canplay', canPlayListener);
                this.audio.addEventListener('playing', () => {
                    this.notifyListeners('status', {
                        action: "status",
                        status: {
                            msgType: exports.RmxAudioStatusMessage.RMXSTATUS_PLAYING,
                            trackId: this.getCurrentTrackId(),
                            value: this.getCurrentTrackStatus('playing'),
                        }
                    });
                });
                this.audio.addEventListener('pause', () => {
                    this.notifyListeners('status', {
                        action: "status",
                        status: {
                            msgType: exports.RmxAudioStatusMessage.RMXSTATUS_PAUSE,
                            trackId: this.getCurrentTrackId(),
                            value: this.getCurrentTrackStatus('paused'),
                        }
                    });
                });
                this.audio.addEventListener('error', () => {
                    this.notifyListeners('status', {
                        action: "status",
                        status: {
                            msgType: exports.RmxAudioStatusMessage.RMXSTATUS_ERROR,
                            trackId: this.getCurrentTrackId(),
                            value: this.getCurrentTrackStatus('error'),
                        }
                    });
                });
                this.audio.addEventListener('ended', () => {
                    this.notifyListeners('status', {
                        action: "status",
                        status: {
                            msgType: exports.RmxAudioStatusMessage.RMXSTATUS_STOPPED,
                            trackId: this.getCurrentTrackId(),
                            value: this.getCurrentTrackStatus('stopped'),
                        }
                    });
                });
                this.audio.addEventListener('timeupdate', () => {
                    this.notifyListeners('status', {
                        action: "status",
                        status: {
                            msgType: exports.RmxAudioStatusMessage.RMXSTATUS_PLAYBACK_POSITION,
                            trackId: this.getCurrentTrackId(),
                            value: this.getCurrentTrackStatus(this.lastState),
                        }
                    });
                });
            }
        }
        getCurrentTrackId() {
            if (this.currentTrack) {
                return this.currentTrack.trackId;
            }
            return "INVALID";
        }
        getCurrentIndex() {
            if (this.currentTrack) {
                for (let i = 0; i < this.playlistItems.length; i++) {
                    if (this.playlistItems[i].trackId === this.currentTrack.trackId) {
                        return i;
                    }
                }
            }
            return -1;
        }
        getCurrentTrackStatus(currentState) {
            var _a, _b;
            this.lastState = currentState;
            return {
                trackId: this.getCurrentTrackId(),
                isStream:!!((_a = this.currentTrack) === null || _a === void 0? void 0 : _a.isStream),
                currentIndex: this.getCurrentIndex(),
                status: currentState,
                currentPosition: (_b = this.audio) === null || _b === void 0? void 0 : _b.currentTime,
            };
        }
        // more internal methods
        setCurrent(item, position) {
            return __awaiter(this, void 0, void 0, function* () {
                let wasPlaying = false;
                if (this.audio) {
                    wasPlaying =!this.audio.paused;
                    this.audio.pause();
                    this.audio.src = "";
                    this.audio.removeAttribute('src');
                    this.audio.load();
                }
                this.audio = document.createElement('video');
                this.currentTrack = item;
                if (item.assetUrl.includes('.m3u8')) {
                    yield this.loadHlsJs();
                    const hls = new Hls({
                        autoStartLoad: true,
                        debug: false,
                        enableWorker: true,
                    });
                    hls.attachMedia(this.audio);
                    hls.on(Hls.Events.MEDIA_ATTACHED, () => {
                        hls.loadSource(item.assetUrl);
                    });
                    //this.registerHlsListeners(hls, position);
                }
                else {
                    this.audio.src = item.assetUrl;
                }
                yield this.registerHtmlListeners(position);
                if (wasPlaying) {
                    this.audio.addEventListener('canplay', () => {
                        this.play();
                    });
                }
                this.notifyListeners("status", {
                    action: "status",
                    status: {
                        msgType: exports.RmxAudioStatusMessage.RMXSTATUS_TRACK_CHANGED,
                        trackId: this.getCurrentTrackId(),
                        value: {
                            currentItem: item
                        }
                    }
                });
            });
        }
        log(message,...optionalParams) {
            if (this.options.verbose) {
                console.log(message,...optionalParams);
            }
        }
        loadHlsJs() {
            if (this.hlsLoaded) {
                return Promise.resolve();
            }
            return new Promise((resolve, reject) => {
                var script = document.createElement('script');
                script.type = 'text/javascript';
                script.src = 'https://cdn.jsdelivr.net/npm/hls.js@0.9.1';
                document.getElementsByTagName('head')[0].appendChild(script);
                script.onload = () => {
                    this.hlsLoaded = true;
                    resolve();
                };
                script.onerror = () => {
                    reject();
                };
            });
        }
    }
    const Playlist = new PlaylistWeb();
    core.registerWebPlugin(Playlist);

    const Playlist$1 = core.Plugins.PlaylistPlugin;
    const log = console;
    const itemStatusChangeTypes = [
        exports.RmxAudioStatusMessage.RMXSTATUS_PLAYBACK_POSITION,
        exports.RmxAudioStatusMessage.RMXSTATUS_DURATION,
        exports.RmxAudioStatusMessage.RMXSTATUS_BUFFERING,
        exports.RmxAudioStatusMessage.RMXSTATUS_CANPLAY,
        exports.RmxAudioStatusMessage.RMXSTATUS_LOADING,
        exports.RmxAudioStatusMessage.RMXSTATUS_LOADED,
        exports.RmxAudioStatusMessage.RMXSTATUS_PAUSE,
        exports.RmxAudioStatusMessage.RMXSTATUS_COMPLETED,
        exports.RmxAudioStatusMessage.RMXSTATUS_ERROR,
    ];
    /**
     * AudioPlayer class implementation. A singleton of this class is exported for use by Cordova,
     * but nothing stops you from creating another instance. Keep in mind that the native players
     * are in fact singletons, so the only thing the separate instance gives you would be
     * separate onStatus callback streams.
     */
    class RmxAudioPlayer {
        /**
         * Creates a new RmxAudioPlayer instance.
         */
        constructor() {
            this.handlers = {};
            this.options = { verbose: false, resetStreamOnPause: true };
            this._inititialized = false;
            this._currentState = 'unknown';
            this._hasError = false;
            this._hasLoaded = false;
            this._currentItem = null;
            /**
             * Player interface
             */
            /**
             * Returns a promise that resolves when the plugin is ready.
             */
            this.ready = () => {
                return this._initPromise;
            };
            this.initialize = () => {
                Playlist$1.addListener('status', (data) => {
                    if (data.action ==='status') {
                        this.onStatus(data.status.trackId, data.status.msgType, data.status.value);
                    }
                    else {
                        console.warn('Unknown audio player onStatus message:', data);
                    }
                });
                Playlist$1.initialize()
                   .then(() => {
                    this._inititialized = true;
                    this._readyResolve(true);
                })
                   .catch((args) => {
                    const message = 'CORDOVA RMXAUDIOPLAYER: Error storing message channel:';
                    