swift2 播放网络文件夹中的视频文件

t1qtbnec  于 2022-11-06  发布在  Swift
关注(0)|答案(2)|浏览(259)

我正在尝试播放网络路径中的视频文件,该路径为
\alan\movies\kids\alladin.mp4的文件夹中。
我可以从Mac查看和浏览目录。
如果我将视频文件添加为项目的一部分,下面的代码可以正常工作

  1. import UIKit
  2. import AVFoundation
  3. import AVKit
  4. class ViewController: AVPlayerViewController {
  5. @IBOutlet var vwVideoView: UIView!
  6. override func viewDidLoad() {
  7. super.viewDidLoad()
  8. // Do any additional setup after loading the view, typically from a nib.
  9. playVideo()
  10. }
  11. override func didReceiveMemoryWarning() {
  12. super.didReceiveMemoryWarning()
  13. // Dispose of any resources that can be recreated.
  14. }
  15. private func playVideo() {
  16. if let path = NSBundle.mainBundle().pathForResource("Alladin", ofType: "mp4") {
  17. let url = NSURL(fileURLWithPath: path)
  18. player = AVPlayer(URL: url)
  19. }
  20. else {
  21. print("Oops, something wrong when playing video")
  22. }
  23. }
  24. }

我从网络文件夹复制视频文件到我的本地电影文件夹如下

  1. NSBundle.mainBundle().pathForResource("file://localhost/Users/alan/Movies/test/Alladin", ofType: "mp4")

视频文件仍然无法播放。这甚至可能从网络播放视频文件吗?
问候-艾伦-

egdjgwm8

egdjgwm81#

您只能将AVPlayer用于嵌入式或本地视频,对于远程视频(在本例中为流视频),您可以使用MPMoviePlayerController并将名为movieSourceType的属性设置为MPMovieSourceTypeStreaming:

  1. MPMoviePlayerViewController *mediaPlayerVC = [[MPMoviePlayerViewController alloc] init];
  2. mediaPlayerVC.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
  3. [mediaPlayerVC.moviePlayer setContentURL:video-url];

或者,如果您不想使用mediaplayer,请尝试:

  1. NSURL *url = [[NSBundle mainBundle] URLForResource:@"Alladin" withExtension:@"mp4"];
  2. AVAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
  3. AVPlayerItem *item = [AVPlayerItem playerItemWithAsset:asset];
  4. AVPlayer *player = [AVPlayer playerWithPlayerItem:item];
xmakbtuz

xmakbtuz2#

因为MPMoviePlayerViewController在iOS9中被弃用,所以,最好使用苹果的建议。

  1. - (void)playVidea:(UIButton *)btn {
  2. // play local video
  3. // NSURL *url = [[NSBundle mainBundle] URLForResource:@"Alladin" withExtension:@"mp4"];
  4. // play video from service
  5. NSURL *url = [NSURL URLWithString:@"http://119.23.148.104/image/931345031250313216.mp4"];
  6. AVAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
  7. AVPlayerItem *item = [AVPlayerItem playerItemWithAsset:asset];
  8. AVPlayer *player = [AVPlayer playerWithPlayerItem:item];
  9. AVPlayerViewController *playController = [[AVPlayerViewController alloc] init];
  10. playController.player = player;
  11. [playController.player play];
  12. [self presentViewController:playController animated:YES completion:nil];
  13. }

相关问题