IOS使用范围滑块修剪视频

时间:2014-01-23 10:01:17

标签: ios video 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”无效。 请帮助我完成项目工作。 谢谢, 罗汉

2 个答案:

答案 0 :(得分:2)

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

就在你的 xcode 点击

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

}

希望回答你的问题

答案 1 :(得分:0)

我不记得我的范围滑块在哪里,但这个可能更好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