Skip to main content

my.createInnerAudioContext

createInnerAudioContext

my.createInnerAudioContext là API dùng để tạo ra InnerAudioContext - đối tượng cung cấp cho bạn các phương thức và thuộc tính để phát audio nền (background audio) trên Tini App.

Thuộc tính

Thuộc tínhKiểu dữ liệuGiá trị mặc địnhMô tả
srcstringĐường dẫn đến tập tin chứa audio.
startTimenumber0Thời điểm bắt đầu phát audio. Đơn vị tính: giây.
autoplaybooleanfalseTự động phát audio.
loopbooleanfalseTự động phát lại audio một khi kết thúc.
obeyMuteSwitchbooleantrueCho phép tắt tiếng audio hay ko. Nếu obeyMuteSwitch = true, dẫu có bật Mute thì audio vẫn phát ra âm thanh.
volumnenumber1Điều chỉnh âm lượng. Nhận giá trị từ 0 đến 1.
durationnumberĐộ dài audio. Đơn vị tính: giây. Read only.
currentTimenumberĐang phát đến thời điểm nào trong Audio. Đơn vị tính giây
pausedbooleanAudio bị pause hoặc stop
bufferednumberAudio được buffer (lưu trữ tạm thời) đến thời điểm nào.

Phương thức

Phương thứcMô tả
InnerAudioContext.playPhát một tập tin audio.
InnerAudioContext.pauseTạm ngưng (pause) phát audio. Nếu phát tiếp, sẽ bắt đầu từ thời điểm bị tam ngưng.
InnerAudioContext.stopDừng (stop) phát audio. Nếu phát tiếp, sẽ bắt đầu lại từ đầu.
InnerAudioContext.seekNhảy đến một thời điểm cụ thể trong audio.
InnerAudioContext.destroyLoại bỏ chính đối tượng đó.
InnerAudioContext.onCanplay(callback function) Lắng nghe sự kiện tập tin audio sẵn sàng phát.
InnerAudioContext.offCanplay(callback function) Huỷ bỏ lắng nghe sự kiện tập tin audio sẵn sàng phát.
InnerAudioContext.onPlay(callback function) Lắng nghe sự kiện audio bắt đầu được phát hoặc đuợc phát tiếp sau khi tạm dừng (pause).
InnerAudioContext.offPlay(callback function) Huỷ bỏ lắng nghe sự kiện audio bắt đầu được phát hoặc đuợc phát tiếp sau khi tạm dừng (pause).
InnerAudioContext.onPause(callback function) Lắng nghe sự kiện audio bị tạm dừng (pause).
InnerAudioContext.offPause(callback function) Huỷ bỏ lắng nghe sự kiện audio bị tạm dừng (pause).
InnerAudioContext.onStop(callback function) Lắng nghe sự kiện audio bị ngưng (stop).
InnerAudioContext.offStop(callback function) Huỷ bỏ lắng nghe sự kiện audio bị ngưng (stop).
InnerAudioContext.onEnded(callback function) Lắng nghe sự kiện audio phát đến hết.
InnerAudioContext.offEnded(callback function) Huỷ bỏ lắng nghe sự kiện audio phát đến hết.
InnerAudioContext.onTimeUpdate(callback function) Lắng nghe sự kiện currentTime của audio bị thay đổi.
InnerAudioContext.offTimeUpdate(callback function) Huỷ bỏ lắng nghe sự kiện currentTime của audio bị thay đổi.
InnerAudioContext.onError(callback function) Lắng nghe sự kiện lỗi xuất hiện trong quá trình phát audio.
InnerAudioContext.offError(callback function) Huỷ bỏ lắng nghe sự kiện lỗi xuất hiện trong quá trình phát audio.
InnerAudioContext.onWaiting(callback function) Lắng nghe sự kiện load audio. Sự kiện được kích hoạt khi việc phát audio phải dừng lại để load bởi vì không đủ dữ liệu.
InnerAudioContext.offWaiting(callback function) Huỷ bỏ lắng nghe sự kiện load audio.
InnerAudioContext.onSeeking(callback function) Lắng nghe sự kiện nhảy đến một thời điểm của audio.
InnerAudioContext.offSeeking(callback function) Huỷ bỏ lắng nghe sự kiện nhảy đến một thời điểm trong audio.
InnerAudioContext.onSeeked(callback function) Lắng nghe sự kiện hoàn thành nhảy đến một thời điểm trong audio.
InnerAudioContext.offSeeked(callback function) Huỷ bỏ lắng nghe sự kiện hoàn thành nhảy đến một thời điểm trong video.

Sample Code

const innerAudioContext = wx.createInnerAudioContext();
innerAudioContext.autoplay = true;
innerAudioContext.src =
'https://file-examples-com.github.io/uploads/2017/11/file_example_MP3_5MG.mp3';
innerAudioContext.onPlay(() => {
console.log('Start playback');
});
innerAudioContext.onError((res) => {
console.log(res.errMsg);
console.log(res.errCode);
});