为什么我的视频在模拟器中播放但在使用AVFoundation Videoplayer的设备(iPad)上播放? [英] Why is my Video played in simulator but not on Device (iPad) using AVFoundation Videoplayer?
问题描述
我制作了自己的自定义视频播放器(编辑:在此处查找示例代码 )
I built my own custom video player (edit: find example code here) with
AVMoviePlayerView.h
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
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
调用它从我的MainViewController.m里面 - (void)viewDidLoad
calling it from inside my MainViewController.m within -(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]];
使用方法
- (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中定义
and of course defined in MainViewController.h
...
#import <CoreGraphics/CoreGraphics.h>
#import "AVMoviePlayerView.h"
@class AVMoviePlayerView;
@class AVPlayer;
...
IBOutlet AVMoviePlayerView *VideoLoop;
AVPlayer* avPlayer;
它在模拟器中工作正常(除了循环,但这是一个不同的问题)但在设备上( iPad3)没有视频显示。
It works fine in Simulator (except the loop, but thats a different problem) but on the device (iPad3) no video shows up.
我错过了什么吗?
Thanx !!!
Did I miss anything? Thanx!!!
编辑:为了让我更容易看到我有多愚蠢,请在这里找到一些例子代码。这确实可以在模拟器中使用,但不适用于设备。
推荐答案
找到原因 - 只花了我50分.. 。
iOS支持高达2.5 Mbps的MPEG-4视频和H.264 1080p。我的视频帧尺寸太大了!
Reason found - costs me only 50 points... iOS supports MPEG-4 video up to 2.5 Mbps, and H.264 1080p. My video frame size was too big!
这篇关于为什么我的视频在模拟器中播放但在使用AVFoundation Videoplayer的设备(iPad)上播放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!