AVPlayer在完成播放时退出全屏

时间:2014-12-09 13:37:35

标签: ios objective-c avplayer

我使用AVPlayer来显示一些视频流(具有固定长度)。问题是当内容完成时,播放器(如果用户已将其设置为全屏)仍然保持全屏。

任何方法可以让玩家在内容播放结束后立即进入最小化状态?

3 个答案:

答案 0 :(得分:1)

再加上龙陇的回答,

您可以在itemDidFinishPlaying函数中使用dismissViewControllerAnimated关闭视频播放器。你还应该记得在完成后删除你创建的观察者。

@font-face{
  font-family: "coolvetica"; /* You can name the font whatever you like here */
  src: url('fonts/coolvetica rg.ttf') format('truetype'); /* Font directory and format */
}


#header {
    margin-right: 50px;
    height: 6%;
    width: 100%;
    border-radius: 3px;
    background-color: #FA5858;
}

#logo {
    height: 60px;
    width: 250px;

}

#steamLogon {
    float: right;
    margin-top: 0px;
}

#navbar {
    float: right;
    margin-top: 13px;
    margin-right: 100px;
}

#navbar a {
    margin-left: 50px;
    height: 20px;
    width: 70px;
    display: align-block;
    font-weight: 200;
    font-size: 22px;
    text-align: center;
    text-decoration: none;
    color: white;
    font-family: coolvetica;
}

#navbar .dropdown {
    margin-left: 50px;
    height: 20px;
    width: 70px;
    display: align-block;
    font-weight: 200;
    font-size: 22px;
    text-align: center;
    text-decoration: none;
    color: white;
    font-family: coolvetica;
}

.logout {
    font-family: coolvetica;
    background-color: #e6e6e6;
    font-weight: 300;
    height: 25px;
    width: 160px;
    border: 2px solid #f2f2f2;
}

.logout:hover {
    font-family: coolvetica;
    font-weight: 300;
    font-size: 14px;
    color: #262626;
    height: 25px;
    width: 160px;
    background-color: #FA5858;
    border: 2px solid #f2f2f2;
}

.account {
    font-family: coolvetica;
    font-size: 14px;
    color: #262626;
    height: 20px;
    margin: auto;
    width: 153px;
    background-color: #e6e6e6;
    border: 2px solid #f2f2f2;
}

.account:hover {
    font-family: coolvetica;
    font-size: 14px;
    color: #262626;
    height: 20px;
    width: 153px;
    background-color: #FA5858;
    border: 2px solid #f2f2f2;
}

/* Dropdown Button */
.dropdown-content {
    display: none;
    border-radius: 3px;
    position: absolute;
    height: 45px;
    background-color: #f9f9f9;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}

.dropdown-content a:hover {background-color: #f1f1f1}

.dropdown-toggle:hover .dropdown-content {
    display: block;
}


#diceholder {
    height: 50%;
    width: 85%;
    margin: auto;
    margin-top: 100px;
    background-color: #404040;


}

/* Account.php */

    .trade-url {
    display: align-block;
    font-family: coolvetica;
    font-size: 24px;
    color: #1a1a1a;
    margin-top: 50px;
    text-align: center;
    margin-left: auto;
    margin-right: auto;



}

答案 1 :(得分:1)

适用于#iOS 10版及更高版本

    # declare your player and 
    var player = AVPlayer()
    var playerVC = AVPlayerViewController()


    let item = AVPlayerItem(url: module.fileURL!)
    DispatchQueue.main.async(execute: {
        self.playerVC.view.backgroundColor = UIColor.clear
        self.player.replaceCurrentItem(with: item)
        try? AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
        self.playerVC.player = self.player

        self.player.play()

        self.playerVC.showsPlaybackControls = true
        if #available(iOS 11.0, *) {
            self.playerVC.exitsFullScreenWhenPlaybackEnds = true
        }
        self.playerVC.view.frame = self.videoView.bounds
        NotificationCenter.default.addObserver(self, selector: #selector(self.playerItemDidReachEnd(notification:)), name: .AVPlayerItemDidPlayToEndTime, object:self.playerVC.player!.currentItem)
        self.videoView.addSubview((self.playerVC.view)!)

})

收听视频文件的结尾

  @objc func playerItemDidReachEnd(notification: Notification?) {
    print("Finishing")
    dismiss(animated: true, completion: nil)
}

答案 2 :(得分:0)

收听视频文件的结尾并在结尾处关闭/调整大小/重新启动视图

-(void)startPlaybackForItemWithURL:(NSURL*)url {

 // First create an AVPlayerItem
 AVPlayerItem* playerItem = [AVPlayerItem playerItemWithURL:url];

 // Subscribe to the AVPlayerItem's DidPlayToEndTime notification.
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];

// Begin playback
[player play]

} 

-(void)itemDidFinishPlaying:(NSNotification *) notification {
 // Will be called when AVPlayer finishes playing playerItem

}