我尝试通过AVAssetExportSession导入歌曲,但无法让它工作。这是我到目前为止尝试过的代码:
NSURL *url = [playingSong valueForProperty:MPMediaItemPropertyAssetURL];
AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:url options:nil];
AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset: songAsset presetName: AVAssetExportPresetPassthrough];
exporter.outputFileType = @"com.apple.coreaudio-format";
NSString *fname = [[NSString stringWithFormat:@"tmp"] stringByAppendingString:@".mp3"];
NSString *tempPath = NSTemporaryDirectory();
NSString *exportFile = [tempPath stringByAppendingPathComponent: fname];
exporter.outputURL = [NSURL fileURLWithPath:exportFile];
[exporter exportAsynchronouslyWithCompletionHandler:^{
self.player = [[SuperpoweredAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:exportFile]];
[self.player play];
}];
通过以下方式打开文件:
player->open([[url path] fileSystemRepresentation]);
即使这样可行,我也很担心这对于音乐播放器来说是否足够快。只要另一个完成,就导入一首歌。
还有其他选择吗?
谢谢!
答案 0 :(得分:2)
如果你有从iTunes资料库获得的MPMediaItem *项目,你可以使用:
player->open([[item assetURL].absoluteString UTF8String]);