如何将来自 url 的音频保存在本地以供以后播放。我可以使用 avplayer 流式传输音频,但无法将其保存在本地并稍后播放。我尝试使用 avurlasset。谁能帮我?
这是我试图在本地保存的代码:
在这里,我使用 avplayer //code 流式传输音频以保存本地
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:@"status"])
{
AVPlayerItem * item = (AVPlayerItem *)object;
if (item.status == AVPlayerItemStatusReadyToPlay)
{
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *myPathDocs = [documentsDirectory stringByAppendingPathComponent: @"audio.m4a"];
asset=player.currentItem.asset;
NSURL *url1 = [NSURL fileURLWithPath:myPathDocs];
// 5 - Create exporter
AVAssetExportSession *assetEx = [[AVAssetExportSession alloc] initWithAsset:player.currentItem.asset presetName:AVAssetExportPresetAppleM4A];//tried with asset2 also
assetEx.outputURL=[NSURL fileURLWithPath:myPathDocs];
assetEx.outputFileType = AVFileTypeAppleM4A;
assetEx.shouldOptimizeForNetworkUse = YES;
[assetEx exportAsynchronouslyWithCompletionHandler:^{
int status = assetEx.status;
dispatch_async(dispatch_get_main_queue(), ^{
[self exportDidFinish:assetEx];
});
}];
}
}
}
but there is no file created in the documents directory.