Xcode Objective-C IOS - 如何播放彼此相邻的两个视频?

时间:2022-06-06 11:45:46

I want to play two videos next to each other. When building Mac apps I did this by using a AVPlayer object. It is a view that you could place inside of your ViewController.

我想互相播放两个视频。在构建Mac应用程序时,我使用AVPlayer对象完成了此操作。您可以将视图置于ViewController内部。

But when I tried to find the AVPlayer object in the object library in an IOS project it didn't show up. Only the AVKit Player View Controller did.

但是当我试图在IOS项目中找到对象库中的AVPlayer对象时,它没有显示出来。只有AVKit播放器视图控制器才能做到。

But the AVKit Player View Controller always plays videos full screen in a separate ViewController. I have searched the internet to find a way to display two videos next to each other but so far, I have not found anything useful.

但AVKit播放器视图控制器始终在单独的ViewController中全屏播放视频。我在互联网上搜索了一种方法来显示彼此相邻的两个视频,但到目前为止,我还没有找到任何有用的东西。

So my question is: Is there any way that I can display two videos next to each other in an IOS application?

所以我的问题是:我有什么方法可以在IOS应用程序中相互显示两个视频吗?

3 个解决方案

#1


0  

But the AVKit Player View Controller always plays videos full screen in a separate ViewController

但AVKit播放器视图控制器始终在单独的ViewController中全屏播放视频

That's false. If you want the movie view to appear as a subview, make the AVPlayerViewController a child view controller. Start with the Container View Controller in the storyboard and make an Embed segue to an AVPlayerViewController.

那是假的。如果希望影片视图显示为子视图,请将AVPlayerViewController设置为子视图控制器。从故事板中的Container View Controller开始,并将一个Embed segue转换为AVPlayerViewController。

Or, you could use AVPlayer without an AVPlayerViewController (in code).

或者,您可以在没有AVPlayerViewController的情况下使用AVPlayer(在代码中)。

#2


1  

// ViewController.h

// ViewController.h

@import AVFoundation;
@import AVKit;
@property(nonatomic, readonly) AVPlayerItem *currentItem;

// ViewController.m

// ViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];
     i=0;
     [self videoPlay:[_passdataArr valueForKey:@"videoUrl"]];


}

-(void)videoPlay:(NSArray*)links
{
    NSLog(@"1");
    //create an AVPlayer
    _detailView.hidden=YES;
//    NSMutableArray *videolinksArr=[[NSMutableArray alloc] init];
//    videolinksArr=_videoLinkArr;
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",links]];

    // create a player view controller
    player = [AVPlayer playerWithURL:url];
    AVPlayerViewController *controller = [[AVPlayerViewController alloc] init];

    [self addChildViewController:controller];
    [self.view addSubview:controller.view];

    controller.view.frame = self.bgimg.frame;
    controller.player = player;
    controller.showsPlaybackControls = YES;
    player.closedCaptionDisplayEnabled = NO;
    [player pause];
    [player play];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(AllVideoPlay:) name:AVPlayerItemDidPlayToEndTimeNotification object:_currentItem];



}

-(void)AllVideoPlay:(NSNotification*)aNotification
{
    NSLog(@"2");

    NSArray *temp = _videoLinkArr[i];
    [self videoPlay:temp];
    i++;
}

#3


0  

AVPlayer has been available on iOS since iOS 4. AVPlayer is a controller object that manages the playback of media assets with no UI elements directly tied to it, which is why it is not located in the IB object library. Rather you can build your own UI using AVPlayerLayer our use Apple's standard AVPlayerViewController, which includes default controls and standard playback behavior. The fastest way to get two videos playing side-by-side is to embed two instances of AVPlayerViewController inside another view controller. If you are using storyboards your code could be as short as:

自iOS 4以来,AVPlayer已经在iOS上可用.AVPlayer是一个控制器对象,它管理媒体资产的播放,没​​有直接与其绑定的UI元素,这就是它不在IB对象库中的原因。相反,您可以使用AVPlayerLayer构建自己的UI,我们使用Apple的标准AVPlayerViewController,其中包括默认控件和标准播放行为。让两个视频并排播放的最快方法是在另一个视图控制器中嵌入两个AVPlayerViewController实例。如果您使用的是故事板,那么您的代码可以简短为:

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.destinationViewController isKindOfClass:[AVPlayerViewController class]] && [segue.identifier isEqualToString:@"First Player"])
    {
        AVPlayerViewController *avPlayerViewController = (AVPlayerViewController *)segue.destinationViewController;
        avPlayerViewController.player = [AVPlayer playerWithURL: [NSURL URLWithString:@"http://devstreaming.apple.com/videos/wwdc/2016/703rx8zlfedjfom6l93/703/hls_vod_mvp.m3u8"]];
        [avPlayerViewController.player play];

    } else if ([segue.destinationViewController isKindOfClass:[AVPlayerViewController class]] && [segue.identifier isEqualToString:@"Second Player"])
    {
        AVPlayerViewController *avPlayerViewController = (AVPlayerViewController *)segue.destinationViewController;
        avPlayerViewController.player = [AVPlayer playerWithURL: [NSURL URLWithString:@"http://devstreaming.apple.com/videos/wwdc/2016/703rx8zlfedjfom6l93/704/hls_vod_mvp.m3u8"]];
        [avPlayerViewController.player play];
    }
}

