0

AVPlayerViewController を使用してローカル ビデオを再生するアプリがあります。しかし、ビデオの再生が終了したら、自動的に閉じるようにします (以前は MPMoviePlayerViewController が行っていたように)。

この質問を投稿した後、以下の回答とAppleの「ガイドとサンプルコード」のおかげで部分的に解決できました。

これは私のために働いたコードです:

#import "ViewController.h"
@import AVFoundation;
@import AVKit;


@interface ViewController ()

@property(nonatomic, readonly) AVPlayerItem *currentItem;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)playerItemDidReachEnd:(NSNotification *) notification {
    //remove the player
   [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)playVideo:(id)sender {

    // grab a local URL to our video
    NSURL *videoURL = [[NSBundle mainBundle]URLForResource:@"bagare" withExtension:@"mp4"];

    // create an AVPlayer
    AVPlayer *player = [AVPlayer playerWithURL:videoURL];

    // create a player view controller
    AVPlayerViewController *controller = [[AVPlayerViewController alloc]init];
    controller.player = player;
    [player play];
    [self presentViewController:controller animated:YES completion:nil];

    // show the view controller
      controller.view.frame = self.view.frame;

    [[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(playerItemDidReachEnd:)
    name:AVPlayerItemDidPlayToEndTimeNotification
    object:_currentItem];

  }

@end
4

3 に答える 3

4
NSURL *url=[NSURL URLWithString:@"URLLINK"];
AVAsset *avAsset = [AVAsset assetWithURL:url];
AVPlayerItem *playerItem = [[AVPlayerItem alloc]initWithAsset:avAsset];

AVPlayerItem の DidPlayToEndTime 通知を購読します。

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

AVPlayerItem を新しいプレーヤーに渡す

 AVPlayer *player = [AVPlayer playerWithURL:videoURL]
   AVPlayerViewController *controller=[[AVPlayerViewController alloc]init];
    controller.player=player;
    [player play];
    [self presentViewController:controller animated:YES completion:nil];

ここで、プレーヤーの却下またはプレーヤーの追加ビューを削除します

-(void)itemDidFinishPlaying:(NSNotification *) notification {
[self dismissViewControllerAnimated:YES completion:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];
}
于 2016-08-26T10:39:34.730 に答える
2

これはうまくいくはずです:

[[NSNotificationCenter defaultCenter] addObserver:self 
    selector:@selector(videoDidFinish:) 
    name:AVPlayerItemDidPlayToEndTimeNotification
    object:[controller.player currentItem]];

- (void)videoDidFinish:(id)notification
{
    AVPlayerItem *p = [notification object];
    //do something with player if you want

    [[NSNotificationCenter defaultCenter] removeObserver:self];

    //fade out / remove subview
}
于 2016-08-26T09:12:24.043 に答える
0

.h ファイル内

@property(nonatomic, strong)AVPlayerViewController *controller;

.m ファイル内:

  - (IBAction)playVideo:(id)sender {

    // grab a local URL to our video
    NSURL *videoURL = [[NSBundle mainBundle]URLForResource:@"bagare" withExtension:@"mp4"];

    // create an AVPlayer
    AVPlayer *player = [AVPlayer playerWithURL:videoURL];

    // create a player view controller
    self.controller = [[AVPlayerViewController alloc]init];
    controller.player = player;
    [player play];
    [self presentViewController:controller animated:YES completion:nil];

    // show the view controller
      controller.view.frame = self.view.frame;

    [[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(playerItemDidReachEnd:)
    name:AVPlayerItemDidPlayToEndTimeNotification
    object:_currentItem];

  }

そして却下のために:

- (void)playerItemDidReachEnd:(NSNotification *)notification {
    [self.controller dismissViewControllerAnimated:YES completion:nil];
}
于 2016-08-26T14:17:20.817 に答える