- 在頁面中使用
<video>
組件來展示視頻,并設置好相應的屬性和事件監聽:<video src="video.mp4" @play="onVideoPlay" @pause="onVideoPause"></video>
- 在頁面的data中定義一個變量來表示是否開啟小窗模式,例如
isPictureInPicture
:data() {return {isPictureInPicture: false}; },
- 在methods中編寫對應的事件處理方法,在需要開啟小窗模式時,通過uni-app提供的API來實現:
methods: {onVideoPlay() {// 當視頻開始播放時,判斷是否需要開啟小窗模式if (this.isPictureInPicture) {uni.createVideoPlayer({videoId: 'video-player',sources: [{src: 'video.mp4'}],autoplay: true,showFullscreenBtn: false,showCenterPlayBtn: false,showPlayBtn: false,pictureInPictureMode: true});}},onVideoPause() {// 當視頻暫停時,關閉小窗模式if (this.isPictureInPicture) {uni.exitPictureInPicture();}} }
在上述代碼中,當視頻開始播放時,判斷
isPictureInPicture
是否為true
,如果是,則調用uni.createVideoPlayer()
方法創建一個視頻播放器,并傳入相應的參數來開啟小窗模式。當視頻暫停時,調用uni.exitPictureInPicture()
方法來關閉小窗模式。