Roi*_*lia 9 orientation avplayer avasset swift avplayeritem
试图使用Ray伟大的教程来解决方向问题.
左 - 视频应该如何(纵向),右边 - 不需要的旋转结果
使用的代码
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)
}
Run Code Online (Sandbox Code Playgroud)
有什么建议?
如果问题在于在整个应用程序中旋转 iPhone :请确保您的应用程序在 Info.plist 文件中具有正确支持的界面方向。弹出 Info.plist,然后检查底部两个数组(Supported Interface Orientations和Supported Interface Orientations (iPad)。确保它们都添加了所有四个方向。如果没有,请将鼠标悬停在 Supported Interface Orientations 上,按下拉箭头,然后按“支持的界面方向”上方显示的 + 号并添加任何缺少的方向。对“支持的界面方向 (iPad)”数组执行相同的操作。
如果这不起作用,则可能只是视频的问题。在此处查看问题和接受的答案:How to fix videoorientation issues in iOS。这是将 Objective-C 转换为 Swift 的好方法: https: //objectivec2swift.com/#/converter/code。
如果您的应用不旋转,并且您希望视频仅旋转:
将其放入您想要锁定为纵向模式的视图中:
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.Portrait
}
Run Code Online (Sandbox Code Playgroud)
然后,在您的视频视图中:
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.All
}
Run Code Online (Sandbox Code Playgroud)
为了更好地衡量 - 您可能也希望在两个视图上都这样做:
override func shouldAutorotate() -> Bool {
return true
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2256 次 |
| 最近记录: |