【问题标题】:Is there a way to save iPod music files to iPhone App using iPhone SDK?有没有办法使用 iPhone SDK 将 iPod 音乐文件保存到 iPhone 应用程序?
【发布时间】:2011-01-20 10:52:54
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    当我点击音频表视图时,我在表视图上显示音频列表,然后我在 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 APP 中吗?
    • 是的,您可以将其保存到文档目录
    • 在哪里声明 AVAssetExportSession 和 mAppDelegate
    • @GhostRider:我面临与meetpd 相同的问题。在这里,您能否提供更多有关UploadSong: 功能的详细信息?
    • 上传歌曲功能调用网络服务将歌曲上传到网络服务。请说清楚你想知道什么我会告诉一切
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-05
    • 1970-01-01
    • 1970-01-01
    • 2011-05-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多