【问题标题】:AVPlayer not showing video in iOS7, working fine on iOS8 and iOS9AVPlayer 在 iOS7 中不显示视频,在 iOS8 和 iOS9 上运行良好
【发布时间】:2016-01-26 10:30:43
【问题描述】:

这是我的代码,

@interface ViewController ()
{
    AVPlayer *avPlayer;
}
@end

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    UIView *containerView = [[UIView alloc] initWithFrame:self.view.frame];
    NSString *filepath = [[NSBundle mainBundle] pathForResource:@"TestVideo" ofType:@"mp4"];
    AVAsset *asset = [AVAsset assetWithURL:[NSURL fileURLWithPath:filepath]];
    AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithAsset:asset];
    avPlayer = [AVPlayer playerWithPlayerItem:playerItem];
    AVPlayerLayer *avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:avPlayer];

    avPlayerLayer.frame = self.view.frame;
    [containerView.layer addSublayer:avPlayerLayer];
    [self.view addSubview:containerView];
    [avPlayer play];
}

【问题讨论】:

    标签: ios objective-c ios7 avplayer


    【解决方案1】:

    这真的不应该在任何 iOS 版本上工作,因为

    AVPlayer *avPlayer = [AVPlayer playerWithPlayerItem:playerItem];
    

    超出范围并在viewDidAppear 结束时被释放。

    您应该将AVPlayer 分配给类成员变量,以阻止这种情况发生。

    否则视频文件可能有问题。

    【讨论】:

    • 谢谢,但更新的代码也对我不起作用。 (请看更新)
    • 我猜这是 iOS7 特有的问题,因为它在 iOS8 和 9 上运行良好。
    • 你有问题文件的 URL 吗?
    • 这是我的文件路径:/Users/zaid.pathan/Library/Developer/CoreSimulator/Devices/C595D53B-EC6E-4BC2-9‌​59D-B6FDDC8CA7BD/data/Containers/Bundle/Application/0E569CF4-E153-46E1-AA86-D2107‌​45287A9/AVPlayerTestiOS7.app/TestVideo.mp4
    • 分享文件,可能问题出在这
    【解决方案2】:

    您的 AVPlayer 项目需要一个强引用,因为它会自动设置为 nil

    请更改

    @interface ViewController ()
    
    {
        AVPlayer *avPlayer;
    }
    @end
    

    到这里

    @interface ViewController ()  
    @property (strong, nonatomic) AVPlayer *avPlayer;
    @end
    

    并将所有avPlayer 引用更改为self.avPlayer 我希望这会有所帮助。

    【讨论】:

    • 不,这对我没有帮助。我猜这是 iOS7 特有的问题,因为它在 iOS8 和 9 上运行良好。
    • 您还有什么建议吗?
    猜你喜欢
    • 2014-11-16
    • 2014-11-26
    • 1970-01-01
    • 1970-01-01
    • 2022-09-29
    • 1970-01-01
    • 2015-10-02
    • 1970-01-01
    • 2016-05-14
    相关资源
    最近更新 更多