将URL中的音频保存在本地以供日后播放

时间:2013-03-15 06:46:58

标签: iphone ipad

如何在本地保存来自网址的音频以供日后播放。我可以使用avplayer传输音频,但无法在本地保存并稍后播放。我尝试使用av​​urlasset。谁能帮我?

这是我尝试在本地保存的代码:

  
    
      

这里我使用avplayer播放音频       //用于保存本地的代码

    
  
-(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.

0 个答案:

没有答案