2017-12-18 167 views
2

我想在人像模式下在閃屏中播放一些視頻 - 全屏。 這可能嗎?請通過適當的解決方案指導我。如何在縱向模式下以全屏尺寸播放視頻?

我在尋找迅速解決應用程序的委託

let splashScreenVC = viewController(withIdentifier: "identifier", inStoryboard: "storyboard:) as? SplashViewController 
window?.rootViewController = splashScreenVC 

裏面的視圖控制器SplashViewController的

+1

的可能的複製[如何發揮IOS視頻飛濺(目標C) ](https://stackoverflow.com/questions/32515407/how-to-play-a-video-splash-in-iosobjective-c) –

+1

它在客觀上c – Jan

+0

@Ahhishek該鏈接是ObjC,而且接受的答案是基本上連接到一個Github repo ... –

回答

3

任何麻煩創建的UIView一個子類,具有AVPlayer和功能createBackground

import AVFoundation 

class BackgroundVideo: UIView { 
    var player: AVPlayer? 

    func createBackground(name: String, type: String) { } 
} 

那麼你的createBackground可能是這樣的:

實現視頻循環

  • AVPlayerItemDidPlayToEndTime:10
    func createBackground(name: String, type: String) { 
        guard let path = Bundle.main.path(forResource: name, ofType: type) else { return } 
    
        player = AVPlayer(url: URL(fileURLWithPath: path)) 
        player?.actionAtItemEnd = AVPlayerActionAtItemEnd.none; 
        let playerLayer = AVPlayerLayer(player: player) 
        playerLayer.frame = self.frame 
        playerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill 
        self.layer.insertSublayer(playerLayer, at: 0) 
        player?.seek(to: kCMTimeZero) 
        player?.play() 
    } 
    

    那麼您可以添加更多的東西,比如觀察的通知。

  • UIApplicationWillEnterForeground用於控制視頻循環,從後臺恢復後。

最後你可能會附上這個BackgroundView,只要您需要:?假的閃屏,主屏幕,等等...

-2

內didFinishLaunch方法,你可以在viewDidLoad方法播放視頻。

func playVideo() { 
    guard let videoPath = Bundle.main.path(forResource: "Redtaxi-splash", ofType:"mov") else { 
     return 
    } 

    let videoURL = URL(fileURLWithPath: videoPath) 
    let player = AVPlayer(url: videoURL) 
    playerViewController = AVPlayerViewController() 
    playerViewController?.player = player 
    playerViewController?.showsPlaybackControls = false 
    playerViewController?.view.frame = view.frame 
    playerViewController?.view.backgroundColor = .white 
    playerViewController?.view.contentMode = .scaleAspectFill 


    NotificationCenter.default.addObserver(self, selector: #selector(playerDidFinishPlaying(note:)), 
              name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: player.currentItem) 

    view.addSubview((playerViewController?.view)!) 
    playerViewController?.player?.play() 
} 
1

SWIFT 3.0

你不能這樣做,在默認的啓動畫面,但你可以做一些變通方法,以達致這。

首先你應該把你的視頻的第一幀,這將是圖像。

你可以使用Photoshop或任何其他圖形工具。

現在,您可以將它設置爲UIImageView中的默認啓動畫面。

現在製作一個UIViewController並啓動它作爲初始視圖控制器。

並在下面的代碼,以播放視頻

private func playFullScreenVideo() { 
    // drag your video file in project 
    // check if video file is available,if not return 
    guard let path = Bundle.main.path(forResource: "video", ofType:"mp4") else { 
     debugPrint("video.mp4 missing") 
     return 
    } 
    // create instance of videoPlayer with video path 
    let videoPlayer = AVPlayer(url: URL(fileURLWithPath: path)) 
    // create instance of playerlayer with videoPlayer 
    let playerLayer = AVPlayerLayer(player: videoPlayer) 
    // set its videoGravity to AVLayerVideoGravityResizeAspectFill to make it full size 
    playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill 
    // add it to your view 
    playerLayer.frame = self.view.frame 
    playerView.layer.addSublayer(playerLayer) 
    // start playing video 
    videoPlayer?.play() 
    } 

讓我知道如果有這個

相關問題