iphone - 有没有办法使用 iPhone SDK 将 iPod 音乐文件保存到 iPhone 应用程序?

标签 iphone objective-c cocoa-touch iphone-sdk-3.0

有没有办法使用 iPhone SDK 将 iPod 音乐文件保存到 iPhone 应用程序?

最佳答案

当我单击音频 TableView 时,我会在 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];
}

关于iphone - 有没有办法使用 iPhone SDK 将 iPod 音乐文件保存到 iPhone 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4746196/

相关文章:

iphone - 在 AIR/AS3 中使用特定于移动设备的类测试代码时,如何避免出现VerifyError?

iphone - 无法创建持久存储协调器

iphone - 自动续订订阅 : How long a month is?

iphone - 如何制作通用的iOS应用程序

objective-c - uibutton在uitableview的uitableviewcell中设置可见

objective-c - NSToolbarItem 子类上的 IBOutlet 导致图像消失并且交互被禁用

objective-c - UITableView : Hide cells animated?

ios - 从 UIView 层次结构创建 UIImage 时背景透明度丢失

cocoa-touch - MKAnnotationView 不显示标注

iphone - 文本滚动到 UITextView 框边界之外