【发布时间】:2012-08-17 18:58:35
【问题描述】:
我用
构建了自己的自定义视频播放器(编辑:find example code here)AVMoviePlayerView.h
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@class AVPlayer;
@interface AVMoviePlayerView : UIView
@property (nonatomic) AVPlayer *player;
- (void)setPlayer:(AVPlayer*)player;
- (void)setVideoFillMode:(NSString *)fillMode;
@end
和
AVMoviePlayerView.m
#import "AVMoviePlayerView.h"
#import <CoreMedia/CoreMedia.h>
@implementation AVMoviePlayerView
+ (Class)layerClass {
return [AVPlayerLayer class];
}
- (AVPlayer*)player
{
return [(AVPlayerLayer*)[self layer] player];
}
- (void)setPlayer:(AVPlayer*)player
{
[(AVPlayerLayer*)[self layer] setPlayer:player];
}
- (void)setVideoFillMode:(NSString *)fillMode
{
AVPlayerLayer *playerLayer = (AVPlayerLayer*)[self layer];
playerLayer.videoGravity = fillMode;
}
@end
在 -(void)viewDidLoad 中的 MainViewController.m 中调用它
NSURL* url = [[NSBundle mainBundle] URLForResource:@"myVideo.264" withExtension:@"mp4"];
self.avPlayer = [AVPlayer playerWithURL:url];
[avPlayer addObserver:self forKeyPath:@"status" options:0 context:AVMoviePlayerViewControllerStatusObservationContext];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[self.avPlayer currentItem]];
方法
- (void)observeValueForKeyPath:(NSString*) path ofObject:(id)object change:(NSDictionary*)change context:(void*)context
{
if (avPlayer.status == AVPlayerStatusReadyToPlay) {
[self.VideoLoop setPlayer:self.avPlayer];
[self.avPlayer play];
}
}
- (void)playerItemDidReachEnd:(NSNotification *)notification {
AVPlayerItem *p = [notification object];
[p seekToTime:kCMTimeZero];
}
当然在 MainViewController.h 中定义
...
#import <CoreGraphics/CoreGraphics.h>
#import "AVMoviePlayerView.h"
@class AVMoviePlayerView;
@class AVPlayer;
...
IBOutlet AVMoviePlayerView *VideoLoop;
AVPlayer* avPlayer;
它在模拟器中运行良好(循环除外,但这是一个不同的问题),但在设备 (iPad3) 上没有视频显示。
我错过了什么吗? 谢谢!!!
【问题讨论】:
-
有趣:使用 NSURL *url = [NSURL URLWithString:@"samkeeneinteractivedesign.com/videos/littleVid3.mp4"]; 适用于 iPad。嗯...
-
在我看来,在某些情况下,视频文件的路径在设备上找不到,但在模拟器上找到。
标签: objective-c ipad ios5 ios-simulator avfoundation