0

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

\\alan\movies\kids\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")

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

问候,-艾伦-

4

2 回答 2

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];
于 2015-11-02T12:30:35.873 回答
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];
}
于 2017-11-17T03:54:56.057 回答