Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get an audio stream from URI and play it on iPhone

Tags:

ios

iphone

audio

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];
like image 453
Denis Kutlubaev Avatar asked Jul 28 '11 09:07

Denis Kutlubaev


People also ask

Can I stream audio from PC to iPhone?

Connect both iOS device and computer to the same Wi-Fi network. Then run Airfoil Satellite and Airfoil separately. In Airfoil for desktop, choose audio source, then click the sound icon besides your iPhone, iPad or iPod touch to start stream audio from Windows or Mac computer to your iOS device.


2 Answers

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];
like image 197
Rakesh Bhatt Avatar answered Nov 15 '22 18:11

Rakesh Bhatt


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.

[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.

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.

like image 23
Rohan Bhale Avatar answered Nov 15 '22 18:11

Rohan Bhale