AVAsset方向/旋转问题

时间:2016-02-24 14:51:25

标签: swift orientation avplayer avasset avplayeritem

尝试使用Ray great tutorial来解决方向问题。

左 - 视频的外观(纵向),右侧 - 不需要的旋转结果

enter image description here

使用的代码

   func setUpVideoNew()
    {

        let originalVideoTrack = asset.tracksWithMediaType(AVMediaTypeVideo)[0]
        let composition = AVMutableComposition()
        let videoTrack = composition.addMutableTrackWithMediaType(AVMediaTypeVideo, preferredTrackID: 1)
        let timeRange = originalVideoTrack.timeRange
        do {
            try videoTrack.insertTimeRange(timeRange, ofTrack: originalVideoTrack, atTime: kCMTimeZero)
        } catch {

        }
        let mainInstruction = AVMutableVideoCompositionInstruction()
        mainInstruction.timeRange = timeRange

        let firstlayerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: videoTrack)
        let firstAssetTrack = originalVideoTrack


        //MARK - Fix Rotation
        var firstAssetOrientation_ = UIImageOrientation.Up

        var isFirstAssetPortrait_ = false

        var firstTransform = videoTrack.preferredTransform;
        if firstTransform.a == 0 && firstTransform.b == 1.0 && firstTransform.c == -1.0 && firstTransform.d == 0 {
            firstAssetOrientation_ = UIImageOrientation.Right;
            isFirstAssetPortrait_ = true;
        }
        if (firstTransform.a == 0 && firstTransform.b == -1.0 && firstTransform.c == 1.0 && firstTransform.d == 0) {
            firstAssetOrientation_ =  UIImageOrientation.Left;
            isFirstAssetPortrait_ = true;
        }
        if (firstTransform.a == 1.0 && firstTransform.b == 0 && firstTransform.c == 0 && firstTransform.d == 1.0) {
            firstAssetOrientation_ =  UIImageOrientation.Up;
        }
        if (firstTransform.a == -1.0 && firstTransform.b == 0 && firstTransform.c == 0 && firstTransform.d == -1.0) {
            firstAssetOrientation_ = UIImageOrientation.Down;
        }

        firstlayerInstruction.setTransform(asset.preferredTransform, atTime: kCMTimeZero)
        mainInstruction.layerInstructions = [firstlayerInstruction]


        let videoComposition = AVMutableVideoComposition()
        videoComposition.frameDuration = CMTimeMake(1, 30)
        videoComposition.instructions = [mainInstruction]

        var naturalSizeFirst = CGSize()

        if(isFirstAssetPortrait_){
            naturalSizeFirst = CGSizeMake(videoTrack.naturalSize.height, videoTrack.naturalSize.width);
        } else {
            naturalSizeFirst = videoTrack.naturalSize;
        }
        videoComposition.renderSize = naturalSizeFirst
        let playerItem = AVPlayerItem(asset: composition)
        playerItem.videoComposition = videoComposition
        player = AVPlayer(playerItem: playerItem)
        let playerLayer = AVPlayerLayer(player: player)
        print(videoPreview.frame)
        print(videoPreview.bounds)
        playerLayer.frame = self.videoPreview.bounds

        player.play()
        self.videoPreview.layer.addSublayer(playerLayer)



    }

有什么建议吗?

2 个答案:

答案 0 :(得分:2)

You should register for a notification for when the status bar changes orientation, like so:

    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("statusBarOrientationChange:"), name: UIApplicationDidChangeStatusBarOrientationNotification, object: nil)

After that, a switch statement for this would read like so, for editing the dimensions of your AVLayer:

    switch (UIApplication.sharedApplication().statusBarOrientation){
        case .Unknown :
        case .PortraitUpsideDown :
        case .Portrait
        case .LandscapeLeft :
        case .LandscapeRight :
    }

When you get to the right orientation, you will need to apply a CGAffineTransform like so:

asset.transform = CGAffineTransformMake(M_PI * (180) / 180.0)

Hope this is helpful!

答案 1 :(得分:1)

如果问题是在整个应用中旋转iPhone:确保您的应用在Info.plist文件中具有正确支持的界面方向。弹出到Info.plist,并检查底部的两个数组(支持的接口方向支持的接口方向(iPad)。确保它们都添加了所有四个方向。如果他们这样做不要,将鼠标悬停在支持的界面方向上,按下拉箭头,然后按“支持的界面方向”上方显示的+号,并添加任何缺少的方向。对“”支持的界面方向(iPad)“阵列执行相同操作。

adding supported interface orientations in Info.Plist

如果这不起作用,可能只是视频的问题。在这里查看问题和接受的答案:How to fix video orientation issue in iOS。 这是将objective-c转换为swift的好方法:https://objectivec2swift.com/#/converter/code

如果您的应用未旋转,并且您希望仅旋转视频:

将此信息放入您想要以纵向模式锁定的视图中:

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
 return UIInterfaceOrientationMask.Portrait
}

然后,在您的视频观看中:

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
 return UIInterfaceOrientationMask.All
}

为了更好地衡量 - 您可能也希望在两个视图上都这样:

override func shouldAutorotate() -> Bool {
 return true
}

来源:how to lock portrait orientation for only main view using swift