首页 > 其他分享 >flutter ios swift avplayer 播放器自动播放下一集

flutter ios swift avplayer 播放器自动播放下一集

时间:2023-12-30 16:33:51浏览次数:21  
标签:flutterAVPlayerViewController filePath ios private player avplayer let urls 自动播放

使用监听监听结束事件

import Foundation
import AVKit
import MediaPlayer
import Flutter

class FlutterAVPlayerView: NSObject, FlutterPlatformView {
    private var _flutterAVPlayerViewController: AVPlayerViewController
    private var urlArray: [URL] = []

    init(frame: CGRect,
         viewIdentifier: CLongLong,
         arguments: Dictionary<String, Any>,
         binaryMessenger: FlutterBinaryMessenger) {
        _flutterAVPlayerViewController = AVPlayerViewController()
        _flutterAVPlayerViewController.viewDidLoad()

        super.init()

        if let urls = arguments["urls"] {
            print("^^^^^^^ ios native av player init urls: \(urls) ^^^^^^^")
            setupPlayerWithURL(urls as! [String])
        } else if let filePath = arguments["file"] {
            print("^^^^^^^ ios native av player init file: \(filePath) ^^^^^^^")
            setupPlayerWithFile(filePath as! String)
        }

    }

    func view() -> UIView {
        return _flutterAVPlayerViewController.view
    }

    private func setupPlayerWithURL(_ urls: [String]) {
        guard urlArray.isEmpty else {
            return
        }

        urlArray = urls.compactMap {
            URL(string: $0)
        }

        _flutterAVPlayerViewController.player = AVPlayer(playerItem: AVPlayerItem(url: URL(string: urls.first ?? "")!))

        NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: nil, queue: .main) { [weak self] _ in
            self?.playNextVideo()
        }

        playNextVideo()

    }

    private func setupPlayerWithFile(_ filePath: String) {
        let appDelegate = UIApplication.shared.delegate as! FlutterAppDelegate
        let vc = appDelegate.window.rootViewController as! FlutterViewController
        let lookUpKey = vc.lookupKey(forAsset: filePath)
        if let path = Bundle.main.path(forResource: lookUpKey, ofType: nil) {
            let item = AVPlayerItem(url: URL(fileURLWithPath: path))
            _flutterAVPlayerViewController.player = AVPlayer(playerItem: item)
        }
    }

    deinit {
        NotificationCenter.default.removeObserver(self)
        _flutterAVPlayerViewController.player?.pause()
    }

    private func playNextVideo() {
        guard let currentPlayer = _flutterAVPlayerViewController.player else {
            return
        }
        
        let nextURL = urlArray.removeFirst()
        print("^^^^^^^ ios native play next video  url: \(nextURL.absoluteString) ^^^^^^^")
        let nextItem = AVPlayerItem(url: nextURL)
        currentPlayer.replaceCurrentItem(with: nextItem)
        currentPlayer.play()
    }
}

标签:flutterAVPlayerViewController,filePath,ios,private,player,avplayer,let,urls,自动播放
From: https://www.cnblogs.com/guanchaoguo/p/17936508.html

