Skip to content

Instantly share code, notes, and snippets.

@Gandum2077
Last active January 27, 2020 01:54
Show Gist options
  • Save Gandum2077/a0348c7f924180397091fe9a54bdc02a to your computer and use it in GitHub Desktop.
Save Gandum2077/a0348c7f924180397091fe9a54bdc02a to your computer and use it in GitHub Desktop.
改动使其支持播放本地和远程视频 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({
url,
path,
showsPlaybackControls = true,
videoGravity = "resizeAspect",
allowsPictureInPicturePlayback = true,
updatesNowPlayingInfoCenter = true,
entersFullScreenWhenPlaybackBegins = false,
exitsFullScreenWhenPlaybackEnds = false,
}) {
if (!url && !path) return
const player = (path)
? $objc("AVPlayer").$playerWithURL($objc("NSURL").$fileURLWithPath($file.absolutePath(path)))
: $objc("AVPlayer").$playerWithURL($objc("NSURL").$URLWithString(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
const url = "https://images.apple.com/media/cn/ipad-pro/2017/43c41767_0723_4506_889f_0180acc13482/films/feature/ipad-pro-feature-cn-20170605_1280x720h.mp4";
const path = "t.mp4";
play({
url: url,
//path: path,
showsPlaybackControls: true,
videoGravity: "resizeAspect",
allowsPictureInPicturePlayback: true,
updatesNowPlayingInfoCenter: false,
entersFullScreenWhenPlaybackBegins: false,
exitsFullScreenWhenPlaybackEnds: false
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment