Add artplayer-plugin-chromecast to index.html

This commit is contained in:
Harvey Zack committed 2024-04-16 00:45:26 +08:00
1 parent 8f8b69cc25
commit aa5dbd0418
11 files changed
+477

No files matched your search

@@ -0,0 +1,10 @@
node_modules
.DS_Store
yarn.lock
package-lock.json
test
coverage
.nyc_output
.parcel-cache
.cache
docs
@@ -0,0 +1 @@
package-lock=false
@@ -0,0 +1,11 @@
# artplayer-plugin-chromecast
Chromecast plugin for ArtPlayer
## Demo
[https://artplayer.org](https://artplayer.org/?libs=./uncompiled/artplayer-plugin-chromecast/index.js&example=chromecast)
## License
MIT © Harvey Zack
@@ -0,0 +1,24 @@
{
"license": "MIT",
"version": "2.0.0",
"name": "artplayer-plugin-chromecast",
"types": "types/artplayer-plugin-chromecast.d.ts",
"description": "Chromecast plugin for ArtPlayer",
"main": "dist/artplayer-plugin-chromecast.js",
"legacy": "dist/artplayer-plugin-chromecast.legacy.js",
"author": "Harvey Zack <laozhaochaguan@gmail.com>",
"browserslist": "last 1 Chrome version",
"homepage": "https://artplayer.org",
"repository": {
"type": "git",
"url": "git+https://github.com/zhw2590582/ArtPlayer.git"
},
"bugs": {
"url": "https://github.com/zhw2590582/ArtPlayer/issues"
},
"keywords": [
"html5",
"video",
"player"
]
}
@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 576 512"><!--! Font Awesome Pro 6.5.1 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) Copyright 2023 Fonticons, Inc. --><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>

After

Width:  |  Height:  |  Size: 691 B

@@ -0,0 +1,102 @@
function loadScript(src) {
return 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 extension = url.split('?')[0].split('#')[0].split('.').pop().toLowerCase();
const mimeTypes = {
mp4: 'video/mp4',
webm: 'video/webm',
ogg: 'video/ogg',
ogv: 'video/ogg',
mp3: 'audio/mp3',
wav: 'audio/wav',
flv: 'video/x-flv',
mov: 'video/quicktime',
avi: 'video/x-msvideo',
wmv: 'video/x-ms-wmv',
mpd: 'application/dash+xml',
m3u8: 'application/x-mpegURL',
};
return mimeTypes[extension] || 'application/octet-stream';
}
export default 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,
});
}
};
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;
}
const castSession = cast.framework.CastContext.getInstance().getCurrentSession();
if (castSession) {
castVideo(castSession);
} else {
cast.framework.CastContext.getInstance()
.requestSession()
.then(
function (session) {
castVideo(session);
},
function (error) {
art.notice.show = 'Error connecting to cast session';
console.log('Error connecting to cast session', error);
},
);
}
},
});
return {
name: 'artplayerPluginChromecast',
};
};
}
if (typeof window !== 'undefined') {
window['artplayerPluginChromecast'] = artplayerPluginChromecast;
}
@@ -0,0 +1,52 @@
import type Artplayer from 'artplayer';
export = artplayerPluginAds;
export as namespace artplayerPluginAds;
type Option = {
/**
* 广告源文本,支持视频链接、图片链接、HTML文本
*/
source: string;
/**
* 知名广告的类型:'video' | 'image' | 'html'
*/
type: 'video' | 'image' | 'html';
/**
* 广告必看的时长,单位为秒
*/
playDuration?: number;
/**
* 广告总的时长,单位为秒
*/
totalDuration?: number;
/**
* 视频广告是否默认静音
*/
muted?: boolean;
};
type Ads = {
name: 'artplayerPluginAds';
/**
* 跳过广告
*/
skip: () => void;
/**
* 暂停广告
*/
pause: () => void;
/**
* 播放广告
*/
play: () => void;
};
declare const artplayerPluginAds: (option: Option) => (art: Artplayer) => Ads;