2017-07-28 145 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() 
     } 


    }