gpt4 book ai didi

iphone - iOS GameCenter、AVAsset 和音频流

转载 作者:行者123 更新时间:2023-11-29 11:18:20 25 4
gpt4 key购买 nike

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

我只是迷路了。信息重载。

我已经用 Love 的音频流代码实现了 Cocoa,但我不知道如何获取通过 G​​ameCenter 收到的 NSData 并将其推送到他的代码中。 http://cocoawithlove.com/2008/09/streaming-and-playing-live-mp3-stream.html

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

最佳答案

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

如果可行,请告诉我!

关于iphone - iOS GameCenter、AVAsset 和音频流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8432246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com