问题
I built my own custom video player (edit: find example code here) with
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
and
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
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]];
with methods
- (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];
}
and of course defined in MainViewController.h
...
#import <CoreGraphics/CoreGraphics.h>
#import "AVMoviePlayerView.h"
@class AVMoviePlayerView;
@class AVPlayer;
...
IBOutlet AVMoviePlayerView *VideoLoop;
AVPlayer* avPlayer;
It works fine in Simulator (except the loop, but thats a different problem) but on the device (iPad3) no video shows up.
Did I miss anything? Thanx!!!
EDIT: TO make it easier toshow me how stupid I am please find here some esample code. this does work in the simulator but not on device.
回答1:
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!
回答2:
Adding on to @headkit's answer, which did the trick for me...
Open your video file in QuickTime,
Export > 1080p.
Dont forget to change the extension to "mov"!!
回答3:
For others that comes here like me and wants updated specs from Apple docs:
iPhone 7, 6s
Video formats supported: H.264 video up to 4K, 30 frames per second, High Profile level 4.2 with AAC-LC audio up to 160 Kbps, 48kHz, stereo audio in .m4v, .mp4 and .mov file formats; MPEG-4 video up to 2.5 Mbps, 640x480 pixels, 30 frames per second, Simple Profile with AAC-LC audio up to 160 Kbps per channel, 48kHz, stereo audio in .m4v, .mp4 and .mov file formats; Motion JPEG (M-JPEG) up to 35 Mbps, 1280x720 pixels, 30 frames per second, audio in ulaw, PCM stereo audio in .avi file format
iPhone 5, 5s, 6
Video formats supported: H.264 video up to 1080p, 30 frames per second, High Profile level 4.1 with AAC-LC audio up to 160 Kbps, 48kHz, stereo audio in .m4v, .mp4, and .mov file formats; MPEG-4 video up to 2.5 Mbps, 640 by 480 pixels, 30 frames per second, Simple Profile with AAC-LC audio up to 160 Kbps per channel, 48kHz, stereo audio in .m4v, .mp4, and .mov file formats; Motion JPEG (M-JPEG) up to 35 Mbps, 1280 by 720 pixels, 30 frames per second, audio in ulaw, PCM stereo audio in .avi file format
(For updated links search in google iPhone (model number) specs and tap on the result from Apple site. Than scroll down to "video playback")
来源:https://stackoverflow.com/questions/12071668/why-is-my-video-played-in-simulator-but-not-on-device-ipad-using-avfoundation