首页 > 解决方案 > Change AVPlayer Video MacOS Objective-C Cocoa

问题描述

For MacOS, not iOS. I am playing a video in an AVPlayerView in an XIB. Just experimenting with video playback. I would like to be able to choose a selection from the File Menu (For instance File / Videos / Video 1, Video2, Video3, etc) and change the currently playing video in the XIB when I select the menu item.

Currently I am using this to play a video:

- (void)windowDidChangeOcclusionState:(NSNotification *)notification
{
    if (self.window.occlusionState & NSWindowOcclusionStateVisible)
    {
        loopPlayer = YES;
        [_aspectView setAspectRatio:NSMakeSize(16, 9)];
        NSBundle *mb = [NSBundle mainBundle];
        NSURL *demoURL = [mb URLForResource:@"Video1" withExtension:@"mp4"];
        player = [[AVPlayer alloc] initWithURL:demoURL];
        self.playerView.player = player;
        NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
        [nc addObserver:self selector:@selector(movieEndDetected:)
                   name:@"AVPlayerItemDidPlayToEndTimeNotification"
                 object:player.currentItem];
        [player play];
    }
    else
    {
        [player pause];
        [player seekToTime:kCMTimeZero];
    }
}

and to loop the video playing:

- (void) movieEndDetected:(NSNotification *) note
{
    if (loopPlayer) {
        [player seekToTime:kCMTimeZero];
        [player play];
    }
}

But I would like to change the playing video in the XIB on the fly by choosing a menu button. Any ideas how this can be done? I have heard of AVPlayerQueue maybe working for this kind of thing, but I am very new and can't get it working on MacOS.

标签: objective-cswiftxcodemacoscocoa

解决方案


My solution was to use many avplayer views in xibs and when one was open from a menu item, close another.

- (void)Visual02Menu:(id)sender {
         if (!visual02Window) {
         visual02Window = [[Visual02 alloc] initWithWindowNibName:@"Visual02"]; }
        [visual01Window close];
        [visual02Window showWindow:self];

推荐阅读