ios 从 URI 获取音频流并在 iPhone 上播放
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6856445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get an audio stream from URI and play it on iPhone
提问by Denis Kutlubaev
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#:
我想从 URL 获取音频流并在 iPhone 上播放。不幸的是,只有 C# 示例说明如何播放该流,但我需要在 iPhone 框架中的 Objective C 上实现。我尝试过自己使用不同的音频框架,但都没有成功。这是 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.
我应该在目标 C 中做什么才能从该 url 获取流音频?在那个 URL 中,我没有 mp3 文件或其他东西,我只是得到一个流。可能我必须以某种方式将该流下载到 iPhone 音频文件,然后播放它以消除延迟。
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];
回答by Rakesh Bhatt
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];
回答by Rohan Bhale
Hey I faced this problem of audio streaming for a radio application. I had searched for the answers and they did not work. AVAudioPlayer is not the class to be used here. Downloading the contents of URL using NSData object is not the solution either as it downloads the contents then the code further executes. It is similar to downloading a mp3 and then playing it locally on a player.
嘿,我遇到了无线电应用程序的音频流问题。我已经搜索了答案,但它们不起作用。AVAudioPlayer 不是这里要使用的类。使用 NSData 对象下载 URL 的内容也不是解决方案,因为它下载内容然后代码进一步执行。这类似于下载 mp3,然后在本地播放器上播放。
[NSData dataWithContentsOfURL:[NSURL URLWithString:resourcePath]]; //Downloads the contents of the url and in case of radio the url's content is unlimited. The program gets frozen on executing this.
Instead use AVPlayer class.
而是使用 AVPlayer 类。
This code worked in my radio application
此代码适用于我的无线电应用程序
AVPlayer *objAVPlayer = [[AVPlayer alloc] initWithURL:url];
[objAVPlayer play];
This code does not download the contents at the url but it plays the stream as it is received.
此代码不会下载 url 中的内容,但会在收到流时播放。
回答by Alex Coplan
回答by Arash Zeinoddini
MPMoviePlayerController *player;
player = [[MPMoviePlayerController alloc] init] ;
player.view.frame = CGRectMake(0, 0, 0, 0);
player.movieSourceType = MPMovieSourceTypeStreaming;
player.view.hidden = YES;
[self.view addSubview: [wikiMusicPlayer view]];
NSString *address = Your URL;
address = [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:address];
[wikiMusicPlayer setContentURL:url];
[wikiMusicPlayer prepareToPlay];
[wikiMusicPlayer setShouldAutoplay: NO];
[wikiMusicPlayer play];