我正从我的服务器收到缓冲区数据;
len = [inputStream read:buffer maxLength:sizeof(buffer)]; //Getting data from server
if (len > 0) {
NSData *audioData = [[NSData alloc]initWithBytes:buffer length:len];
if (nil != audioData) {
[[mainSounds alloc]initSounds:audioData];
}
}
现在我的 mainSounds 类
-(id)initSounds : (NSData *) audioData{
NSError *error;
testAudioPlayer=[[AVAudioPlayer alloc] initWithData:audioData error:&error];
NSLog(@"Player played %d and error %@",[testAudioPlayer play],[error userInfo]);
return self;
}
我还播放了静态音频文件,播放效果很好。 给NSLog - >球员打了1个
但是当我尝试我的服务器数据时,它说播放器播放0并且错误{}
我在NSLog中检查来自服务器的NSData
AUDIO DATA <6f06af08 b209cd09 0a0aed0a 000c870c 6f0c5b0c dc0cc70d 820ec00e cc0e270f f20fd710 5d117b1c 6136013f aa28c112 050d070b 291b1235 812faf14 8e0a050b 18080a>
是我的NSData中的任何问题..
答案 0 :(得分:0)
尝试使用以下代码
NSString *urlString = [[NSString alloc] initWithData: audioData encoding:NSUTF8StringEncoding];
NSURL *url = [[NSURL alloc] initWithString:urlString];
NSData *finalDATA = [NSData dataWithContentsOfURL:url];
[[mainSounds alloc]initSounds: finalDATA];
并在您的initSounds
方法
NSError *error;
testAudioPlayer =[[AVAudioPlayer alloc] initWithData:finalDATA error:&error];
[testAudioPlayer play];