简体   繁体   中英

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. Please help me to progress my project work. Thanks, Rohan

I have faced this problem before and done a swift package for this

just in your xcode click

file -> swift packages -> add package url

copy paste this url 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

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.

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

     }
 }];
 }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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