目標は、コントロールなしで UIView 内でビデオ ファイル (*.mp4) を再生することです。
これは、ViewController やその他のコントロール (テーブルビュー、テキスト フィールドなど) の背景/壁紙として機能し、ビデオが再生されると、画像がビュー上に表示されます。
これを行うより良い方法は何ですか?ありがとうございます
ベストアンサー1
ネイティブの目標に到達しましたAVPlayer
1.使用したAVFoundation:
#import <AVFoundation/AVFoundation.h>
2.プレイヤーが使用するプロパティ:
@property (nonatomic) AVPlayer *avPlayer;
3.ビデオファイルを「ビデオ」フォルダに追加し、「ビデオ」をプロジェクトに追加しました
4.プレーヤーを初期化しました
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.イベントに登録しました - ビデオは最後まで再生されました
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:[self.avPlayer currentItem]];
6.関連方法でビデオの再生を最初から再開する
- (void)itemDidFinishPlaying:(NSNotification *)notification {
AVPlayerItem *player = [notification object];
[player seekToTime:kCMTimeZero];
}