【问题标题】:Why is my Video played in simulator but not on Device (iPad) using AVFoundation Videoplayer?为什么使用 AVFoundation Videoplayer 在模拟器中播放我的视频,而不是在设备 (iPad) 上播放?
【发布时间】: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) 上没有视频显示。

我错过了什么吗? 谢谢!!!

编辑: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.

【问题讨论】:

标签: objective-c ipad ios5 ios-simulator avfoundation


【解决方案1】:

找到原因 - 我只花了 50 分... iOS 支持高达 2.5 Mbps 的 MPEG-4 视频和 H.264 1080p。我的视频帧太大了!

【讨论】:

    【解决方案2】:

    添加到@headkit 的答案,这对我有用...

    在 QuickTime 中打开您的视频文件,

    导出 > 1080p。

    别忘了把扩展名改成“mov”!!

    【讨论】:

    • 最好把这个作为评论放到答案中,并删除这个答案,因为这个问题已经解决了。
    • 非常感谢 omg,接受的答案解释了问题并暗示了修复,但这确实告诉我要一步一步做什么!
    【解决方案3】:

    对于像我一样来到这里并想要更新规格的其他人from Apple docs

    iPhone 7、6s

    支持的视频格式:最高 4K 的 H.264 视频,每秒 30 帧, High Profile 4.2 级,具有高达 160 Kbps、48kHz、立体声的 AAC-LC 音频 .m4v、.mp4 和 .mov 文件格式的音频; MPEG-4 视频高达 2.5 Mbps,640x480 像素,每秒 30 帧,带 AAC-LC 音频的简单配置文件,每通道高达 160 Kbps,48kHz,.m4v 立体声音频, .mp4 和 .mov 文件格式; Motion JPEG (M-JPEG) 高达 35 Mbps, 1280x720 像素,每秒 30 帧,音频在 ulaw,PCM 立体声音频 .avi 文件格式

    iPhone 5、5s、6

    支持的视频格式:H.264 视频,最高 1080p,每帧 30 帧 第二,High Profile level 4.1,AAC-LC 音频高达 160 Kbps, 48kHz,.m4v、.mp4 和 .mov 文件格式的立体声音频; MPEG-4 视频 高达 2.5 Mbps,640 x 480 像素,每秒 30 帧,简单 带有 AAC-LC 音频的配置文件,每通道高达 160 Kbps,48kHz,立体声 .m4v、.mp4 和 .mov 文件格式的音频;运动 JPEG (M-JPEG) 高达 35 Mbps,1280 x 720 像素,每秒 30 帧,音频输入 ulaw,PCM .avi 文件格式的立体声音频

    (在谷歌iPhone(型号)规格中搜索更新的链接,然后点击 Apple 网站的结果。然后向下滚动到“视频播放”)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多