0

I am trying to play a video file from my network path which is

\\alan\movies\kids\alladin.mp4.

I am able to see and browse the directory from my Mac.

The code below work fine if I add the video file as part of the project

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")
        }
    }
}

I copied the video file from network folder to my local movies folder as below

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

The video file is still not playing. Is this even possible to play video file from network?

regards, -Alan-

Alan B
  • 2,219
  • 4
  • 32
  • 62

2 Answers2

0

You can use AVPlayer only for embedded or local video, for a remote video, in this case is a streaming video, you can use the MPMoviePlayerController and sets the property named movieSourceType to MPMovieSourceTypeStreaming:

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

or if you don't want use mediaplayer, try with :

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];
Danilo
  • 631
  • 1
  • 9
  • 24
  • I understood that MPMoviePlayerViewController is deprecated in iOS 9. Shouldn't there be a replacement in AVPlayer for the same feature (playing a remote file)? – Alan B Nov 02 '15 at 13:41
  • Deprecation doesn't mean that your app will stop working. It means that Apple is gradually phasing out usage of those classes, and that, at some point in the future, they will drop support completely. However, Apple will make it clear when these classes are no longer available. – Danilo Nov 02 '15 at 14:14
  • @Danillo. Thanks. But do you have a swift version of this code? I never done any objective C before – Alan B Nov 02 '15 at 15:29
0

because of MPMoviePlayerViewController deprecated in iOS9, so, It's better to use Apple's recommendation.

- (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];
}
林平君
  • 111
  • 1
  • 2