Get an audio stream from URI and play it on iPhone

前端 未结 4 982
轻奢々
轻奢々 2020-12-08 05:24

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

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-08 06:16

    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.

提交回复
热议问题