InnerAudioContext

    格式iOSAndroid
    flac
    amr
    wma
    ogg
    ape
    mp4
    m4a
    wav
    mp3
    aac
    aiff
    caf

    示例

    请使用百度APP扫码

    InnerAudioContext - 图3

    • 在 js 文件中
    • 在 js 文件中
    1. Page({
    2. onLoad() {
    3. // 每次触发就会注册一次回调事件,所以只需把所有回调写在onLoad中即可
    4. const innerAudioContext = swan.createInnerAudioContext();
    5. innerAudioContext.src = 'https://vd3.bdstatic.com/mda-ic7mxzt5cvz6f4y5/mda-ic7mxzt5cvz6f4y5.mp3';
    6. innerAudioContext.autoplay = false;
    7. innerAudioContext.obeyMuteSwitch = false;
    8. innerAudioContext.onPlay(res => {
    9. swan.showToast({
    10. title: 'play',
    11. icon: 'none'
    12. });
    13. console.log('onPlay', res);
    14. });
    15. innerAudioContext.onPause(res => {
    16. swan.showToast({
    17. title: 'pause',
    18. icon: 'none'
    19. });
    20. console.log('onPause', res);
    21. innerAudioContext.onStop(res => {
    22. swan.showToast({
    23. title: 'stop',
    24. icon: 'none'
    25. console.log('onStop', res);
    26. });
    27. innerAudioContext.onEnded(res => {
    28. swan.showToast({
    29. title: 'end',
    30. icon: 'none'
    31. });
    32. console.log('onEnded', res);
    33. });
    34. innerAudioContext.onTimeUpdate(res => {
    35. console.log('onTimeUpdate', res);
    36. });
    37. innerAudioContext.onError(res => {
    38. swan.showToast({
    39. title: 'error',
    40. icon: 'none'
    41. });
    42. console.log('onError', res);
    43. });
    44. innerAudioContext.onWaiting(res => {
    45. swan.showToast({
    46. title: 'waiting',
    47. });
    48. });
    49. this.innerAudioContext = innerAudioContext;
    50. },
    51. play() {
    52. this.innerAudioContext.play();
    53. },
    54. pause() {
    55. this.innerAudioContext.pause();
    56. },
    57. stop() {
    58. this.innerAudioContext.stop();
    59. },
    60. seek() {
    61. this.innerAudioContext.seek(10);
    62. },
    63. destroy() {
    64. this.innerAudioContext.destroy();
    65. },
    66. offTimeUpdate() {
    67. this.innerAudioContext.offTimeUpdate(res => {
    68. swan.showToast({
    69. title: 'offTimeUpdate',
    70. icon: 'none'
    71. });
    72. console.log('offTimeUpdate', res);
    73. });
    74. }