收藏
回答

wx.createInnerAudioContext seek 跳转一直卡在一个时间点?

音频可以正常播放,快进10秒后,第一次可以。但只要超过了28秒 。 就会一直从28秒开始播放?


调式基础库3.4.0

IOS 16.1.1

微信版本 8.0.48


真机预览 -演示视频

https://ortronimage.oss-cn-shanghai.aliyuncs.com/111.mp4


代码

<!--index.wxml-->
<scroll-view class="scrollarea" scroll-y type="list">
    <view class="container">
        <view class="container">
            <view class="controls">
                <button class="control-btn" bindtap="backward">快退</button>
                <button class="control-btn" bindtap="togglePlay">
                    {{isPlaying ? '暂停' : '播放'}}
                </button>
                <button class="control-btn" bindtap="forward">快进</button>
            </view>


            <view class="slider-container" style="width: 600rpx;">
                <slider class="slider" value="{{sliderValue}}" min="0" max="{{duration}}" bindchange="handleSliderChange" />
                <view class="progress">
                    当前进度:{{sliderValue}}s
                </view>
                <view class="duration">
                    总时长:{{duration}}
                </view>
            </view>
        </view>


    </view>


</scroll-view>


// index.ts
const audioSrc = 'https://ortronimage.oss-cn-shanghai.aliyuncs.com/1oodxp58wi%20(2).mp3';


let audioContext: any = null;


Page({
    data: {
        sliderValue: 0,
        isPlaying: false,
        duration: 540,
    },


    onLoad() {
        audioContext = wx.createInnerAudioContext();
        audioContext.src = audioSrc;


        audioContext.onCanplay(() => {
            console.log('可以播放');
            audioContext.play(); // 自动开始播放
            this.setData({ isPlaying: true, duration: 540 });
        });


        audioContext.onTimeUpdate(() => {
            const currentTime = audioContext.currentTime;
            this.setData({ sliderValue: currentTime });
        });


        audioContext.onEnded(() => {
            this.setData({ isPlaying: false });
        });


        audioContext.onSeeked(() => {
            console.log('跳转完成');
            audioContext.play();
        });


        audioContext.onWaiting(() => {
            console.log('等待中');
        });


        audioContext.onCanplay(() => {
            console.log('可以播放');
        });


        audioContext.onError((error: any) => {
            console.error('播放失败:', error);
        });
    },


    togglePlay() {
        if (this.data.isPlaying) {
            audioContext.pause();
            this.setData({ isPlaying: false });
        } else {
            audioContext.play();
            this.setData({ isPlaying: true });
        }
    },


    handleSliderChange(e) {
        const { value } = e.detail;
        console.log(value);
        audioContext.seek(value);
    },


    onUnload() {
        audioContext.stop();
        audioContext.destroy();
    },


    backward() {
        audioContext.seek(audioContext.currentTime - 10);
    },


    forward() {
        audioContext.seek(audioContext.currentTime + 10);
    },
});
回答关注问题邀请回答
收藏

1 个回答

登录 后发表内容