简体   繁体   English

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

[英]IOS trim video with range slider

I am using range slider to trim running video.But I can't stop the video to the specific time.我正在使用范围滑块修剪正在运行的视频。但我无法将视频停止到特定时间。 Here is the following code, that i am using:这是我正在使用的以下代码:

- (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 is working, But "setEndPlaybackTime" is not working. setCurrentPlaybackTime 有效,但“setEndPlaybackTime”无效。 Please help me to progress my project work.请帮助我推进我的项目工作。 Thanks, Rohan谢谢,罗汉

I have faced this problem before and done a swift package for this我之前遇到过这个问题,并为此做了一个 swift 包

just in your xcode click只需在您的 xcode 中单击

file -> swift packages -> add package url文件 -> swift 包 -> 添加包 url

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

it has a view model and you can make your own , pass it to the the initialiser它有一个视图模型,您可以创建自己的视图模型,并将其传递给初始化程序

example例子

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)
}

} }

hope that answered your question希望回答了你的问题

I can't remember where I got my range slider, but this one might be better https://github.com/muZZkat/NMRangeSlider我不记得我的范围滑块在哪里了,但这个可能更好https://github.com/muZZkat/NMRangeSlider

The one I had could give you a range of values easily.我拥有的那个可以轻松地为您提供一系列值。 Here is some code.这是一些代码。 min is for the first slider value and max is for the second slider value. min 用于第一个滑块值,max 用于第二个滑块值。

.h 。H

AVAssetExportSession *exportSession;
float max;
float min;

.m .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;
         }

     }
 }];
 }

I hope this helps a little.希望这会有帮助。 All you need to do is put the pieces together.您需要做的就是将各个部分放在一起。 You can get the SVProgress from here: https://github.com/TransitApp/SVProgressHUD您可以从这里获取 SVProgress: https : //github.com/TransitApp/SVProgressHUD

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

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