iOS Objective-C/Swift read file while downloading - ios

I'm trying to play an audio file while it's downloading, is that possible in iOS?
I know I can get the location of the file "after" it's downloaded through:
(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
But I need to read/play it while it's still downloading.
Update
My original problem is:
I need to play a streamable audio file in either right or left headphones. There's already method setPan in AVAudioPlayer, but AVAudioPlayer does not work perfectly with streaming data. On the other hand, AVPlayer works perfect with streaming data, but it does not allow to "setPan".
My idea was to start downloading the audio file and pass it to AVAudioPlayer and call "setPan", but i can't access the file while it's being downloaded. And I'm still not sure if AVAudioPlayer will append the new downloaded data that is added to the file, while the file is playing.

Directly stream audio using
AVPlayer *anAudioStreamer = [[AVPlayer alloc] initWithURL:[NSURL URLWithString:#"http://my.url.com/my.mp3"]];
[anAudioStreamer play];

Related

How can I use AVAssetDownloadTask to download a FairPlay-encrypted AVURLAsset instance that has not yet been streamed?

I'm trying to use AVAssetDownloadTask to download and play FairPlay-encrypted audio content offline. I kept getting an error like this in urlSession:task:didCompleteWithError::
Error Domain=AVFoundationErrorDomain Code=-11863 "Operation Stopped" UserInfo={NSLocalizedFailureReason=This content is no longer available., NSLocalizedDescription=Operation Stopped}
My flow was:
Create an AVURLAsset using a URL like https://my.cdn.com/playlist.m3u8
Set its resource loader's delegate
Give it to a player in the form of an AVPlayerItem
Using the methods in AVAssetResourceLoaderDelegate, look for a URL beginning the scheme skd, download the CKC, get the persist-able form, and hand it back to the resource loader request
All of this worked, and was done in the same way as in Apple's HLSCatalog sample code. But downloading would still give me the above error, even though plugging the same playlist and key URLs into the sample code would download fine.
What I finally figured out was that AVAssetDownloadTask will only download an AVURLAsset instance that has already been streamed and given its decryption keys (via the AVAssetResourceLoaderDelegate) and that is not associated with a player. I can't just make a new AVURLAsset using the same URL as what is already playing and download it. So it seems that in order to download arbitrary FairPlay content, I have to:
Make an AVURLAsset
Make an AVPlayer and set its volume to 0
Give it the asset and play it
Wait until it requests its keys from the resource loader and starts playing
Give it to a download task and deassociate it with the player
But this seems horrible. It can't be true.
So, my question: How do I download a FairPlay-encrypted AVURLAsset without having streamed that specific instance of it before?
Turns out you set preloadsEligibleContentKeys to true on the asset's resource loader. Then you can download:
AVURLAsset *asset = [AVURLAsset assetWithURL:self.currDownload.url];
[asset.resourceLoader setDelegate:self queue:dispatch_get_main_queue()];
asset.resourceLoader.preloadsEligibleContentKeys = YES;
AVAssetDownloadTask *task = [self.downloadSession assetDownloadTaskWithURLAsset:asset assetTitle:self.currDownload.title assetArtworkData:nil options:#{AVAssetDownloadTaskMinimumRequiredMediaBitrateKey: #(265000)}];
task.taskDescription = self.currDownload.title;
[task resume];

AVPlayer does not play file recorded with AVAudioRecorder — Swift

AVPlayer does not play a .aif file recorded with AVAudioRecorder. My current process is:
AVAudioSession.sharedInstance's category is set to AVAudioSessionCategoryPlayAndRecord
AVAudioRecorder is instantiated with a NSURL in the app's documents directory and settings of format of kAudioFormatAppleIMA4, bit rate of 32000, 1 channel, and sample rate of 16000.0.
Audio is recorded, then stopped. The file is saved and I can find it in the app's documents directory and verify the audio is properly recorded.
An instance of AVPlayer is instantiated with the file's NSURL. An observer is added to this object to register changes to its status. When the AVPlayer is ReadyToPlay, I call the play function.
However, despite control flow reaching the point of AVPlayerStatus.ReadyToPlay and calling play, no sound is produced. I'm checking errors and verifying the existence and attributes of the file at the NSURL. I've also tried following the process of this SO post by instantiating an AVAsset, AVPlayerItem, and AVPlayer. No luck.
Any thoughts on why AVPlayer isn't playing audio for this newly recorded local file?
edit: The app is built for iOS 9 with Xcode 7 beta 5. The app is able to play audio files streamed with AVPlayer.
The issue was not caused by AVAudioRecorder or AVPlayer. My recording URL is a NSURL I'll call fileURL. I passed this NSURL to a different view controller but AVPlayer wouldn't play when instantiated with fileURL.
I got a hint to what was going wrong when
fileURL.checkResourceIsReachableAndReturnError(&error)
failed with the error "The file does not exist." Instead, I instantiated a new NSURL with the path of the newly recorded file using:
let url = NSURL(fileURLWithPath: path)
With this url, I was able to instantiate and play an instance of AVPlayer.

IOS - Streaming and Downloading an Audio File

I am currently making an app that is capable of streaming a music file. The problem is, our client wants it that while we are streaming an audio file, the streamed bytes will also be saved in the local storage. Which means that the streamed audio file will also be saved on the device's storage. For example I have this m4a file streamed, when the user stops streaming the audio file, the streamed music file will be saved in the device's local storage for future use.
Is this possible? If it is, what library should I use?
Thanks.
Yes it is possible use AVFoundation framework for this and play your audio.
First drag AVFoundation framework from built phase section then import like this #import <AVFoundation/AVFoundation.h>
Then declare AVAudioPlayer in .h file of your view controller instance like this
AVAudioPlayer *myAudioPlayer;
In view controller.m put this code
NSURL *fileURL = // your url.
myAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
myAudioPlayer.numberOfLoops = -1; //infinite loop
[myAudioPlayer play];
Via this way you are able to play audio in your iOS device.hope it will help your for playing audio

Streaming Audio Files in iOS

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.
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;
}
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.
Yes, AudioStreamer is really good till now what I have used and searched through.
But with AudioStreamer, there are certain changes needed when trying to stream in background, also when you are creating 2 instances, and lots more.....
https://github.com/mattgallagher/AudioStreamer/
You can find 2-3 questions regarding the same in my profile also...
I think what you are looking to do are uploading and downloading files from and to a server not streaming. For downloading, you can use NSURLConnection/ NSURLRequest. For uploading, you can use HTTP POST method. There is also an old third party library called ASIHTTPRequest. There are quite a bit of samples on these topics on the Internet.

IOS iPhone use MPMusicPlayerController to play external music file, and display iPod interface

I'm trying to use the standardized "iPod" audio player to play some MP3 tracks in an iPhone app I'm building. The tracks are downloaded from the internet and stored in the app's "Documents" directory. I thought of using a MPMusicPlayerController to do this, but I don't seem to be able to get it to work. Also, I've seen the AVAudioPlayer, but that just plays the audio without an interface. Any suggestions?
The MPMusicPlayerController is for playing items out of the iPod library (songs sync'd via iTunes) so you won't be able to use it for this.
You can get the NSData for your audio using...
NSData* data = [NSMutableData dataWithContentsOfFile:resourcePath options:0 error:&err];
Then use an AVAudioPlayer created from that data and call play.
AVAudioPlayer* player = [[AVAudioPlayer alloc] initWithData:data error:&err];
[player play];

Resources