我想從URL獲取音頻流並在iPhone上播放它。不幸的是,只有C#如何播放該流,但我需要在iPhone框架中實現Objective C。我嘗試過使用不同的音頻框架,但都不成功。這是什麼在C#中:從URI獲取音頻流並在iPhone上播放它
response = httpWebRequest.GetResponse();
using (Stream stream = response.GetResponseStream())
{
using (SoundPlayer player = new SoundPlayer(stream))
{
player.PlaySync();
}
}
我應該做什麼目標C中獲取從該網址流音頻?在該URL中,我沒有mp3文件或其他東西,我只是得到一個流。可能我必須以某種方式將該流下載到iPhone音頻文件,然後播放它以消除滯後。
我嘗試這樣做:
AVPlayer *player = [AVPlayer playerWithURL:url];
[player play];
這:
AVAudioPlayer *theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL: url error:nil];
[theAudio setVolume :0.50]; //setting the volume
[theAudio play];
但是都沒有成功。 我測試了我的網址,它在瀏覽器中運行良好。
這是工作的代碼:
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];
但它首先將所有數據下載到NSData對象。它不是流媒體。 –
它不會做音頻流,請分享一些鏈接到音頻流 – AsifHabib
它不能在線播放。它仍然下載。如果你的mp3是一個巨大的文件。 RIP這段代碼會給你一生的時間等待。 – vichhai