相关文章

  • iOSapp开发怎么分享小程序?
    Hello,大家好我是咕噜铁蛋!随着移动互联网的迅猛发展,小程序作为一种新型的应用形态,已经逐渐成为移动开发领域的新宠。对于iOS开发者来说,如何将自己的APP与小程序进行无缝对接,为用户提供更加便捷的服务,成为了一个值得探讨的话题。今天铁蛋讲为大家详细解读iOS开发APP如何分享小程序。......
  • mac安装appium(android/ios)
    一.(android)前提:1.安装java2.安装homebrew3.安装xcode4.安装git5.安装androidsdk官网https://www.androiddevtools.cn/  将下载的AndroidSDK解压,将得到如下目录。(具体安装步骤可以阅读SDKReadme.txt)阅读SDKReadme.txt文档得知:Inordertostartde......
  • iOS 常见问题总结及解决方法
    SDK如何初始化在您需要使用融云SDK功能的类中,import相关头文件。#import<RongIMKit/RongIMKit.h>如果是Swift的话,需要在您工程的Bridging-Header.h文件中加入SDK的引用#import<RongIMKit/RongIMKit.h>请使用您之前从融云开发者控制台注册得到的AppKey,通过RCIM的......
  • VMware ESXi 8.0U2 macOS Unlocker & OEM BIOS 标准版和厂商定制版
    VMwareESXi8.0U2macOSUnlocker&OEMBIOS标准版和厂商定制版ESXi8.0U2标准版,Dell(戴尔)、HPE(慧与)、Lenovo(联想)、Inspur(浪潮)等定制版作者主页:sysin.org2023-09-22,VMwarevSphere8.0U2发布,本站定制镜像相应更新。新增功能详见:VMwarevSphere8Update2新增......
  • Cisco® Catalyst® 8000V 边缘软件(虚拟路由器)IOS XE 17.12.1 发布
    Cisco®Catalyst®8000V边缘软件(虚拟路由器)IOSXE17.12.1发布CiscoCatalyst8000vEdgeSoftware,IOSXEReleaseDublin-17.12.1aEDCisco®Catalyst®8000V边缘软件-虚拟路由器作者主页:sysin.orgCisco®Catalyst®8000V边缘软件(Catalyst8000V)是一款虚拟路由......
  • Cisco Catalyst 8000 边缘平台系列 IOS XE 17.12.01 发布
    CiscoCatalyst8000边缘平台系列IOSXE17.12.01发布CiscoCatalyst8000SeriesEdgePlatforms,IOSXEReleaseDublin-17.12.01aEDCiscoCatalyst8000边缘平台系列作者主页:sysin.orgCiscoCatalyst8000:随心所欲访问位于云、数据中心和边缘的混合型应用和多云应用。特性......
  • Cisco Secure Client 5.1.1.42 (macOS, Linux, Windows & iOS, Andrord) - VPN 和远程
    CiscoSecureClient5.1.1.42(macOS,Linux,Windows&iOS,Andrord)-VPN和远程访问客户端思科安全客户端(包括AnyConnect)作者主页:sysin.orgCiscoSecureClient(includingAnyConnect)思科安全客户端(包括AnyConnect)安全访问只是开始您的团队需要轻松访问公司资源和私有应......
  • VMware ESXi 8.0U2 macOS Unlocker & OEM BIOS 标准版和厂商定制版 2023年12月更新
    VMwareESXi8.0U2macOSUnlocker&OEMBIOS标准版和厂商定制版2023年12月更新ESXi8.0U2标准版,Dell(戴尔)、HPE(慧与)、Lenovo(联想)、Inspur(浪潮)等定制版作者主页:sysin.org2023-12-17更新(驱动和OEM组件更新):DellESXi8.0U2A03和HPEESXi8.0U2Oct2023。VMware......
  • VMware ESXi 7.0 U3o macOS Unlocker & OEM BIOS 标准版和厂商定制版 2023年12月更新
    VMwareESXi7.0U3omacOSUnlocker&OEMBIOS标准版和厂商定制版2023年12月更新ESXi7.0标准版和Dell(戴尔)、HPE(慧与)、Lenovo(联想)、Inspur(浪潮)、Cisco(思科)定制版镜像作者主页:sysin.org2023-12-17更新(驱动和OEM组件更新):DellESXi7.0U3oA17HPEESXi7.0......
  • HTML中获取iOS手机安全区域SafeArea
    苹果手机iPhoneX及以上版本都带有刘海屏,14pro以上版本变成了所谓灵动岛,所以顶部和底部都有一个安全区域。手机型号尺寸(pt)倍数屏幕(px)状态栏高度顶部安全距离底部安全距离iPhone14Pro、iPhone15Pro、iPhone15393*85231179*255654pt59pt34pt(竖屏)/21pt(横屏)......