我能够成功地创建一个简单的应用程序,播放声音使用下面的代码。然而,问题是,如果我将资产从mp3更改为m4a (使用aac编码)。没有声音产生,但乐曲的持续时间与添加的样本的长度相匹配。
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];有人能帮上忙吗?
发布于 2012-07-11 07:35:27
我最终把这个问题张贴在所有地方,并最终与苹果公司一起创建了一个bug报告。
我解决这个问题的方法是在一开始就有一个步骤,将m4a下载到应用程序的临时目录中,然后从那里将其添加到组合中。就像一种护身符。
我不会详细介绍如何做到这一点的代码,但由于我的应用程序使用了流行的AFNetworking库,以下是关键部分。
1: AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:songURLRequest];
2: NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"song.m4a"];
3: operation.outputStream = [NSOutputStream outputStreamToFileAtPath:filePath append:NO];第1行:设置AFHTTPRequestOperation第2行:将本地路径设置为保存文件的字符串第3行:将AFHTTPRequestOperation上的outputStream设置为本地路径
希望这对将来有帮助的人!
https://stackoverflow.com/questions/11356549
复制相似问题