@end

Or something similar. Where most of the work is done by two container views and embed segues to two AVPlayerViewControllers If you are looking to have custom video UI, you can look at Apple's PIP sample code where they show how you can use key value observing to a video player with custom UI.

或类似的东西。大多数工作都是通过两个容器视图完成的,并且将segue嵌入到两个AVPlayerViewControllers中如果您希望拥有自定义视频UI,可以查看Apple的PIP示例代码,其中显示了如何使用键值观察到视频播放器自定义UI。

#1


0  

But the AVKit Player View Controller always plays videos full screen in a separate ViewController

但AVKit播放器视图控制器始终在单独的ViewController中全屏播放视频

That's false. If you want the movie view to appear as a subview, make the AVPlayerViewController a child view controller. Start with the Container View Controller in the storyboard and make an Embed segue to an AVPlayerViewController.

那是假的。如果希望影片视图显示为子视图,请将AVPlayerViewController设置为子视图控制器。从故事板中的Container View Controller开始,并将一个Embed segue转换为AVPlayerViewController。

Or, you could use AVPlayer without an AVPlayerViewController (in code).

或者,您可以在没有AVPlayerViewController的情况下使用AVPlayer(在代码中)。

#2


1  

// ViewController.h

// ViewController.h

@import AVFoundation;
@import AVKit;
@property(nonatomic, readonly) AVPlayerItem *currentItem;

// ViewController.m

// ViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];
     i=0;
     [self videoPlay:[_passdataArr valueForKey:@"videoUrl"]];


}

-(void)videoPlay:(NSArray*)links
{
    NSLog(@"1");
    //create an AVPlayer
    _detailView.hidden=YES;
//    NSMutableArray *videolinksArr=[[NSMutableArray alloc] init];
//    videolinksArr=_videoLinkArr;
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",links]];

    // create a player view controller
    player = [AVPlayer playerWithURL:url];
    AVPlayerViewController *controller = [[AVPlayerViewController alloc] init];

    [self addChildViewController:controller];
    [self.view addSubview:controller.view];

    controller.view.frame = self.bgimg.frame;
    controller.player = player;
    controller.showsPlaybackControls = YES;
    player.closedCaptionDisplayEnabled = NO;
    [player pause];
    [player play];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(AllVideoPlay:) name:AVPlayerItemDidPlayToEndTimeNotification object:_currentItem];



}

-(void)AllVideoPlay:(NSNotification*)aNotification
{
    NSLog(@"2");

    NSArray *temp = _videoLinkArr[i];
    [self videoPlay:temp];
    i++;
}

#3


0  

AVPlayer has been available on iOS since iOS 4. AVPlayer is a controller object that manages the playback of media assets with no UI elements directly tied to it, which is why it is not located in the IB object library. Rather you can build your own UI using AVPlayerLayer our use Apple's standard AVPlayerViewController, which includes default controls and standard playback behavior. The fastest way to get two videos playing side-by-side is to embed two instances of AVPlayerViewController inside another view controller. If you are using storyboards your code could be as short as:

自iOS 4以来,AVPlayer已经在iOS上可用.AVPlayer是一个控制器对象,它管理媒体资产的播放,没​​有直接与其绑定的UI元素,这就是它不在IB对象库中的原因。相反,您可以使用AVPlayerLayer构建自己的UI,我们使用Apple的标准AVPlayerViewController,其中包括默认控件和标准播放行为。让两个视频并排播放的最快方法是在另一个视图控制器中嵌入两个AVPlayerViewController实例。如果您使用的是故事板,那么您的代码可以简短为:

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.destinationViewController isKindOfClass:[AVPlayerViewController class]] && [segue.identifier isEqualToString:@"First Player"])
    {
        AVPlayerViewController *avPlayerViewController = (AVPlayerViewController *)segue.destinationViewController;
        avPlayerViewController.player = [AVPlayer playerWithURL: [NSURL URLWithString:@"http://devstreaming.apple.com/videos/wwdc/2016/703rx8zlfedjfom6l93/703/hls_vod_mvp.m3u8"]];
        [avPlayerViewController.player play];

    } else if ([segue.destinationViewController isKindOfClass:[AVPlayerViewController class]] && [segue.identifier isEqualToString:@"Second Player"])
    {
        AVPlayerViewController *avPlayerViewController = (AVPlayerViewController *)segue.destinationViewController;
        avPlayerViewController.player = [AVPlayer playerWithURL: [NSURL URLWithString:@"http://devstreaming.apple.com/videos/wwdc/2016/703rx8zlfedjfom6l93/704/hls_vod_mvp.m3u8"]];
        [avPlayerViewController.player play];
    }
}

@end

Or something similar. Where most of the work is done by two container views and embed segues to two AVPlayerViewControllers If you are looking to have custom video UI, you can look at Apple's PIP sample code where they show how you can use key value observing to a video player with custom UI.

或类似的东西。大多数工作都是通过两个容器视图完成的,并且将segue嵌入到两个AVPlayerViewControllers中如果您希望拥有自定义视频UI,可以查看Apple的PIP示例代码,其中显示了如何使用键值观察到视频播放器自定义UI。