首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >m3u8未离线播放- swift 3.0

m3u8未离线播放- swift 3.0
EN

Stack Overflow用户
提问于 2017-11-07 20:35:08
回答 1查看 1.2K关注 0票数 1

我正在尝试流式传输m3u8 url并脱机保存。然后在离线状态下玩。我正在使用Apple HTTP Live Streaming

使用以下代码开始下载

代码语言:javascript
复制
func setupAssetDownload() {
        // Create new background session configuration.
         configuration = URLSessionConfiguration.background(withIdentifier: DOWNLOAD_ID)

        // Create a new AVAssetDownloadURLSession with background configuration, delegate, and queue
         downloadSession = AVAssetDownloadURLSession(configuration: configuration,
                                                    assetDownloadDelegate: self,
                                                    delegateQueue: OperationQueue.main)
        let url = URL(string: urlString)
        let asset = AVURLAsset(url: url!)


        // Create new AVAssetDownloadTask for the desired asset
        let downloadTask = downloadSession.makeAssetDownloadTask(asset: asset, assetTitle: "asset_Title", assetArtworkData: nil, options: nil)
        // Start task and begin download
        downloadTask?.resume()

    }

哪里,

代码语言:javascript
复制
let DOWNLOAD_ID = "downloadIdentifier"

let urlString = "http://domain/vod/mp4:sample1.mp4/playlist.m3u8"

// Saving the Download Location

    func urlSession(_ session: URLSession, assetDownloadTask: AVAssetDownloadTask, didFinishDownloadingTo location: URL) {


        print("didFinishDownloadingTo \(location.relativePath)")
        // Do not move the asset from the download location
        UserDefaults.standard.set(location.relativePath, forKey: "assetPath")
    }

当我尝试离线玩的时候,我可以得到AVURLAsset,

代码语言:javascript
复制
<AVURLAsset: 0x1742319e0, URL = file:///var/mobile/Containers/Data/Application/79C0043F-6161-4231-9129-B71C95903E05/Library/com.apple.UserManagedAssets.bhVNRN/asset_Title_63128673BE57FBDD.movpkg/>

但是,不是从资产中支付。

代码语言:javascript
复制
func playOfflineAsset() {
        guard let assetPath = UserDefaults.standard.value(forKey: "assetPath") as? String else {
            // Present Error: No offline version of this asset available
            return
        }
        let baseURL = URL(fileURLWithPath: NSHomeDirectory())
        let assetURL = baseURL.appendingPathComponent(assetPath)
        let asset = AVURLAsset(url: assetURL)


        let player = AVPlayer(url:assetURL)

        let playerLayer = AVPlayerLayer(player: player)
        playerLayer.frame = videoView2.bounds
        playerLayer.backgroundColor = UIColor.purple.cgColor
        videoView2.layer.addSublayer(playerLayer)

        player.play()


        if let cache = asset.assetCache, cache.isPlayableOffline {
            // Set up player item and player and begin playback

            print("exist")

        } else {
            // Present Error: No playable version of this asset exists offline
            print("doesn't exist")
        }
    }
EN

回答 1

Stack Overflow用户

发布于 2018-04-11 13:00:44

你需要添加AVPlayerController到AVPlayer可以播放声音,我不知道为什么,但它对我工作

//设置播放器条目和播放器,开始播放

代码语言:javascript
复制
let playerItem = AVPlayerItem(asset: asset)

player = AVPlayer(playerItem: playerItem)

// play without present playerController
self.player?.play()

// play with present playerController
// let playerController = AVPlayerViewController()

// playerController.player = player
// self.present(playerController, animated: true) {
//       self.player?.play()
// }

编辑:确保hls未加密。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47158159

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档