在应用程序后台实现视频文件播放的更好方法是什么?

What is the better way to implement video files playback in application background?

我确实需要实施 iOS 具有在后台播放视频文件的应用程序。

例如首先 viewController 应在背景中播放一个视频,而不是某些照片或彩色背景。 第二个 viewController 另一个视频应该在背景中播放,而不是一些照片或彩色背景。 等等。

实现这个的最佳方法是什么? * 在项目中导入那些视频文件是不是更好? * 还是将它们存储在某个外部位置并通过网络播放更好?

从 AppStore 批准的角度和 Apple 指南的角度来看 - 这种情况下的视频是否正确?或者最好避免在移动应用程序中使用视频?

提前致谢。

找到了通过 AVFoundation

的原生 AVPlayer 播放本地视频文件的解决方案

1.Import AVFoundation:

#import <AVFoundation/AVFoundation.h>

2.Use 属性 玩家:

@property (nonatomic) AVPlayer *avPlayer;

3.Add 视频文件到 "Video" 文件夹并添加 "Video" 到项目

4.Initialize玩家

NSString *filepath = [[NSBundle mainBundle] pathForResource:@"shutterstock_v885172.mp4" ofType:nil inDirectory:@"Video"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
self.avPlayer = [AVPlayer playerWithURL:fileURL];
self.avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;

AVPlayerLayer *videoLayer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer];
videoLayer.frame = self.view.bounds;
videoLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[self.view.layer addSublayer:videoLayer];

[self.avPlayer play];

5.Subscribe 事件 - 视频播放到最后

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:[self.avPlayer currentItem]]; 

6.Resume视频播放到开头相关方法

- (void)itemDidFinishPlaying:(NSNotification *)notification {
    AVPlayerItem *player = [notification object];
    [player seekToTime:kCMTimeZero];
}