从网络文件夹播放视频文件

时间:2015-11-02 11:24:55

标签: ios swift swift2 xcode7 avkit

我正在尝试从我的网络路径播放视频文件

\\阿兰\电影\孩子\ alladin.mp4。

我可以从Mac上查看和浏览目录。

如果我将视频文件添加为项目的一部分,则下面的代码可以正常工作

import UIKit
import AVFoundation
import AVKit

class ViewController: AVPlayerViewController {


    @IBOutlet var vwVideoView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        playVideo()
    }

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

    private func playVideo() {
        if let path = NSBundle.mainBundle().pathForResource("Alladin", ofType: "mp4") {
            let url = NSURL(fileURLWithPath: path)
            player = AVPlayer(URL: url)
        }
        else {
            print("Oops, something wrong when playing video")
        }
    }
}

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

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

视频文件仍未播放。这甚至可以播放来自网络的视频文件吗?

的问候, -Alan -

2 个答案:

答案 0 :(得分:0)

您可以将AVPlayer仅用于嵌入式或本地视频,对于远程视频,在这种情况下是流式视频,您可以使用MPMoviePlayerController并将名为movieSourceType的属性设置为MPMovieSourceTypeStreaming:

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

或者如果您不想使用媒体播放器,请尝试使用:

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

答案 1 :(得分:0)

因为iOS9中不推荐使用MPMoviePlayerViewController,所以最好使用Apple的推荐。

- (void)playVidea:(UIButton *)btn {
    // play local video
    // NSURL *url = [[NSBundle mainBundle] URLForResource:@"Alladin" withExtension:@"mp4"];

    // play video from service 
    NSURL *url = [NSURL URLWithString:@"http://119.23.148.104/image/931345031250313216.mp4"];
    AVAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
    AVPlayerItem *item = [AVPlayerItem playerItemWithAsset:asset];
    AVPlayer *player = [AVPlayer playerWithPlayerItem:item];

    AVPlayerViewController *playController = [[AVPlayerViewController alloc] init];
    playController.player = player;
    [playController.player play];
    [self presentViewController:playController animated:YES completion:nil];
}