iOS GameCenter,AVAsset和Audio Streaming

时间:2011-12-08 14:10:01

标签: iphone objective-c ios ipad

我从设备iTunes资料库中获取一首歌并将其推入AVAsset:

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

    MPMediaItem *song = [arr objectAtIndex:0];

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

然后我有这个Game Center接收数据的方法:

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

我在解决如何通过GameCenter发送这个AVAsset然后让它在接收设备上播放时遇到了很多麻烦。

我读过: 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

我迷失了。信息超载。

我已经实现了Cocoa With Love的音频流代码,但我无法弄清楚如何通过GameCenter获取我收到的NSData并将其推送到他的代码中。 http://cocoawithlove.com/2008/09/streaming-and-playing-live-mp3-stream.html

有人可以帮我解决这个问题吗?谢谢!

1 个答案:

答案 0 :(得分:2)

据我所知,AVAsset不是真正的歌曲。因此,如果你想发送所选歌曲的实际数据,你需要尝试这样的事情:

- (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
}

现在,在另一台设备上,您需要将收到的NSData写入磁盘,而不是使用新的URL创建AVAsset。像这样:

- (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
}

让我知道这是否有效!