mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-21 21:01:20 -08:00
feat: update version to 1.0.1 and enhance Chromecast plugin functionality
This commit is contained in:
1 parent
c9d2d6c194
commit
a6f0dc50e5
4 files changed
+125
-86
No files matched your search
@@ -145,16 +145,15 @@
|
||||
})({"NDQPH":[function(require,module,exports) {
|
||||
var parcelHelpers = require("@parcel/transformer-js/src/esmodule-helpers.js");
|
||||
parcelHelpers.defineInteropFlag(exports);
|
||||
function loadScript(src) {
|
||||
return new Promise((resolve, reject)=>{
|
||||
parcelHelpers.export(exports, "default", ()=>artplayerPluginChromecast);
|
||||
const loadScript = (src)=>new Promise((resolve, reject)=>{
|
||||
const script = document.createElement("script");
|
||||
script.src = src;
|
||||
script.onload = resolve;
|
||||
script.onerror = reject;
|
||||
document.body.appendChild(script);
|
||||
});
|
||||
}
|
||||
function getMimeType(url) {
|
||||
const getMimeType = (url)=>{
|
||||
const extension = url.split("?")[0].split("#")[0].split(".").pop().toLowerCase();
|
||||
const mimeTypes = {
|
||||
mp4: "video/mp4",
|
||||
@@ -171,49 +170,67 @@ function getMimeType(url) {
|
||||
m3u8: "application/x-mpegURL"
|
||||
};
|
||||
return mimeTypes[extension] || "application/octet-stream";
|
||||
}
|
||||
};
|
||||
function artplayerPluginChromecast(option) {
|
||||
const DEFAULT_ICON = `<svg height="20" width="20" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 576 512"><path d="M512 96H64v99c-13-2-26.4-3-40-3H0V96C0 60.7 28.7 32 64 32H512c35.3 0 64 28.7 64 64V416c0 35.3-28.7 64-64 64H288V456c0-13.6-1-27-3-40H512V96zM24 224c128.1 0 232 103.9 232 232c0 13.3-10.7 24-24 24s-24-10.7-24-24c0-101.6-82.4-184-184-184c-13.3 0-24-10.7-24-24s10.7-24 24-24zm8 192a32 32 0 1 1 0 64 32 32 0 1 1 0-64zM0 344c0-13.3 10.7-24 24-24c75.1 0 136 60.9 136 136c0 13.3-10.7 24-24 24s-24-10.7-24-24c0-48.6-39.4-88-88-88c-13.3 0-24-10.7-24-24z"/></svg>`;
|
||||
const DEFAULT_SDK = "https://www.gstatic.com/cv/js/sender/v1/cast_sender.js?loadCastFramework=1";
|
||||
window["__onGCastApiAvailable"] = function(isAvailable) {
|
||||
artplayerPluginChromecast.isAvailable = isAvailable;
|
||||
if (isAvailable) cast.framework.CastContext.getInstance().setOptions({
|
||||
receiverApplicationId: chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID,
|
||||
autoJoinPolicy: chrome.cast.AutoJoinPolicy.ORIGIN_SCOPED
|
||||
let isCastInitialized = false;
|
||||
let castSession = null;
|
||||
const initializeCastApi = ()=>{
|
||||
return new Promise((resolve, reject)=>{
|
||||
window["__onGCastApiAvailable"] = (isAvailable)=>{
|
||||
if (isAvailable) {
|
||||
const context = window.cast.framework.CastContext.getInstance();
|
||||
context.setOptions({
|
||||
receiverApplicationId: window.chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID,
|
||||
autoJoinPolicy: window.chrome.cast.AutoJoinPolicy.ORIGIN_SCOPED
|
||||
});
|
||||
context.addEventListener(window.cast.framework.CastContextEventType.SESSION_STATE_CHANGED, (event)=>{
|
||||
castSession = event.session;
|
||||
updateCastButton();
|
||||
});
|
||||
isCastInitialized = true;
|
||||
resolve();
|
||||
} else reject(new Error("Cast API is not available"));
|
||||
};
|
||||
if (!window.chrome || !window.chrome.cast) loadScript(option.sdk || DEFAULT_SDK).catch(reject);
|
||||
});
|
||||
};
|
||||
const castVideo = (art, session)=>{
|
||||
const url = option.url || art.option.url;
|
||||
const mediaInfo = new window.chrome.cast.media.MediaInfo(url, option.mimeType || getMimeType(url));
|
||||
const request = new window.chrome.cast.media.LoadRequest(mediaInfo);
|
||||
session.loadMedia(request).then(()=>art.notice.show = "Casting started").catch((error)=>{
|
||||
art.notice.show = "Error casting media";
|
||||
throw error;
|
||||
});
|
||||
};
|
||||
const updateCastButton = ()=>{
|
||||
const button = document.querySelector(".art-icon-cast");
|
||||
if (button) button.style.color = castSession ? "red" : "white";
|
||||
};
|
||||
return async (art)=>{
|
||||
await loadScript(option.sdk || DEFAULT_SDK);
|
||||
function castVideo(session) {
|
||||
const url = option.url || art.option.url;
|
||||
const mimeType = option.mimeType || getMimeType(url);
|
||||
const mediaInfo = new chrome.cast.media.MediaInfo(url, mimeType);
|
||||
const request = new chrome.cast.media.LoadRequest(mediaInfo);
|
||||
session.loadMedia(request).then(function() {
|
||||
console.log("Media loaded successfully");
|
||||
}, function(error) {
|
||||
art.notice.show = "Error casting media";
|
||||
console.log("Error casting media", error);
|
||||
});
|
||||
}
|
||||
art.controls.add({
|
||||
name: "chromecast",
|
||||
position: "right",
|
||||
tooltip: "Chromecast",
|
||||
html: `<i class="art-icon art-icon-cast">${option.icon || DEFAULT_ICON}</i>`,
|
||||
click () {
|
||||
if (!artplayerPluginChromecast.isAvailable) {
|
||||
art.notice.show = "Cast API is not available";
|
||||
return;
|
||||
click: async ()=>{
|
||||
if (!isCastInitialized) try {
|
||||
await initializeCastApi();
|
||||
} catch (error) {
|
||||
art.notice.show = "Failed to initialize Cast API";
|
||||
throw error;
|
||||
}
|
||||
const castSession = cast.framework.CastContext.getInstance().getCurrentSession();
|
||||
if (castSession) castVideo(castSession);
|
||||
else cast.framework.CastContext.getInstance().requestSession().then(function(session) {
|
||||
castVideo(session);
|
||||
}, function(error) {
|
||||
const context = window.cast.framework.CastContext.getInstance();
|
||||
if (castSession) castVideo(art, castSession);
|
||||
else try {
|
||||
const session = await context.requestSession();
|
||||
castVideo(art, session);
|
||||
} catch (error) {
|
||||
art.notice.show = "Error connecting to cast session";
|
||||
console.log("Error connecting to cast session", error);
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
});
|
||||
return {
|
||||
@@ -221,7 +238,6 @@ function artplayerPluginChromecast(option) {
|
||||
};
|
||||
};
|
||||
}
|
||||
exports.default = artplayerPluginChromecast;
|
||||
if (typeof window !== "undefined") window["artplayerPluginChromecast"] = artplayerPluginChromecast;
|
||||
|
||||
},{"@parcel/transformer-js/src/esmodule-helpers.js":"5dUr6"}],"5dUr6":[function(require,module,exports) {
|
||||
@@ -237,7 +253,7 @@ exports.defineInteropFlag = function(a) {
|
||||
};
|
||||
exports.exportAll = function(source, dest) {
|
||||
Object.keys(source).forEach(function(key) {
|
||||
if (key === "default" || key === "__esModule" || dest.hasOwnProperty(key)) return;
|
||||
if (key === "default" || key === "__esModule" || Object.prototype.hasOwnProperty.call(dest, key)) return;
|
||||
Object.defineProperty(dest, key, {
|
||||
enumerable: true,
|
||||
get: function() {
|
||||
|
||||
Reference in new issue
Block a user