播放完成后如何自动关闭AVPlayer?

时间:2016-08-26 09:02:00

标签: ios objective-c avplayer avplayerviewcontroller avkit

我有一个使用AVPlayerViewController播放本地视频的应用。 但是当视频播放完毕后我想让它自动关闭(就像以前用过的MPMoviePlayerViewController)。

在发布此问题后,我设法解决了这个问题,这要归功于以下答案,部分来自Apples“指南和示例代码”。

这是对我有用的代码:

#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

3 个答案:

答案 0 :(得分: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];
}

答案 1 :(得分:2)

这应该有效:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<item android:id="@android:id/background">
    <shape>
        <corners android:radius="5dip" />
        <gradient
            android:startColor="#ff9d9e9d"
            android:centerColor="#ff5a5d5a"
            android:centerY="0.75"
            android:endColor="#ff747674"
            android:angle="270"
            />
    </shape>
</item>

<item android:id="@android:id/secondaryProgress">
    <clip>
        <shape>
            <corners android:radius="5dip" />
            <gradient
                android:startColor="#80ffd300"
                android:centerColor="#80ffb600"
                android:centerY="0.75"
                android:endColor="#a0ffcb00"
                android:angle="270"
                />
        </shape>
    </clip>
</item>

<item android:id="@android:id/progress">
    <clip>
        <shape>
            <corners
                android:radius="5dip" />
            <gradient
                android:startColor="@color/primary"
                android:angle="270"
                />
        </shape>
    </clip>
</item>

答案 2 :(得分: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];
}