iOS Frame by Frame video playback forward/backward

后端 未结 2 835
伪装坚强ぢ
伪装坚强ぢ 2021-01-14 04:04

I\'d like to show a video on an iOS device in slow motion.

My view contains a video (~2 seconds long) and a slider.

The user can move the slider and step (f

2条回答
  •  梦毁少年i
    2021-01-14 04:45

    AVFoundation is a very powerful framework but requires a little more typing and understanding to use than MPMoviePlayerController. I highly recommend Dinesh's recommendation to watch the WWDC videos, look at the sample code, and read the documentation. The documentation is quite good for getting a conceptual understanding, but it lacks some of the details when it comes to the particulars of actual video formats. For that, sample code and experimentation is good.

    If you want to go frame by frame then I think AVPlayerItem stepByCount: is the way to go.

    // Setup a PlayerItem and a Player
    NSURL* movieURL = [NSBundle URLForResource:@"movie" withExtension:@"mov" subdirectory:@"" inBundleWithURL:[NSBundle mainBundle].bundleURL];
    AVURLAsset* movieAsset = [AVURLAsset assetWithURL:movieURL];
    AVPlayerItem* playerItem = [AVPlayerItem playerItemWithAsset:movieAsset];
    AVPlayer* player = [AVPlayer playerWithPlayerItem:playerItem];
    
    // setup the layer so you can see it
    AVPlayerLayer* playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
    playerLayer.frame = self.view.bounds;
    [self.view.layer addSublayer:playerLayer];
    
    // Verify our assumptions, in production code you should do something better
    NSAsset(player.currentItem.canStepForward, @"Assume this format can step forward");    
    NSAsset(player.currentItem.canStepBackward, @"Assume this format can step backward");
    
    
    [player.currentItem stepByCount:1];  // step forward a frame
    
    [player.currentItem stepByCount:-1];  // step backward a frame
    

提交回复
热议问题