2016-03-30 106 views
2

我試圖用AVPlayerViewController播放mp4視頻。我用 「HTTP URL」,它可以播放,但是當我使用fileUrl,這是行不通的......ios AVPlayerViewController無法播放本地mp4

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"mp4ForDownloadTest" ofType:@"mp4"]; 
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) { 
    NSURL *url = [NSURL fileURLWithPath:filePath isDirectory:NO]; 

    AVPlayerViewController *playerVC = [[AVPlayerViewController alloc] init]; 

    AVAsset *movieAsset = [AVURLAsset URLAssetWithURL:url options:nil]; 
    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:movieAsset]; 
    AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem]; 

    playerVC.player = player; 

    [self presentViewController:playerVC animated:YES completion:^{ 
     [player play]; 
    }]; 
} 

enter image description here

回答

1

試試這個:

 guard let path = NSBundle.mainBundle().pathForResource("drum", ofType:"mp4") else { 
      break 
     } 

     let url = NSURL(fileURLWithPath: path) 
     self.loadPlayer(url) 

則:

private func loadPlayer(url: NSURL) { 
    // present video window 
    let playerItem = AVPlayerItem(URL: url) 
    let player = AVPlayer.init(playerItem: playerItem) 
    self.playerViewController.player = player 

    self.view.addSubview(self.playerViewController.view) 
    self.videoControlView.alpha = 1.0 
    self.setupPlayerObserver() 
    self.playPlayer() 
    self.setSlider() 
} 
+0

非常感謝你 – tangkunyin

+0

請接受,如果它幫助你:) – rcat24