从 iPod 库中复制项目

Posted

技术标签:

【中文标题】从 iPod 库中复制项目【英文标题】:Copy item from iPod Library 【发布时间】:2012-09-08 19:12:45 【问题描述】:

我正在尝试将一个项目从 iPod 库复制到我的本地存储空间 - 以供以后播放。我有项目 URl,但它是 (ipod-library://item/item.mp3?id=2398084975506389321) 知道如何访问实际文件吗?

谢谢, 瑞克

【问题讨论】:

【参考方案1】:

这将工作https://gist.github.com/3304992

-(void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection
    NSString *tempPath = NSTemporaryDirectory();
    int i=1;
    for (MPMediaItem *theItem in mediaItemCollection.items) 
        NSURL *url = [theItem valueForProperty:MPMediaItemPropertyAssetURL];
        AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL:url options:nil];
        AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset: songAsset presetName: AVAssetExportPresetPassthrough];
        exporter.outputFileType = @"com.apple.coreaudio-format";
        NSString *fname = [[NSString stringWithFormat:@"%d",i] stringByAppendingString:@".caf"];
        ++i;
        NSString *exportFile = [tempPath stringByAppendingPathComponent: fname];
        exporter.outputURL = [NSURL fileURLWithPath:exportFile];
        [exporter exportAsynchronouslyWithCompletionHandler:^
            //Code for completion Handler
        ];
    
    [picker dismissViewControllerAnimated:YES completion:Nil];

使用 MPMediaPickerController 选择媒体

【讨论】:

public void ItemsPicked (object sender, ItemsPickedEventArgs e) MPMediaItem[] mediaItems; NSUrl mediaURL = 新 NSUrl(); mediaItems = e.MediaItemCollection.Items; mediaURL = (NSUrl) mediaItems[0].ValueForProperty(MPMediaItemProperty.AssetUrl);资产 = AVUrlAsset.FromUrl(mediaURL, null); exportURL = NSUrl.FromFilename(Path.Combine(appDelegate.store.mediaPath, "tmp.m4a"));出口=新的AVAssetExportSession(资产,AVAssetExportSession.PresetAppleM4A); export.OutputFileType = AVFileType.AppleM4A;出口.OutputUrl = 出口网址; export.ExportAsynchronously(复制完成); 【参考方案2】:

这就是我在 Objective-C 中的做法:

#import <CoreMedia/CoreMedia.h>
#import <AVFoundation/AVFoundation.h>
#import <CoreAudio/CoreAudio.h>

// or [NSURL URLWithString:@"ipod-library://item/item.mp3?id=2398084975506389321"]
NSURL *assetURL = [item valueForProperty:MPMediaItemPropertyAssetURL];
NSMutableData *data = [[NSMutableData alloc] init];

const uint32_t sampleRate = 16000;
const uint16_t bitDepth = 16;
const uint16_t channels = 2;

NSDictionary *opts = [NSDictionary dictionary];
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:assetURL options:opts];
AVAssetReader *reader = [[AVAssetReader alloc] initWithAsset:asset error:NULL];
NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
    [NSNumber numberWithInt:kAudioFormatLinearPCM], AVFormatIDKey,
    [NSNumber numberWithFloat:(float)sampleRate], AVSampleRateKey,
    [NSNumber numberWithInt:bitDepth], AVLinearPCMBitDepthKey,
    [NSNumber numberWithBool:NO], AVLinearPCMIsNonInterleaved,
    [NSNumber numberWithBool:NO], AVLinearPCMIsFloatKey,
    [NSNumber numberWithBool:NO], AVLinearPCMIsBigEndianKey,
    nil];

AVAssetReaderTrackOutput *output = [[AVAssetReaderTrackOutput alloc] initWithTrack:[[asset tracks] objectAtIndex:0] outputSettings:settings];
[asset release];
[reader addOutput:output];
[reader startReading];

// read the samples from the asset and append them subsequently
while ([reader status] != AVAssetReaderStatusCompleted) 
    CMSampleBufferRef buffer = [output copyNextSampleBuffer];
    if (buffer == NULL) continue;

    CMBlockBufferRef blockBuffer = CMSampleBufferGetDataBuffer(buffer);
    size_t size = CMBlockBufferGetDataLength(blockBuffer);
    uint8_t *outBytes = malloc(size);
    CMBlockBufferCopyDataBytes(blockBuffer, 0, size, outBytes);
    CMSampleBufferInvalidate(buffer);
    CFRelease(buffer);
    [data appendBytes:outBytes length:size];
    free(outBytes);

[output release];

这里的data 将包含曲目的原始 PCM 数据。请注意,您不能直接访问歌曲或视频的文件,只能通过此方法访问其数据。您可以使用 e 压缩它。 G。 FLAC (that's how I'm processing it in my tweak)。

由于 MonoTouch 与 Objective-C 类和方法名称有 1:1 的映射关系,因此应该很容易复制。 :)

【讨论】:

以上是关于从 iPod 库中复制项目的主要内容,如果未能解决你的问题,请参考以下文章

将音乐复制到 Ipod

从 ipod 库导入媒体

在我的应用程序中复制 iphone 库的音乐文件

从 git 存储库中排除 ios 项目的哪些文件? [复制]

iPhone SDK:如何将从 iPod 库中挑选的音频文件写入我的应用程序的文档文件夹?

获取 iPod 库中项目的专辑年份?