视频无法在iPhone上播放

时间:2016-06-01 12:24:00

标签: ios swift video playback

enter image description here我刚刚看了一个简单的视频。当我在模拟器中运行应用程序时,视频会播放,但是当我在iPhone上运行它时,视频会丢失。非常感谢任何帮助,谢谢!

这是我的代码:

import UIKit
import AVKit
import AVFoundation

class ViewController: UIViewController {

    var playerviewcontroller = AVPlayerViewController()
    var playerview = AVPlayer ()

    @IBAction func playMusic(sender: AnyObject) {
        var fileURL = NSURL(fileURLWithPath:"/Users/MorganEvans/Documents/Apps/32134.mp4")
        playerview = AVPlayer(URL: fileURL)

        playerviewcontroller.player = playerview

        self.presentViewController(playerviewcontroller, animated: true){

            self.playerviewcontroller.player?.play()
        }


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

}

2 个答案:

答案 0 :(得分:1)

视频文件应该添加到Xcode项目中,如下所示:

enter image description here

然后你就可以得到这样的视频网址:

let movieURL = NSBundle.mainBundle().URLForResource("ElephantSeals", withExtension: "mov")!

答案 1 :(得分:0)

请将视频文件放入项目包中。只需将视频拖放到项目中即可。

NSURL *url=[[NSBundle mainBundle]URLForResource:@"arreg" withExtension:@"mp4"];
AVPlayer *video=[AVPlayer playerWithURL:url];
AVPlayerViewController *controller=[[AVPlayerViewController alloc]init];
controller.player=video;
[self.view addSubview:controller.view];

controller.view.frame=self.view.frame;
[self addChildViewController:controller];

[video play];