繁体   English   中英

带有范围滑块的 IOS 修剪视频

[英]IOS trim video with range slider

我正在使用范围滑块修剪正在运行的视频。但我无法将视频停止到特定时间。 这是我正在使用的以下代码:

- (void)videoRange:(SAVideoRangeSlider *)videoRange didChangeLeftPosition:   (CGFloat)leftPosition rightPosition:(CGFloat)rightPosition
{
    self.startTime = leftPosition;
    self.stopTime = rightPosition;
    [self.movieController stop];

    MPMoviePlayerController *player = self.movieController;
    [player stop];
    [player setCurrentPlaybackTime:self.startTime];
    [player setEndPlaybackTime:self.stopTime];
    [player play];

}  

setCurrentPlaybackTime 有效,但“setEndPlaybackTime”无效。 请帮助我推进我的项目工作。 谢谢,罗汉

我之前遇到过这个问题,并为此做了一个 swift 包

只需在您的 xcode 中单击

文件 -> swift 包 -> 添加包 url

复制粘贴这个网址https://github.com/madadoux/DUTrimVideoView

它有一个视图模型,您可以创建自己的视图模型,并将其传递给初始化程序

例子

class ViewController: UIViewController,VideoTrimViewDelegate {
func rangeSliderValueChanged(trimView: DUTrimVideoView, rangeSlider: DURangeSlider) {
    
}

func add() {
    let url = URL(fileURLWithPath:  Bundle.main.path(forResource: "video", ofType: "mp4")!)
    let trimView = DUTrimVideoView(asset: AVAsset(url: url), frame: CGRect(x: 20, y: 200, width: self.view.frame.width-40, height: 100))
    trimView.delegate = self
    let rangeSlider = trimView.rangeSlider!
    rangeSlider.leftThumbImage = UIImage(named: "trim-right")
    rangeSlider.rightThumbImage = UIImage(named: "trim-left")
    rangeSlider.thumbWidth = 30
    rangeSlider.thumbHeight = 30
    
    self.view.addSubview(trimView)
    
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    
    let btn = UIButton(type: .infoLight, primaryAction: UIAction(handler: { (a) in
        self.view.subviews.filter({$0 is DUTrimVideoView }).first?.removeFromSuperview()
        
        self.add()
    }))
    btn.frame = CGRect(origin: .zero, size: CGSize(width: 50, height: 50))
    btn.center = self.view.center
    self.view.addSubview(btn)
}

}

希望回答了你的问题

我不记得我的范围滑块在哪里了,但这个可能更好https://github.com/muZZkat/NMRangeSlider

我拥有的那个可以轻松地为您提供一系列值。 这是一些代码。 min 用于第一个滑块值,max 用于第二个滑块值。

。H

AVAssetExportSession *exportSession;
float max;
float min;

.m

- (void)trimVideo:(NSString *)outputURL assetObject:(AVAsset *)asset
{

@try
{
    exportSession = [[AVAssetExportSession alloc]initWithAsset:asset presetName:AVAssetExportPresetHighestQuality];

    exportSession.outputURL = [NSURL fileURLWithPath:outputURL];

    exportSession.outputFileType = AVFileTypeQuickTimeMovie;

    CMTime start = CMTimeMakeWithSeconds(min, 1);

    CMTime duration = CMTimeMakeWithSeconds((max - min), 1);

    CMTimeRange range = CMTimeRangeMake(start, duration);

    exportSession.timeRange = range;

    exportSession.outputFileType = AVFileTypeQuickTimeMovie;

    [self checkExportSessionStatus:exportSession];

    exportSession = nil;

}
@catch (NSException * e)
{
    NSLog(@"Exception Name:%@ Reason:%@",[e name],[e reason]);
}
}

- (void)checkExportSessionStatus:(AVAssetExportSession *)exportSession
{


[exportSession exportAsynchronouslyWithCompletionHandler:^(void)
 {

     switch ([exportSession status])
     {
         case AVAssetExportSessionStatusCompleted:
         {

                 [[[UIAlertView alloc] initWithTitle:@"Video Trimmed"
                                             message:@"Your trimmed video has been sent to your finished videos."
                                            delegate:nil
                                   cancelButtonTitle:@"Ok"
                                   otherButtonTitles:nil] show];
                 // dismiss progress bar or i use the SVProgress framework [SVProgressHUD dismiss];
                  // you can save it here                 
             break;
         }
         case AVAssetExportSessionStatusFailed:
         {
             NSLog(@"Error in exporting");
             [[[UIAlertView alloc] initWithTitle:@"Export fail"
                                         message:nil
                                        delegate:nil
                               cancelButtonTitle:@"Ok"
                               otherButtonTitles:nil] show];
         }
             break;

         default:
         {
             break;
         }

     }
 }];
 }

希望这会有帮助。 您需要做的就是将各个部分放在一起。 您可以从这里获取 SVProgress: https : //github.com/TransitApp/SVProgressHUD

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM