【问题标题】:iOS GameCenter, AVAsset, and Audio StreamingiOS GameCenter、AVAsset 和音频流
【发布时间】:2011-12-08 14:10:01
【问题描述】:

我从设备的 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

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

【问题讨论】:

    标签: iphone objective-c ios ipad


    【解决方案1】:

    据我所知,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
    }
    

    让我知道这是否有效!

    【讨论】:

    • 米海,我很欣赏这里的代码,但我知道如何通过游戏中心发送文件,但我的问题是,假设一首歌是 3 兆字节,我不想等到整个文件在我开始播放之前发送,我希望第二个设备流式传输它。所以我想我需要发送数据块(包括整首歌曲),然后解析这些数据块并在接收设备上播放它们。你知道我的意思吗?
    • 我知道你要做什么了。我唯一可以确定的是,您不能直接发送AVAsset,因为它不包含实际的歌曲数据。所以我认为整个 GameCenter 方法不足以满足您的需求。试试看this 是否可以帮助您在两部 iPhone 之间建立一个套接字流并像这样发送歌曲数据......这对您来说是一个选项还是 GameCenter 是唯一的选项?
    • 我还发现了this,这听起来有点有前途...看看你能用它做什么!
    • 我可以不使用游戏中心,但我的最大障碍是寻找并连接到其他设备。我什至不知道从哪里开始。
    猜你喜欢
    • 2017-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-08
    相关资源
    最近更新 更多