从URI获取的音频流,并发挥它在iPhone [英] Get an audio stream from URI and play it on iPhone
问题描述
我想从URL获得音频流和发挥它在iPhone上。不幸的是,如何发挥该流只有C#示例,但我需要在iPhone上的框架Objective C的一个实现。我已经试过我自己使用不同的音频框架,但它是所有成功。这是在C#中:
=响应httpWebRequest.GetResponse();
使用(流流= response.GetResponseStream())
{
使用(声音播放球员=新声音播放(流))
{
player.PlaySync();
}
}
我应该在客观C至从那个URL的流音频吗?在这个网址我没有一个mp3文件什么的,我只是得到一个流。也许我必须在某种程度上流下载到iPhone的音频文件,然后发挥它摆脱滞后的。
我试过这样:
AVPlayer *玩家= [AVPlayer playerWithURL:URL]
[播放器播放];
和这样的:
AVAudioPlayer * theAudio = [[AVAudioPlayer页头] initWithContentsOfURL:URL错误:无]; [theAudio setVolume:0.50]; //设置音量 [theAudio玩]
但是都没有成功。
我测试了我的网址,它工作得很好的浏览器。
这是code的工作:
的NSData * _objectData = [NSData的dataWithContentsOfURL:URL]
NSError *错误; AVAudioPlayer * audioPlayer = [[AVAudioPlayer页头] initWithData:_objectData错误:&放大器;错误]
audioPlayer.numberOfLoops = 0;
audioPlayer.volume = 1.0F;
[audioPlayer prepareToPlay]。 如果(audioPlayer ==无)
的NSLog(@%@,[错误描述]);
其他
[audioPlayer播放];
的NSString * resourcePath =网址; //您的网址
的NSData * _objectData = [NSData的dataWithContentsOfURL:[NSURL URLWithString:resourcePath]];
NSError *错误;app.audioPlayer = [[AVAudioPlayer页头] initWithData:_objectData错误:&放大器;错误]
app.audioPlayer.numberOfLoops = 0;
app.audioPlayer.volume = 1.0F;
[app.audioPlayer prepareToPlay]。如果(app.audioPlayer ==无)
的NSLog(@%@,[错误描述]);
其他
[app.audioPlayer玩]
I would like to get an audio stream from URL and play it on iPhone. Unfortunately, there is only C# example of how to play that stream, but I need a realization on Objective C in iPhone frameworks. I have tried my self to use different audio frameworks but it was all unsuccessful. This is what in C#:
response = httpWebRequest.GetResponse();
using (Stream stream = response.GetResponseStream())
{
using (SoundPlayer player = new SoundPlayer(stream))
{
player.PlaySync();
}
}
What should I do in objective C to get a stream audio from that url? In that URL I don't have an mp3 file or something, I just get a stream. Probably I must somehow download that stream to an iPhone audio file and then play it to get rid of lags.
I tried this:
AVPlayer *player = [AVPlayer playerWithURL:url];
[player play];
And this:
AVAudioPlayer *theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL: url error:nil];
[theAudio setVolume :0.50]; //setting the volume
[theAudio play];
Neither worked. I have tested my url, it works well in browser.
This is the code that worked:
NSData *_objectData = [NSData dataWithContentsOfURL:url];
NSError *error;
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithData:_objectData error:&error];
audioPlayer.numberOfLoops = 0;
audioPlayer.volume = 1.0f;
[audioPlayer prepareToPlay];
if (audioPlayer == nil)
NSLog(@"%@", [error description]);
else
[audioPlayer play];
NSString* resourcePath = url; //your url
NSData *_objectData = [NSData dataWithContentsOfURL:[NSURL URLWithString:resourcePath]];
NSError *error;
app.audioPlayer = [[AVAudioPlayer alloc] initWithData:_objectData error:&error];
app.audioPlayer.numberOfLoops = 0;
app.audioPlayer.volume = 1.0f;
[app.audioPlayer prepareToPlay];
if (app.audioPlayer == nil)
NSLog(@"%@", [error description]);
else
[app.audioPlayer play];
这篇关于从URI获取的音频流,并发挥它在iPhone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!