Created
February 4, 2020 15:03
-
-
Save TheMetaphysicalCrook/257e036b3cd2b02787c6fda27e6e28f6 to your computer and use it in GitHub Desktop.
JSBox video player based on AVPlayerViewController
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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