我正在尝试将mp3设置为视频的音轨。但是,我在AVURLAsset中找不到任何曲目。这个mp3在我的项目中,所以它是一个本地文件。
我的代码在下面,我也试图获得任何曲目,但这也回到了范围之外。
AVURLAsset *blank = [AVURLAsset URLAssetWithURL:[NSURL URLWithString:[NSBundle.mainBundle pathForResource:@"empty" ofType:@"mp3"]] options:nil];
if (blank != nil)
{
// Out of range error occurs here.
AVAssetTrack *tmp = [blank tracksWithMediaType:AVMediaTypeAudio][0];
[audioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, tmp.duration) ofTrack:tmp atTime:kCMTimeZero error:nil];
}
我错过了什么?我对视频文件执行相同类型的过程没有问题。任何帮助将不胜感激!
更新
经过一番研究后,我发现轨道可能尚未加载,我可以请求加载它们。我尝试了同样的结果:(
我也试过了不同的MP3和WAV文件,结果也一样!帮助!
AVURLAsset *blank = [AVURLAsset URLAssetWithURL:[NSURL URLWithString:[NSBundle.mainBundle pathForResource:@"empty" ofType:@"mp3"]] options:nil];
//AVURLAsset *blank = [AVURLAsset URLAssetWithURL:[NSURL URLWithString:[NSBundle.mainBundle pathForResource:@"0917" ofType:@"wav"]] options:nil];
if (blank != nil)
{
if ([blank isPlayable])
{
mediaReady = NO;
NSString *tracksKey = @"tracks";
[blank loadValuesAsynchronouslyForKeys:@[tracksKey] completionHandler:
^{
NSError *error;
AVKeyValueStatus status = [blank statusOfValueForKey:tracksKey error:&error];
if (status == AVKeyValueStatusLoaded) {
// The asset is ready at this point
NSArray *tmp = [blank tracks];
//NSArray *tmp = [blank tracksWithMediaType:AVMediaTypeAudio];
if ([tmp count])
{
[audioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, Asset1.duration) ofTrack:[tmp objectAtIndex:0] atTime:kCMTimeZero error:nil];
}
}
mediaReady = YES;
}];
while (!mediaReady)
{
NSLog(@"Waiting...");
[NSThread sleepForTimeInterval:0.5f];
}
}
}
else
{
NSLog(@"Not Playable");
}