Streaming Audio Files in iOS

11,952

Solution 1

In general, the AVAudioPlayer uses music playback. However, this framework does not support streaming. So using AVPlayer streaming can be achieved. Usually the developers AVPlayer purposes only know that can play video, but can also play music.

I look at the following Apple's sample code is recommended.this is using a AVPlayer

StitchedStreamPlayer

I upload to myServer Tested, .mp3 also perfectly

3G, wifi tested in both environments. although sample code, It was surprisingly works perfectly. mp3 file upload your server. Try to test right now. Will work well.

And If you want to play in the background, the following code don't forget:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    AVAudioSession* audio = [[AVAudioSession alloc] init];
    [audio setCategory: AVAudioSessionCategoryPlayback error: nil];
    [audio setActive: YES error: nil];

    return YES;
}

Solution 2

For playback form the server my Audjustable project (https://github.com/tumtumtum/audjustable) fixes most of the problems in the original AudioStreamer project and includes gapless playback and decoupled audio data source to allow for things like error-recovery, encryption etc. Completely open source...

If you want to upload audio data to the server you can do it many ways but NSURLConnection would seem to be the easiest way.

Share:
11,952
Jason McCreary
Author by

Jason McCreary

I'm JMac - a software engineer in Louisville, KY working primarily on PHP and iOS applications. Creator Laravel Shift - the automated way to upgrade Laravel projects. Produced Getting Git - a comprehensive video series to help you learn Git. I believe in providing answers that teach. StackOverflow Tips: Tell us what you have tried? If asking about an error, include the error message and offending line. Learn the formatting options.

Updated on June 04, 2022

Comments

  • Jason McCreary
    Jason McCreary almost 2 years

    I need to send and receive audio files within my app. To be more performant and keep an open connection, I am looking to stream this data.

    I have looked at things like HTTP Live Streaming and AudioStreamer based on answers to questions. However, these seem to be for continuous streaming, 1-way (read). Whereas I am sending a finite audio file (> 10 seconds) and then receiving one back.

    I am familiar with NSURLConnection and have reviewed this answer. But again, this uses a continuous, 1-way stream.

    I would appreciate any recommend on the architecture to accomplish the above to help me get started.

  • Jason McCreary
    Jason McCreary almost 12 years
    I'll take a look at the sample code. But I see that this will handled playback, but you also said upload. What did you use for the upload?
  • bitmapdata.com
    bitmapdata.com almost 12 years
    i say, 'upload' means is .mp3 file upload to myServer. about 'blahblah.com/my_file.mp3'. The sample code is a skeleton. is the basic-code using a AVPlayer implement streaming audio or video play. For further information, please see the following: developer.apple.com/library/mac/#documentation/AudioVideo/…
  • Jason McCreary
    Jason McCreary almost 12 years
    But how. You did not use AVPlayer for the upload… Did you use NSURLConnection?
  • bitmapdata.com
    bitmapdata.com almost 12 years
    No, directly upload using a FTP.
  • Jason McCreary
    Jason McCreary almost 12 years
    Okay. I fear this may not meet my requirements. But I will look into these. Maybe they do…
  • Jason McCreary
    Jason McCreary almost 12 years
    This would require polling the server for when the resource is ready. I want to send data to the server as it is available, and keep the connection until it returns all data (in this case data is an audio file). So I am indeed looking for sockets/streaming.