2017-07-28 258 views
2

我是新来的ios编程,实际上我需要流视频URL,但我无法使用avplayer流视频URL,但使用avplayer下载的文件,我可以play.Actual问题是我的文件格式是不同 例如我的文件名是这样song.apa但song.mp4如何在ios swift中播放视频

代码:

let avplayerController = AVPlayerViewController() 
var avPlayer:AVPlayer? 

override func viewDidLoad() 
{ 
    super.viewDidLoad() 
    let movieUrl = URL.init(string: "http://techslides.com/demos/sample-videos/small.3gp") 
    self.avPlayer = AVPlayer.init(url: movieUrl!) 
    self.avplayerController.player = self.avPlayer 
} 

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

@IBAction func playVideo(_ sender: Any) 
{ 
    self.present(self.avplayerController, animated: true) { 
    self.avplayerController.player?.play() 
    } 
} 
+0

AVPlayer不能流Youtube-视频,看到这个答案https://stackoverflow.com/a/34709948/1518174 – Matz

+0

在youtube是基于工作在iframe ,你不能在avplayer中玩。使用webview或youtubeplayer –

+0

不仅youtube视频其他视频链接也我无法播放,我改变了链接 – skyshine

回答

0

继承人视频流的例子..

override func viewDidLoad() { 
     super.viewDidLoad() 
     let player = AVPlayer(url: URL(string: "http://techslides.com/demos/sample-videos/small.mp4")!) 
     let controller = AVPlayerViewController() 
     present(controller, animated: true) { _ in } 
     controller.player = player 
     addChildViewController(controller) 
     view.addSubview(controller.view) 
     controller.view.frame = CGRect(x: 50, y: 50, width: 300, height: 300) 
     controller.player = player 
     controller.showsPlaybackControls = true 
     player.isClosedCaptionDisplayEnabled = false 
     player.play() 
    } 
+0

我的流媒体视频url链接扩展名是不同的,例如song1.apa,但实际的格式是song1.mp4 – skyshine

+0

song1.apa ??从未听说过.apa扩展 –

+0

song1.app但我的文件是mp4只有当我给那些工作的Android播放器的链接 – skyshine

0

