Objc - 如何在这样的UIView中添加视频播放器?

问题描述 投票:3回答:3

这个播放器只有2个按钮,播放/暂停和全屏,它看起来像一个system player,如此快速和简单。

我在UIView中使用了UIWebView and HTML5,但是UIWebView非常慢,没有全屏就无法播放。

如果这个玩家是UIObject或一些UIView的东西,我怎么能添加它?

谢谢〜

objective-c video mpmovieplayercontroller video-player
3个回答
5
投票

您可以使用UIView将视频播放器添加到MPMoviePlayerController中。

这是Apple示例代码,就像这样。它可以从互联网流式传输视频以及播放本地电影文件。

您可以通过更改moviePlayer controlStyle属性来更改播放控件。

您可以从以下链接中找到示例代码

http://developer.apple.com/library/ios/#samplecode/MoviePlayer_iPhone/Introduction/Intro.html


13
投票

从iOS 9不推荐使用MPMoviePlayerController。您现在可以使用AVPlayerViewController将视频插入iOS项目中。

例如:

#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>

...

@property (nonatomic, retain) AVPlayerViewController *playerViewController;

...

- (void)viewDidLoad
{

    [super viewDidLoad];

    NSString *stringVideoName = @"video.m4v";
    NSString *stringVideoPath = [[NSBundle mainBundle] pathForResource:stringVideoName ofType:nil];
    NSAssert(stringVideoPath, @"Expected not nil video file");

    NSURL *urlVideoFile = [NSURL fileURLWithPath:stringVideoPath];
    NSAssert(urlVideoFile, @"Expected not nil video url");

    _playerViewController = [[AVPlayerViewController alloc] init];
    _playerViewController.player = [AVPlayer playerWithURL:urlVideoFile];
    _playerViewController.view.frame = self.view.bounds;
    _playerViewController.showsPlaybackControls = YES;

    [self.view addSubview:_playerViewController.view];
    self.view.autoresizesSubviews = YES;

}

0
投票

进口以下框架

#import <MediaPlayer/MediaPlayer.h>
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>
#import <UIKit/UIKit.h>

在UIView或UIViewController中添加以下代码类videoView是UIView

    // Url of Video
    NSURL *videoURL = [NSURL URLWithString:@"urlOfVideo"];

    // Init Player
    AVPlayer *player = [AVPlayer playerWithURL:videoURL];
    AVPlayerViewController *playerViewController = [AVPlayerViewController new];

    // Add Player in AVPlayerViewController
    playerViewController.player = player;
    playerViewController.showsPlaybackControls = true;

    // Set Frame accoding to view
    playerViewController.view.frame = _videoView.bounds;

    //Used to Play On start
    [playerViewController.player play];

    // Add Video in UIView
    [_videoView addSubview:playerViewController.view];

我希望它对你有用......

© www.soinside.com 2019 - 2024. All rights reserved.