hea*_*kit 1 objective-c avfoundation ipad ios-simulator ios5
我建立了自己的自定义视频播放器(编辑:在这里找到示例代码)
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
Run Code Online (Sandbox Code Playgroud)
和
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
Run Code Online (Sandbox Code Playgroud)
在我的MainViewController.m中调用它 - (void)viewDidLoad
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]];
Run Code Online (Sandbox Code Playgroud)
用方法
- (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];
}
Run Code Online (Sandbox Code Playgroud)
当然也在MainViewController.h中定义
...
#import <CoreGraphics/CoreGraphics.h>
#import "AVMoviePlayerView.h"
@class AVMoviePlayerView;
@class AVPlayer;
...
IBOutlet AVMoviePlayerView *VideoLoop;
AVPlayer* avPlayer;
Run Code Online (Sandbox Code Playgroud)
它在模拟器中工作正常(除了循环,但这是一个不同的问题),但在设备(iPad3)上没有显示视频.
我错过了什么吗?感谢名单!
| 归档时间: |
|
| 查看次数: |
2484 次 |
| 最近记录: |