您无法使用AVPlayer或AVPlayerViewController播放此特定链接(http://techslides.com/demos/sample-videos/small.3gp),因为该文件处于AMR(自适应多速率,语音格式) - 自iOS 4.3以来不支持此功能012,某些3gp文件可以播放,但只能播放有Apple支持的视频和音频流。

1

在斯威夫特3,试试这个代码项目

import UIKit 
import AVKit 
import AVFoundation 
import MediaPlayer 
import MobileCoreServices 

class VideoPlayerViewController: UIViewController,AVPlayerViewControllerDelegate { 

    //MARK: - Outlet - 
    @IBOutlet weak var viewVidioPlayer: UIView! 
    //MARK: - Variable 

    //MARK: - View Life Cycle - 
    override func viewDidLoad() { 
     super.viewDidLoad()   
    } 
    //MARK: - Action - 
    //for Playing Video 
    @IBAction func btnvideoPlayClicked(_ sender: UIButton) { 
     self.videoPlay() 
    } 

    func videoPlay() 
    { 
     let playerController = AVPlayerViewController() 
     playerController.delegate = self 

     let bundle = Bundle.main 
     let moviePath: String? = "http://techslides.com/demos/sample-videos/small.3gp" 
     let movieURL = URL(fileURLWithPath: moviePath!) 

     let player = AVPlayer(url: movieURL) 
     playerController.player = player 
     self.addChildViewController(playerController) 
     self.view.addSubview(playerController.view) 
     playerController.view.frame = self.view.frame 

     player.play() 

    } 
    //MARK: - other Function - 

    func playerViewControllerWillStartPictureInPicture(_ playerViewController: AVPlayerViewController){ 
     print("playerViewControllerWillStartPictureInPicture") 
    } 

    func playerViewControllerDidStartPictureInPicture(_ playerViewController: AVPlayerViewController) 
    { 
     print("playerViewControllerDidStartPictureInPicture") 

    } 
    func playerViewController(_ playerViewController: AVPlayerViewController, failedToStartPictureInPictureWithError error: Error) 
    { 
     print("failedToStartPictureInPictureWithError") 
    } 
    func playerViewControllerWillStopPictureInPicture(_ playerViewController: AVPlayerViewController) 
    { 
     print("playerViewControllerWillStopPictureInPicture") 
    } 
    func playerViewControllerDidStopPictureInPicture(_ playerViewController: AVPlayerViewController) 
    { 
     print("playerViewControllerDidStopPictureInPicture") 
    } 
    func playerViewControllerShouldAutomaticallyDismissAtPictureInPictureStart(_ playerViewController: AVPlayerViewController) -> Bool 
    { 
     print("playerViewControllerShouldAutomaticallyDismissAtPictureInPictureStart") 
     return true 
    } 
} 

播放视频我希望这是对你的工作, 谢谢

+0

本地文件我可以播放,但我的流url有不同的扩展名,例如192.179.10.11/content/song1.apa,但实际文件是song1.mp4 – skyshine

+0

你明白吗我的问题 – skyshine

0

您可以使用YoutubePlayerView用于播放YouTube视频,并为其他格式你可以使用UIWebView。这样做,只需使用if-else块,

if movieUrl.contains("youtube.com/") { 
    var videoId: String = extractYoutubeId(fromLink: movieUrl) 
    youtubePlayerView.load(withVideoId: videoId) 
} else { 
    webView = UIWebView(frame: CGRect(x: 0, y: 0, width: 288, height: 277)) 
    webView.delegate = self 
    webView.backgroundColor = UIColor.black 
    webView?.loadRequest(movieUrl) 
} 
0

对于斯威夫特3播放视频从URL

//AVPlayerViewControllerDelegate // example : class TestAudioVideoVC: UIViewController,AVPlayerViewControllerDelegate 

func videoPlay() 
    { 
     let playerController = AVPlayerViewController() 
     playerController.delegate = self 

     let videoURL = NSURL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4") 

     let player = AVPlayer(url: videoURL! as URL) 
     playerController.player = player 
     self.addChildViewController(playerController) 
     self.view.addSubview(playerController.view) 
     playerController.view.frame = CGRect(x: 0, y: 64, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height - 64) 
     playerController.showsPlaybackControls = true 
     player.play() 
    } 

//MARK: - AVPlayerViewController Delegate - 

    func playerViewControllerWillStartPictureInPicture(_ playerViewController: AVPlayerViewController){ 
     print("playerViewControllerWillStartPictureInPicture") 
    } 

    func playerViewControllerDidStartPictureInPicture(_ playerViewController: AVPlayerViewController) 
    { 
     print("playerViewControllerDidStartPictureInPicture") 

    } 
    func playerViewController(_ playerViewController: AVPlayerViewController, failedToStartPictureInPictureWithError error: Error) 
    { 
     print("failedToStartPictureInPictureWithError") 
    } 
    func playerViewControllerWillStopPictureInPicture(_ playerViewController: AVPlayerViewController) 
    { 
     print("playerViewControllerWillStopPictureInPicture") 
    } 
    func playerViewControllerDidStopPictureInPicture(_ playerViewController: AVPlayerViewController) 
    { 
     print("playerViewControllerDidStopPictureInPicture") 
    } 
    func playerViewControllerShouldAutomaticallyDismissAtPictureInPictureStart(_ playerViewController: AVPlayerViewController) -> Bool 
    { 
     print("playerViewControllerShouldAutomaticallyDismissAtPictureInPictureStart") 
     return true 
    } 
3

在斯威夫特3

import Foundation 
import AVFoundation 
import MediaPlayer 
import AVKit 


func play() 
    { 
     let player = AVPlayer(url: "Your Url") 
     let playerViewController = AVPlayerViewController() 
     playerViewController.player = player 
     self.present(playerViewController, animated: true) 
     { 
      playerViewController.player!.play() 
     } 


    }