Skip to content

Instantly share code, notes, and snippets.

@markd2
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markd2/a8d1de0fb2dc0d1dbe3d to your computer and use it in GitHub Desktop.
Save markd2/a8d1de0fb2dc0d1dbe3d to your computer and use it in GitHub Desktop.
I'm building an AVComposition. The audio plays back fine. The video is blind and silent. After the video's duration has elapsed the audio comes back in. The audio is coming from the itunes library and the video is a mp4 file sitting in the file system. The video plays fine with MPMoviePlayerController.
Here are the segments:
(lldb) po compositionVideoTrack.segments
<__NSArrayM 0x145f84d0>(
<AVCompositionTrackSegment: 0x145f85c0 timeRange [0.000,+440.256] from trackID 1 of asset file:///var/mobile/Applications/11472C08-EB4B-4566-8C09-DAFD90C904F6/Documents/.../globa-soaring_over_sori-italy-m-italy3.mp4 sourceTimeRange [0.000,+440.256]>
)
here's the rest of the composition. no matter where the video is, it's silent, and nothing appears on an AVPlayerLayer.
(lldb) po compositionAudioTrack.segments
<__NSArrayM 0x146cbb30>(
<AVCompositionTrackSegment: 0x146cbc80 timeRange [0.000,+440.256] is empty>,
<AVCompositionTrackSegment: 0x146cbdf0 timeRange [440.256,+229.042] from trackID 1 of asset ipod-library://item/item.m4a?id=1167773541825882613 sourceTimeRange [0.000,+229.042]>,
<AVCompositionTrackSegment: 0x146cbe70 timeRange [669.298,+159.335] from trackID 1 of asset ipod-library://item/item.m4a?id=1167773541825882619 sourceTimeRange [0.000,+159.335]>,
<AVCompositionTrackSegment: 0x146cbef0 timeRange [828.633,+38.243] from trackID 1 of asset ipod-library://item/item.m4a?id=1167773541825883049 sourceTimeRange [0.000,+38.243]>,
<AVCompositionTrackSegment: 0x146cbf70 timeRange [866.876,+179.212] from trackID 1 of asset ipod-library://item/item.m4a?id=1167773541825882617 sourceTimeRange [0.000,+179.212]>,
<AVCompositionTrackSegment: 0x146cbff0 timeRange [1046.088,+200.040] from trackID 1 of asset ipod-library://item/item.m4a?id=1167773541825882622 sourceTimeRange [0.000,+200.040]>,
<AVCompositionTrackSegment: 0x146cc070 timeRange [1246.128,+162.145] from trackID 1 of asset ipod-library://item/item.m4a?id=1167773541825882606 sourceTimeRange [0.000,+162.145]>,
<AVCompositionTrackSegment: 0x146cc0f0 timeRange [1408.273,+122.508] from trackID 1 of asset ipod-library://item/item.m4a?id=1167773541825882597 sourceTimeRange [0.000,+122.508]>,
<AVCompositionTrackSegment: 0x146cc170 timeRange [1530.781,+215.133] from trackID 1 of asset ipod-library://item/item.m4a?id=1167773541825882602 sourceTimeRange [0.000,+215.133]>,
<AVCompositionTrackSegment: 0x146cc1f0 timeRange [1745.914,+252.262] from trackID 1 of asset ipod-library://item/item.m4a?id=1167773541825882612 sourceTimeRange [0.000,+252.262]>,
<AVCompositionTrackSegment: 0x146cc270 timeRange [1998.176,+170.017] from trackID 1 of asset ipod-library://item/item.m4a?id=1167773541825882615 sourceTimeRange [0.000,+170.017]>,
<AVCompositionTrackSegment: 0x146cc2f0 timeRange [2168.192,+156.711] from trackID 1 of asset ipod-library://item/item.m4a?id=1167773541825882601 sourceTimeRange [0.000,+156.711]>,
<AVCompositionTrackSegment: 0x146cc370 timeRange [2324.904,+161.379] from trackID 1 of asset ipod-library://item/item.m4a?id=1167773541825882607 sourceTimeRange [0.000,+161.379]>,
<AVCompositionTrackSegment: 0x146cc3f0 timeRange [2486.282,+164.049] from trackID 1 of asset ipod-library://item/item.m4a?id=1167773541825882598 sourceTimeRange [0.000,+164.049]>,
<AVCompositionTrackSegment: 0x146cc470 timeRange [2650.331,+188.291] from trackID 1 of asset ipod-library://item/item.m4a?id=1167773541825882614 sourceTimeRange [0.000,+188.291]>,
<AVCompositionTrackSegment: 0x146cc4f0 timeRange [2838.622,+196.394] from trackID 1 of asset ipod-library://item/item.m4a?id=1167773541825882618 sourceTimeRange [0.000,+196.394]>,
<AVCompositionTrackSegment: 0x146cc570 timeRange [3035.016,+205.636] from trackID 1 of asset ipod-library://item/item.m4a?id=1167773541825882620 sourceTimeRange [0.000,+205.636]>,
<AVCompositionTrackSegment: 0x146cc660 timeRange [3240.652,+157.176] from trackID 1 of asset ipod-library://item/item.m4a?id=1167773541825882603 sourceTimeRange [0.000,+157.176]>,
<AVCompositionTrackSegment: 0x146cc6e0 timeRange [3397.828,+205.241] from trackID 1 of asset ipod-library://item/item.m4a?id=1167773541825882616 sourceTimeRange [0.000,+205.241]>,
<AVCompositionTrackSegment: 0x146cc760 timeRange [3603.069,+305.110] from trackID 1 of asset ipod-library://item/item.m4a?id=1167773541825882604 sourceTimeRange [0.000,+305.110]>,
<AVCompositionTrackSegment: 0x146cc7e0 timeRange [3908.179,+208.538] from trackID 1 of asset ipod-library://item/item.m4a?id=1167773541825882599 sourceTimeRange [0.000,+208.538]>,
<AVCompositionTrackSegment: 0x146cc860 timeRange [4116.718,+167.973] from trackID 1 of asset ipod-library://item/item.m4a?id=1167773541825882600 sourceTimeRange [0.000,+167.973]>,
<AVCompositionTrackSegment: 0x146cc8e0 timeRange [4284.691,+108.066] from trackID 1 of asset ipod-library://item/item.m4a?id=1167773541825882625 sourceTimeRange [0.000,+108.066]>,
<AVCompositionTrackSegment: 0x146cc960 timeRange [4392.757,+333.996] from trackID 1 of asset ipod-library://item/item.m4a?id=1167773541825882609 sourceTimeRange [0.000,+333.996]>,
<AVCompositionTrackSegment: 0x146cc9e0 timeRange [4726.753,+157.756] from trackID 1 of asset ipod-library://item/item.m4a?id=1167773541825882605 sourceTimeRange [0.000,+157.756]>,
<AVCompositionTrackSegment: 0x146cca60 timeRange [4884.509,+471.829] from trackID 1 of asset ipod-library://item/item.m4a?id=1167773541825882621 sourceTimeRange [0.000,+471.829]>
)
And how I'm building it:
- (void) buildComposition {
self.composition = nil;
AVMutableComposition *composition = [[AVMutableComposition alloc] init];
NSError *error;
BOOL result;
// housekeeping
AVMutableCompositionTrack *compositionAudioTrack =
[composition addMutableTrackWithMediaType: AVMediaTypeAudio
preferredTrackID: kCMPersistentTrackID_Invalid];
AVMutableCompositionTrack *compositionVideoTrack =
[composition addMutableTrackWithMediaType: AVMediaTypeVideo
preferredTrackID: kCMPersistentTrackID_Invalid];
    CMTime currentTime = kCMTimeZero;
int index = 0;
for (id mediaItem in self.mediaItems) {
NSURL *assetURL;
BOOL isVideoURL = [mediaItem isKindOfClass: NSURL.class];
if (isVideoURL) {
assetURL = mediaItem;
} else {
assetURL =
[[mediaItem mediaItem] valueForProperty: MPMediaItemPropertyAssetURL];
}
AVURLAsset *asset = [AVURLAsset URLAssetWithURL: assetURL
options: nil];
CMTimeRange assetTimeRange = CMTimeRangeMake(kCMTimeZero, asset.duration);
if (isVideoURL) {
NSArray *tracks = [asset tracksWithMediaType: AVMediaTypeVideo];
NSLog (@"%@" , tracks);
AVAssetTrack *track =
[[asset tracksWithMediaType: AVMediaTypeVideo] firstObject];
result =
[compositionVideoTrack insertTimeRange: assetTimeRange
ofTrack: track
atTime: currentTime
error: &error];
} else {
AVAssetTrack *track =
[[asset tracksWithMediaType: AVMediaTypeAudio] firstObject];
result =
[compositionAudioTrack insertTimeRange: assetTimeRange
ofTrack: track
atTime: currentTime
error: &error];
}
if (!result) {
NSLog (@"oops - %@", error);
}
CMTimeRange timeRange = CMTimeRangeMake(currentTime, asset.duration);
// housekeeping
currentTime = CMTimeAdd(currentTime, asset.duration);
}
self.composition = composition;
} // buildComposition
and playing it like
AVPlayerItem *item = [[AVPlayerItem alloc] initWithAsset: self.composition];
self.player = [AVPlayer playerWithPlayerItem: item];
[self addVideoSurface];
ClassBuilderAppDelegate *appdelegate = ClassBuilderAppDelegate.appDelegate;
[appdelegate.window addSubview: self.playerView]; // the player view has an AVPlayerLayer. doesn't matter if I do this or not.
__weak typeof(self) weakSelf = self;
__weak typeof(_player) weakPlayer = self.player;
self.avObserver = [weakPlayer addPeriodicTimeObserverForInterval: CMTimeMake(1, 1)
queue: NULL // main queue
usingBlock: ^(CMTime time) {
NSLog (@"observer 1");
[weakSelf handleTimeChange: time];
}];);
[self.player play];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment