0

したがって、私のアプリにはMPMoviePlayerViewController、ユーザーがビデオを表示できるようにする があります。どの向きでもビデオを見ることができ、MPMoviePlayerViewController終了して「完了」を押すと、前のビュー コントローラーに戻りますが、縦向きのみです。問題は、[完了] ボタンを押すと、View Controller が縦向きに戻る前に横向きで短時間点滅することです。これは、実行時にどのように見えるかです: http://1drv.ms/1RSqUUD

私のコードが添付されています:

import UIKit
import MediaPlayer

class VideoViewController: UIViewController {

var movieViewController : MPMoviePlayerViewController?
var movieplayer : MPMoviePlayerController!

override func viewDidLoad() {
    var url = NSURL(string: "http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v")!
    movieViewController = MPMoviePlayerViewController(contentURL: url)
}



override func viewWillAppear(animated: Bool) {
    let value = UIInterfaceOrientation.Portrait.rawValue
    UIDevice.currentDevice().setValue(value, forKey: "orientation")
}

@IBAction func WatchPressed(sender: AnyObject)
{
    self.presentMoviePlayerViewControllerAnimated(movieViewController)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "Rotated", name: UIDeviceOrientationDidChangeNotification, object: nil)
}


func Rotated()
{
    if UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation)
    {
        if UIDevice.currentDevice().orientation.rawValue == 4 ||  UIDevice.currentDevice().orientation.rawValue == 3
        {
            movieViewController!.view.frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height)

        }
    }
    else if UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation)
    {
        if UIDevice.currentDevice().orientation.rawValue == 1 || UIDevice.currentDevice().orientation.rawValue == 2
        {
            movieViewController!.view.frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height)

            let value = UIInterfaceOrientation.Portrait.rawValue
            UIDevice.currentDevice().setValue(value, forKey: "orientation")
        }

編集

このコードを使用する場合: (以下で提案されているように) http://txt.do/nt1s ユーザーが横向きに回転するとムービー プレーヤーから放り出され、縦向きに回転する前に横向きの前のビュー コントローラーが短時間表示されます。 .

4

1 に答える 1

1

次の方法で解決できます。

まず第一に、これをviewDidLoad追加してください:

override func viewDidLoad() {

    //This observer will call doneButtonClick method when done button is pressed.
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "doneButtonClick:", name: MPMoviePlayerPlaybackDidFinishNotification, object: nil)

    var url = NSURL(string: "http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v")!
    movieViewController = MPMoviePlayerViewController(contentURL: url)
}

その後、このメソッドを追加します。

func doneButtonClick(sender:NSNotification?){
    let value = UIInterfaceOrientation.Portrait.rawValue
    UIDevice.currentDevice().setValue(value, forKey: "orientation")
}

これにより、向きが強制的に縦向きに変更されます。

正常に動作しています。

于 2015-06-12T10:22:16.920 に答える