2017-09-18 9 views
1

iOSアプリケーションを開発していて、テーブルビューから行を選択したときにローカルビデオファイルを再生できるようにします。セル行が選択されると、ビデオが再生されるはずのAVPlayerViewControllerに続きますが、セグが実行され、AVPlayerViewControllerが表示された後、そのAVPlayerには空白が残ります。すべてのコントロールが表示されますが、プレーヤーは空白になります。プレイヤーがプレイしていない理由はありますか?すべての動画をフォルダに追加して、その動画フォルダをメインバンドルに追加したので、動画が再生されない理由はわかりません。どのように私はプレイと仕事を得ることができるようにどのような提案は非常に高く評価されるだろう。私はまた、ストーリーボードのAVPlayerViewControllerへのセグをトリガし、セルがタップされるたびにセグを行います。URLの配列を使用してAVPlayerでローカルビデオを再生する方法は?

ビデオがロードされているコード:プレイヤーがレンダリングされる

import UIKit 
import AVKit 
import AVFoundation 

    class DrillsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 


var playerViewController = AVPlayerViewController() 
var player = AVPlayer() 
var videoURL = [URL]() 
var videoUrl = [URL]() 
var drillVid = URL(fileURLWithPath: String()) 

override func viewDidLoad() { 
    super.viewDidLoad() 
videoUrl = [URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BHB DrillVids/TwoBallBBw:Pound.mp4"),URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BHB DrillVids/TwoBallBtwLegwPound.mp4"),URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BHB DrillVids/TwoBallCrosswPound.mp4"),URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BHB DrillVids/TwoBallHiLo.mp4"),URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BHB DrillVids/OneBallThruHoop.mp4"),URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BHB DrillVids/TwoBallIOw:Wiper.mp4"),URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BHB DrillVids/TwoBallJuggle.mp4"),URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BHB DrillVids/TwoBallInOut.mp4"),URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BHB DrillVids/TwoBallWiper.mp4"),URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BHB DrillVids/TwpBallOverDribble.mp4")] 


    tableView.delegate = self 
    tableView.dataSource = self } 

コード:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
{ 

drillVid = videoUrl[indexPath.row] 

    return cell 
} 

AVPlayerViewControllerが示され、それはプレーヤーのコードは、ビデオを再生しなければならない、しかし何もそれだけで空の果たしていません。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    drillVid = videoUrl[indexPath.row]. } 


    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 


     if segue.identifier == "playDrill" { 
       let destination = segue.destination as! AVPlayerViewController 
      if let indexPath = self.tableView.indexPathForSelectedRow { 
        drillVid = videoURL[indexPath.row] 
        let destination = segue.destination as! AVPlayerViewController 
        destination.player = AVPlayer(url: drillVid) 
        destination.player?.play() 

       } 
       } 

答えて

1

[URL(fileURLWithPath: "/Users/jordanlagrone/Desktop/BlackHeartBB/BHB DrillVids/TwoBallBBw:Pound.mp4")

それはlet path = Bundle.main.path(forResource: "Pound", ofType: "mp4")

例だ..あなたがあなたのバンドル内のファイルを取得する方法

ではありません:.MP4形式でプロジェクトに「ビデオ」を追加します。ビデオを選択し、右側のパネルでターゲットをあなたのアプリにします。これでメインバンドルにビデオが追加されます。

ストーリーボードにボタンを追加します。 AVPlayerControllerを追加します。 Ctrl +そのボタンをクリックし、AVPlayerControllerにドラッグします。ビューコントローラのコードで

enter image description here

、機能prepareForSegueをオーバーライドします。コード

enter image description here enter image description here enter image description here

...その先を取得し、それをビデオにパスをつかんで動画を再生するにはplayerだ設定のようになります。あなたは、コードを実行すると

import UIKit 
import AVKit 
import AVFoundation 

class ViewController: UIViewController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 


    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     let destination = segue.destination as! AVPlayerViewController 

     let url = URL(fileURLWithPath: Bundle.main.path(forResource: "Video", ofType: "mp4")!) 
     destination.player = AVPlayer(playerItem: AVPlayerItem(url: url)) 
     destination.player?.play() 
    } 

} 

ボタンを押すと、ビデオが再生されます。 iPhone 6S、iOS 11で自分自身をテストしました。

+0

アドバイスありがとうございますが、これは本当に私の質問を解決するのに役立ちません。これがプレイヤーがプレイしていない理由がない限りですか? –

+0

@MarkoCain;あなたはビデオプレーヤーに間違ったパスを与える可能性が最も高いです。これは "Pound"リソースの正しいパスを取得し、ビデオプレーヤーはそれを再生できます。 – Brandon

+0

ありがとう、それは助けにはならなかったとは決して言わなかった!やってみます。 –

関連する問題