Question

I've created an AVMutableComposition that consists of a bunch of audio tracks that start at specific times. From there, following Apple recommendations, i turned it into an AVComposition before playing it with AVPlayer.

It all works fine playing this AVPlayer item, but if I pause it and then continue, all the tracks in the composition appear to slip back about 0.2 seconds relative to each other (i.e., they bunch up). Hitting pause and continuing several times compounds the effect and the overlap is more significant (basically if I hit it enough, I will end up with all 8 tracks playing simultaneously).

if (self.player.rate > 0.0) {
    //if player is playing, pause
    [self.player pause];

} else {

    if (self.player) {
      [self.player play];
       return;

        }

     */CODE CREATING COMPOSITION - missed out big chunk of code relating to finding the track and retrieving its position and scale/*


        NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES]
                                                            forKey:AVURLAssetPreferPreciseDurationAndTimingKey];

        AVURLAsset *sourceAsset = [[AVURLAsset alloc] initWithURL:url options:options];

        //calculate times
        NSNumber *time = [soundArray1 objectAtIndex:1]; //this is the time scale - e.g. 96 or 120 etc.

        double timenow = [time doubleValue];

        double insertTime = (240*y);

                    AVMutableCompositionTrack *track =
        [composition addMutableTrackWithMediaType:AVMediaTypeAudio
                                 preferredTrackID:kCMPersistentTrackID_Invalid];

        //insert the audio track from the asset into the track added to the mutable composition
        AVAssetTrack *myTrack = [[sourceAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
        CMTimeRange myTrackRange = myTrack.timeRange;
        NSError *error = nil;
        [track insertTimeRange:myTrackRange
                                      ofTrack:myTrack
                                       atTime:CMTimeMake(insertTime, timenow) 
                                        error:&error];

        [sourceAsset release];







    }

} 
    AVComposition *immutableSnapshotOfMyComposition = [composition copy];

        AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:immutableSnapshotOfMyComposition];
        self.player = [[AVPlayer alloc] initWithPlayerItem:playerItem];
        NSLog(@"here");

        [self.player play];

Thanks

Was it helpful?

Solution

OK, this feels a little hacky, but it definitely works if anybody is stuck. If someone has a better answer, do let me know!

Basically, I just save the player.currentTime of the track when I hit pause and remake the track when i hit play, just starting from the point at which i paused it. No discernible delay, but I'd still be happier without wasting this extra processing.

Make sure you properly release your player item after you hit pause, otherwise you'll end up with a giant stack of AVPlayers!

OTHER TIPS

I have a solution that is a bit less hacky but still hacky.

The solution comes from the fact that I noticed that if you seeked on the player, the latency between audio and video introduced by pausing disappeared.

Hence: just save the player.currentTime just before pausing and, player seekToTime just before playing again. It works pretty well on iOS 6, haven't tested on other versions yet.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top