Skip to content
Please note that GitHub no longer supports Internet Explorer.

We recommend upgrading to the latest Microsoft Edge, Google Chrome, or Firefox.

Learn more

Instantly share code, notes, and snippets.

@cyanzhong cyanzhong/player.js
Last active Jan 27, 2020

Embed
What would you like to do?
JSBox video player based on AVPlayerViewController
const frameworks = ["AVFoundation", "AVKit"];
frameworks.forEach(name => {
$objc("NSBundle").$bundleWithPath(`/System/Library/Frameworks/${name}.framework`).$load();
});
const gravities = {
resize: "AVLayerVideoGravityResize",
resizeAspect: "AVLayerVideoGravityResizeAspect",
resizeAspectFill: "AVLayerVideoGravityResizeAspectFill",
}
function play(src, {
showsPlaybackControls = true,
videoGravity = "resizeAspect",
allowsPictureInPicturePlayback = true,
updatesNowPlayingInfoCenter = true,
entersFullScreenWhenPlaybackBegins = false,
exitsFullScreenWhenPlaybackEnds = false,
} = {}) {
const url = (() => {
if (/^(http|https):\/\//i.test(src)) {
return $objc("NSURL").$URLWithString(src);
} else {
return $objc("NSURL").$fileURLWithPath($file.absolutePath(src));
}
})();
const player = $objc("AVPlayer").$playerWithURL(url);
player.$play();
const playerVC = $objc("AVPlayerViewController").$new();
playerVC.$setPlayer(player);
playerVC.$setShowsPlaybackControls(showsPlaybackControls);
playerVC.$setVideoGravity(gravities[videoGravity]);
playerVC.$setAllowsPictureInPicturePlayback(allowsPictureInPicturePlayback);
playerVC.$setUpdatesNowPlayingInfoCenter(updatesNowPlayingInfoCenter);
playerVC.$setEntersFullScreenWhenPlaybackBegins(entersFullScreenWhenPlaybackBegins);
playerVC.$setExitsFullScreenWhenPlaybackEnds(exitsFullScreenWhenPlaybackEnds);
const rootVC = $ui.controller.ocValue();
rootVC.$presentViewController_animated_completion(playerVC, true, null);
}
exports.play = play;
// Example (http/https, or local path)
const src = "https://images.apple.com/media/cn/ipad-pro/2017/43c41767_0723_4506_889f_0180acc13482/films/feature/ipad-pro-feature-cn-20170605_1280x720h.mp4";
// Optional, refer: https://developer.apple.com/documentation/avkit/avplayerviewcontroller
const options = {
showsPlaybackControls: true,
videoGravity: "resizeAspect", // resize, resizeAspectFill
allowsPictureInPicturePlayback: true,
updatesNowPlayingInfoCenter: true,
entersFullScreenWhenPlaybackBegins: false,
exitsFullScreenWhenPlaybackEnds: false,
}
play(src, options);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.