Frage

I am able to successfully create a simple app that plays sound using the code below. The problem however is if I change the asset from being an mp3 to an m4a (with aac encoding). No sound is produced, yet the duration of the composition matches the length of the added sample.

NSError *error;

AVMutableComposition* composition = [AVMutableComposition composition];
AVURLAsset* audioAsset1 = [[AVURLAsset alloc]initWithURL:[NSURL URLWithString:@"http://www.myserver.com/sample.mp3"] options:nil];

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

[audioTrack1 insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAsset1.duration)
                     ofTrack:[[audioAsset1 tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]
                      atTime:kCMTimeZero
                       error:&error];

NSLog(@"%@", error);

playerItem = [AVPlayerItem playerItemWithAsset:composition];

player = [AVPlayer playerWithPlayerItem:playerItem];

[player play];

Can anyone help?

War es hilfreich?

Lösung

I ended up posting this question everywhere and ended up creating a bug report with apple.

How I solved the issue is to have a step at the start that downloads the m4a into the apps temp directory and then add it to the composition from there. Works like a charm.

I won't go into detailed code on how to do it but as my app makes use of the popular AFNetworking library, here is the key parts.

1: AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:songURLRequest];
2: NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"song.m4a"];
3: operation.outputStream = [NSOutputStream outputStreamToFileAtPath:filePath append:NO];

Line 1: Setup an AFHTTPRequestOperation Line 2: Setup a local path as a string where the file will be saved Line 3: Set the outputStream on the AFHTTPRequestOperation to the local path

Hope this helps someone in the future!

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top