1
votes

I get a song from the device iTunes library and shove it into an AVAsset:

- (void)mediaPicker: (MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection
{
    NSArray *arr = mediaItemCollection.items;

    MPMediaItem *song = [arr objectAtIndex:0];

    AVAsset *songAsset = [AVAsset assetWithURL:[song valueForProperty:MPMediaItemPropertyAssetURL]];
}

Then I have this Game Center method for receiving data:

- (void)match:(GKMatch *)match didReceiveData:(NSData *)data fromPlayer:(NSString *)playerID

I'm having a LOT of trouble figuring out how to send this AVAsset via GameCenter and then have it play on the receiving device.

I've read through: http://developer.apple.com/library/ios/#documentation/MusicAudio/Reference/AudioStreamReference/Reference/reference.html#//apple_ref/doc/uid/TP40006162

http://developer.apple.com/library/ios/#documentation/AudioVideo/Conceptual/MultimediaPG/UsingAudio/UsingAudio.html#//apple_ref/doc/uid/TP40009767-CH2-SW5

http://developer.apple.com/library/mac/#documentation/AVFoundation/Reference/AVAudioPlayerClassReference/Reference/Reference.html

http://developer.apple.com/library/mac/#documentation/MusicAudio/Conceptual/AudioQueueProgrammingGuide/Introduction/Introduction.html

I am just lost. Information overload.

I've implemented Cocoa With Love's Audio Stream code, but I can't figure out how to take the NSData I receive through GameCenter and shove it into his code. http://cocoawithlove.com/2008/09/streaming-and-playing-live-mp3-stream.html

Can someone please help me figure this out? Thanks!

1

1 Answers

2
votes

As far as I know the AVAsset is not the actual song. So if you want to send the actual data of the picked song you need to try something like this:

- (void)mediaPicker: (MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection
{
    NSArray *arr = mediaItemCollection.items;
    MPMediaItem *song = [arr objectAtIndex:0];
    NSData *songData = [NSData dataWithContentsOfURL:[song valueForProperty:MPMediaItemPropertyAssetURL]];
    // Send the songData variable trough GameCenter
}

On the other device now you need to write the NSData you receive to the disk somewhere and than create an AVAsset with it's newly URL. Like this:

- (void)match:(GKMatch *)match didReceiveData:(NSData *)data fromPlayer:(NSString *)playerID
{
    NSString *url = NSTemporaryDirectory();
    [url stringByAppendingPathComponent:<#audio_file_name#>];

    // Make sure there is no other file with the same name first
    if ([[NSFileManager defaultManager] fileExistsAtPath:url]) {
        [[NSFileManager defaultManager] removeItemAtPath:url error:nil];
    }

    [data writeToFile:url atomically:NO];

    AVURLAsset *urlAsset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:url] options:nil];

    // Do whatever you want with your new asset
}

Let me know if that works!