2011-01-20 82 views
3

我們可以將iPod音樂庫文件(歌曲)存儲到iPhone App文件夾嗎? 如果是的請建議我該怎麼做?將iPod音樂庫音頻文件複製到Iphone App文件夾?

請幫助

感謝

+1

這個問題與上一個問題有什麼不同? http://stackoverflow.com/questions/4743959/how-to-import-music-file-from-iphone-music-library-to-iphone-app – spender 2011-01-20 11:12:16

+0

我曾用兩種方式將數據合成一首歌曲在下面提到 – GhostRider 2011-01-20 11:26:28

回答

4

i顯示了在表視圖音頻列表當我再音頻tableview中單擊歌曲我在NSData的持有上傳

-(void)startVideoStart:(NSNumber*)number 

{ 

    MPMediaItem *song = [miPodSongsArray objectAtIndex:[number intValue]]; 

    songTitle = [song valueForProperty: MPMediaItemPropertyTitle]; 

    NSURL *url = [song valueForProperty:MPMediaItemPropertyAssetURL]; 

    [NSThread detachNewThreadSelector:@selector(loadAudioInBackground:) toTarget:self withObject:url]; 

    [self performSelector:@selector(UploadSong:) withObject:songDict afterDelay:10]; 

} 

-(void)loadAudioInBackground:(NSURL*)url 

{ 

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init]; 


    NSLog(@"%@",url); 

    AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL: url options:nil]; 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

    NSString *documentsDirectory = [paths objectAtIndex:0]; 

    NSLog (@"compatible presets for songAsset: %@",[AVAssetExportSession exportPresetsCompatibleWithAsset:songAsset]); 

    AVAssetExportSession *exporter = [[AVAssetExportSession alloc] 
             initWithAsset: songAsset 
             presetName: AVAssetExportPresetPassthrough]; 

    NSLog (@"created exporter. supportedFileTypes: %@", exporter.supportedFileTypes); 

    exporter.outputFileType = @"com.apple.m4a-audio"; 

    NSString *exportFile = [documentsDirectory stringByAppendingPathComponent: @"item.m4a"]; 

    NSError *error1; 

    if([[NSFileManager defaultManager] fileExistsAtPath:exportFile]) 
    { 

     [[NSFileManager defaultManager] removeItemAtPath:exportFile error:&error1]; 

} 

    NSURL* exportURL = [[NSURL fileURLWithPath:exportFile] retain]; 

    exporter.outputURL = exportURL; 

    // do the export 
    [exporter exportAsynchronouslyWithCompletionHandler:^{ 

     NSData *data1 = [NSData dataWithContentsOfFile: [documentsDirectory stringByAppendingPathComponent: @"item.m4a"]]; 

     //NSLog(@"%@",data1); 

     if (songDict) { 

      [songDict release]; 

      songDict=nil; 

     } 

     songDict= [[NSMutableDictionary alloc]init]; 

     [songDict setValue:data1 forKey:@"AudioData"]; 



     [songDict setValue:songTitle forKey:@"AudioName"]; 

     [songDict setValue:[[mAppDelegate.userInfoArray objectAtIndex:1]valueForKey:@"user_id"] forKey:@"user_id"]; 

     //NSLog(@"%@",infoDict); 

     mAppDelegate.uploadType = @"Audio"; 

     int exportStatus = exporter.status; 

     switch (exportStatus) { 

      case AVAssetExportSessionStatusFailed: { 

       // log error to text view 
       NSError *exportError = exporter.error; 

       NSLog (@"AVAssetExportSessionStatusFailed: %@", exportError); 

       //  errorView.text = exportError ? [exportError description] : @"Unknown failure"; 

       //errorView.hidden = NO; 

       break; 
      } 

      case AVAssetExportSessionStatusCompleted: { 

       NSLog (@"AVAssetExportSessionStatusCompleted"); 


       break; 
      } 

      case AVAssetExportSessionStatusUnknown: { NSLog (@"AVAssetExportSessionStatusUnknown"); 
break; 
} 
      case AVAssetExportSessionStatusExporting: { NSLog (@"AVAssetExportSessionStatusExporting"); 
break; 
} 

    case AVAssetExportSessionStatusCancelled: { NSLog (@"AVAssetExportSessionStatusCancelled"); 
break; 
} 

case AVAssetExportSessionStatusWaiting: { 
NSLog (@"AVAssetExportSessionStatusWaiting"); 
break; 
} 

     default: 
{ NSLog (@"didn't get export status"); 
break; 
} 
     } 

    }]; 

    [pool release]; 



} 
相關問題