2016-10-04 91 views
0

你好我試圖在後臺播放視頻,但它給出了一個錯誤和錯誤是「意外地發現零,同時展開一個可選值」,我已經設置在視頻捆綁和選擇複製,如果需要在後臺播放視頻swift 3

這個我的代碼

import UIKit 
import AVFoundation 

class ViewController: UIViewController { 

    var avPlayer: AVPlayer! 
    var avPlayerLayer: AVPlayerLayer! 
    var paused: Bool = false 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     let url = Bundle.main.url(forResource: "Grad_Cap_Toss", withExtension: "mp4") 


     avPlayer = AVPlayer(url: url!) 
     avPlayerLayer = AVPlayerLayer(player: avPlayer) 
     avPlayerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill 
     avPlayer.volume = 0 
     avPlayer.actionAtItemEnd = AVPlayerActionAtItemEnd.none 

     avPlayerLayer.frame = view.layer.bounds 
     view.backgroundColor = UIColor.clear; 
     view.layer.insertSublayer(avPlayerLayer, at: 0) 

     NotificationCenter.default.addObserver(self,selector: Selector(("playerItemDidReachEnd:")),name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,object: avPlayer.currentItem) 

    } 

請任何機構擁有該出主意,謝謝

+1

在'AVPlayer(url:url!)''url' nil? –

+0

沒有它不是n! –

+1

然後,你在哪條線上墜毀? –

回答

0

如果urlnil和視頻已被成功複製到捆綁,可能的問題爲c敏感度。確保文件名使用正確的大小寫。 iOS設備文件系統區分大小寫,因爲iPhone模擬器不是。

+0

我設置每個東西都會像名稱一樣在捆綁但仍然不工作:( –

+0

'Bundle.main.url(forResource:withExtension:)'只會返回'nil',如果它找不到具有匹配名稱的文件並且如果以下任何一種情況屬實,則可能發生這種情況:a)文件未包含在「複製包資源」構建階段中,b)提供的名稱不匹配(區分大小寫)或c)提供的擴展名不匹配(區分大小寫)。 – Rick

1

我有一個類似的問題,並按照裏克答案的指示來解決它。 我的問題是我沒有捆綁資源中的視頻文件。 檢查您的視頻文件是否在項目中的「構建階段」下的「複製包資源」中列出。如果沒有,請使用+按鈕在其中添加文件。 此外,您可能會遺漏代碼中的avPlayer.play()語句。

2

sup,我試圖在我的登錄頁面的視圖控制器中實現相同類型的視頻背景。

我認爲問題是您使用的語法有點不贊成。

烏爾工作的代碼可能看起來像沿着下面的內容:

import UIKit 
import AVKit 
import AVFoundation 

class ViewController: UIViewController { 

@IBOutlet var blackOverlay: UIView! 

// init video background and its path 
var player: AVPlayer? 
let videoURL: NSURL = Bundle.main.url(forResource: "videoName", withExtension: "mp4")! as NSURL 

override func viewDidLoad() { 
    super.viewDidLoad() 

    // Adds a black overlay to the looped video in question 

    blackOverlay.alpha = 0.75; 
    blackOverlay.layer.zPosition = 0; 

    // begin implementing the avplayer 

    player = AVPlayer(url: videoURL as URL) 
    player?.actionAtItemEnd = .none 
    player?.isMuted = true 

    let playerLayer = AVPlayerLayer(player: player) 
    playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill 
    playerLayer.zPosition = -1 

    playerLayer.frame = view.frame 

    view.layer.addSublayer(playerLayer) 

    player?.play() 

    // add observer to watch for video end in order to loop video 

    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.loopVideo), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: self.player) 

    // if video ends, will restart 

    func playerItemDidReachEnd() { 
     player!.seek(to: kCMTimeZero) 
    } 

} 
    // maybe add this loop at the end, after viewDidLoad 
    // func loopVideo() { player?.seek(to: kCMTimeZero) player?.play()}} 

讓我知道是否有幫助,:)

0

我有同樣的問題,當我試圖播放視頻背景。我認爲這是視頻資源的問題。

我解決了這樣的問題。

當您拖動視頻時,您應該選中'如果需要複製項目'和'添加到目標'。 或者您可以右鍵點擊項目導航器,點擊'添加'添加資源。

2
  1. 首先,在您的項目中拖放視頻文件。
    1. 然後導入AVFundation。
    2. 添加例子:var player:AVPlayer?
    3. @IBOutlet weak var videoView:UIView! - 將故事板的視圖連接到VC類。
    4. 之後,將此代碼粘貼到您的ViewController中。
    5. 呼叫 「playBackgoundVideo()」 在您的viewDidLoad中()
private func playBackgoundVideo() { 
    if let filePath = Bundle.main.path(forResource: "your_video_here", ofType:"mp4") { 
     let filePathUrl = NSURL.fileURL(withPath: filePath) 
     player = AVPlayer(url: filePathUrl) 
     let playerLayer = AVPlayerLayer(player: player) 
     playerLayer.frame = self.videoView.bounds 
     playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill 
     NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: self.player?.currentItem, queue: nil) { (_) in 
      self.player?.seek(to: kCMTimeZero) 
      self.player?.play() 
     } 
     self.videoView.layer.addSublayer(playerLayer) 
     player?.play() 
    } 
} 

我認爲這將有助於。 ^^