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

时间:2012-12-11 08:37:05

标签: objective-c video mpmovieplayercontroller video-player

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

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

如果此播放器是UIObject或某些UIView的东西,我该如何添加它?

感谢〜

sample video player in app Everyday.me

3 个答案:

答案 0 :(得分: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;

}

答案 1 :(得分:5)

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

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

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

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

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

答案 2 :(得分:0)

Improts遵循框架

#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];

我希望它对您有用...