feat: update player

This commit is contained in:
Rogee
2025-05-09 20:30:22 +08:00
parent c042f1c31a
commit 8b06ffcaad
2 changed files with 40 additions and 44 deletions

View File

@@ -6,5 +6,5 @@ name = "test-http"
type = "tcp" type = "tcp"
localIP = "127.0.0.1" localIP = "127.0.0.1"
#localPort = 8088 #localPort = 8088
localPort = 8088 localPort = 4173
remotePort = 1423 remotePort = 1423

View File

@@ -1,6 +1,5 @@
<script setup> <script setup>
import Player, { Events, I18N } from 'xgplayer'; import Player, { Events, I18N } from 'xgplayer';
import Mp4Plugin from "xgplayer-mp4";
import 'xgplayer/dist/index.min.css'; import 'xgplayer/dist/index.min.css';
import ZH from 'xgplayer/es/lang/zh-cn'; import ZH from 'xgplayer/es/lang/zh-cn';
import MobilePreset from 'xgplayer/es/presets/mobile'; import MobilePreset from 'xgplayer/es/presets/mobile';
@@ -27,42 +26,34 @@ const mediaLoaded = ref(false);
I18N.use(ZH) I18N.use(ZH)
const initializePlayer = async () => { const initializePlayer = async () => {
try { if (player.value) {
return
}
const { data } = await postApi.play(route.params.id); const { data } = await postApi.play(route.params.id);
if (!data.url) { if (!data.url) {
alert("视频地址不存在"); alert("视频地址不存在");
return; return;
} }
try {
player.value = new Player({ player.value = new Player({
id: 'player', id: 'player',
url: data.url, url: data.url,
width: '100%', width: '100%',
fitVideoSize: 'fixWidth', fitVideoSize: 'auto',
videoFillMode: 'fillWidth',
fluid: true, fluid: true,
plugins: [Mp4Plugin],
presets: [MobilePreset], presets: [MobilePreset],
playsinline: true, playsinline: true,
playbackRate: [0.5, 0.75, 1], playbackRate: [0.5, 0.75, 1],
autoplay: true, autoplay: true,
videoFillMode: 'contain', // plugins: [Mp4Plugin],
mp4plugin: { // mp4plugin: {
maxBufferLength: 15, // maxBufferLength: 15,
minBufferLength: 5, // minBufferLength: 5,
// reqOptions: {
// mode: 'cors',
// method: 'POST',
// headers: { // 需要带的自定义请求头
// 'x-test-header': 'rrrr'
// }, // },
// }
// ... 其他配置
},
// 'x5-video-player-type': 'h5',
// 'x5-video-player-fullscreen': false,
poster: article.value.head_images[0], poster: article.value.head_images[0],
}); });
@@ -71,15 +62,16 @@ const initializePlayer = async () => {
}) })
player.value.on(Events.PLAY, async () => { player.value.on(Events.PLAY, async () => {
console.log("player play")
}) })
player.value.on(Events.ENDED, () => { player.value.on(Events.ENDED, async () => {
player.value.destroy(); console.log("player ended")
player.value = null; await updateMediaSource();
}); });
player.value.on(Events.ERROR, (error) => { player.value.on(Events.ERROR, (error) => {
alert(`Player失败: ${error.errorCode} ${error.message} mediaError: ${error.mediaError?.code} ${error.mediaError?.message}`); alert(`视频播放失败: ${error.errorCode} ${error.message} mediaError: ${error.mediaError?.code} ${error.mediaError?.message}`);
}) })
} catch (error) { } catch (error) {
console.error("Failed to load video:", error); console.error("Failed to load video:", error);
@@ -87,6 +79,17 @@ const initializePlayer = async () => {
} }
}; };
const updateMediaSource = async () => {
if (player.value) {
const { data } = await postApi.play(route.params.id);
if (!data.url) {
alert("视频地址不存在");
return;
}
player.value.switchURL(data.url)
}
};
const handleBuy = async () => { const handleBuy = async () => {
if (buying.value) return; if (buying.value) return;
buying.value = true; buying.value = true;
@@ -100,11 +103,11 @@ const handleBuy = async () => {
{ {
...payData, ...payData,
}, },
function (res) { async function (res) {
if (res.err_msg === "get_brand_wcpay_request:ok") { if (res.err_msg === "get_brand_wcpay_request:ok") {
// 支付成功,刷新文章数据 // 支付成功,刷新文章数据
fetchArticle(); fetchArticle();
//TODO: reload media loadVideoSource(); await updateMediaSource();
} else if (res.err_msg === "get_brand_wcpay_request:cancel") { } else if (res.err_msg === "get_brand_wcpay_request:cancel") {
// 用户取消支付 // 用户取消支付
console.log("Payment cancelled"); console.log("Payment cancelled");
@@ -135,7 +138,6 @@ const fetchArticle = async () => {
const { data } = await postApi.show(id); const { data } = await postApi.show(id);
article.value = data; article.value = data;
// 调用微信 JS SDK 分享接口 // 调用微信 JS SDK 分享接口
wx.setShareInfo({ wx.setShareInfo({
title: article.value.title, title: article.value.title,
@@ -183,6 +185,7 @@ onMounted(async () => {
onUnmounted(() => { onUnmounted(() => {
if (player.value) { if (player.value) {
player.value.destroy(); player.value.destroy();
player.value = null;
} }
}); });
</script> </script>
@@ -202,20 +205,13 @@ onUnmounted(() => {
<div class="flex-1"> <div class="flex-1">
<div v-if="article"> <div v-if="article">
<!-- <video webkit-playsinline playsinline ref="videoElement" :poster="article.head_images[0]"
class="w-full aspect-video object-cover plyr-video" preload="none">
<source type="video/mp4" />
Your browser does not support the video tag.
</video> -->
<div id="player"> <div id="player">
<div class="relative flex items-center justify-center bg-gray-100"> <div class="relative flex items-center justify-center bg-gray-100">
<div @click="initializePlayer" <div @click="initializePlayer"
class="absolute shadow shadow-white z-1 rounded-full bg-orange-500 text-white border-2 border-yellow-500 font-bold px-8 py-4"> class="absolute z-1 rounded-full bg-orange-500 text-white border-2 border-yellow-500 font-bold px-4 py-2">
点击播放 点击播放
</div> </div>
<img class="z-0 w-full brightness-50" :src="article.head_images[0]" :alt="article.title" />
<img class="z-0 w-full brightness-50 blur-xs" :src="article.head_images[0]"
:alt="article.title" />
</div> </div>
</div> </div>