views:

528

answers:

2

I was wondering if Apple allowed you to access the MPMediaItem's raw data as an NSData object. If you can, thats great and I would like to know how. If you cannot, then Apple is way too over-protective, and my app that i started is utterly useless. Also if you cannot, Is there any hacky workarounds? I don't care if Apple doesn't accept my app as it would still be cool if me and my friends all had it.

Thanks, Conrad

+2  A: 

You can't, and there are no workaround. An MPMediaItem is not the actual piece of media, it is just the metadata about the media item communicated to the application via RPC from another process. The data for the item itself is not accessible in your address space.

I should note that even if you have the MPMediaItem its data probably is not loaded into the devices memory. The flash on the iPhone is slow and memory is scarce. While Apple may not want you to have access to the raw data backing an MPMediaItem, it is just as likely that they didn't bother dealing with it because they didn't want to invest the time necessary to deal with the APIs. If they did provide access to such a thing it almost certainly would not be as an NSData, but more likely as an NSURL they would give your application that would allow it to open the file and stream through the data.

In any event, if you want the functionality, you should file a bug report asking for.

Also, as a side note, don't mention your age in a bug report you send to Apple. I think it is very cool you are writing apps for the phone, when I was your age I loved experimenting with computers (back then I was working on things written in Lisp). The thing is you cannot legally agree to a contract in the United States, which is why the developer agreement specifically prohibits you from joining. From the first paragraph of the agreement:

You also certify that you are of the legal age of majority in the jurisdiction in which you reside (at least 18 years of age in many countries) and you represent that you are legally permitted to become a Registered iPhone Developer.

If you mention to a WWDR representative that you are not of age of majority they may realize you are in violation of the agreement and be obligated to terminate your developer account. Just a friendly warning.

Louis Gerbarg
My dad did all that stuff and technically, He's the iPhone Developer, so it's all good
ckrames1234
Also, if I were to take the jailbreak path, the music, is stored in /var/mobile/Media/iTunes_Control/Music/, and it is a bunch of randomly named folders with randomly named songs in it. How to decipher the names would be the challenge but its probably in one of the scattered SQLite databases in the "iTunes_Control" folder. Once I build the Toolchain, thats were i'm going next.
ckrames1234
+1  A: 

Hi, you can obtain the media item's data in such way:

-(void)mediaItemToData { // Implement in your project the media item picker

MPMediaItem *curItem = musicPlayer.nowPlayingItem;

NSURL *url = [curItem valueForProperty: MPMediaItemPropertyAssetURL];

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

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

exporter.outputFileType = @"public.mpeg-4";

NSString *exportFile = [[self myDocumentsDirectory] stringByAppendingPathComponent:           
                                                               @"exported.mp4"];

NSURL *exportURL = [[NSURL fileURLWithPath:exportFile] retain];
exporter.outputURL = exportURL; 

// do the export
// (completion handler block omitted) 
[exporter exportAsynchronouslyWithCompletionHandler:
  ^{
 NSData *data = [NSData dataWithContentsOfFile: [[self myDocumentsDirectory] 
                                     stringByAppendingPathComponent: @"exported.mp4"]];

 // Do with data something

   }];

}

This code will work only on ios 4.0 and later

Good luck!

MIchael
i try this code in ios 4.0 but it not work there below is source of code
GhostRider