I am trying to play partially downloaded file (I want the audio to start when it has enough downloaded data).
I have tried the following:
self.mutableData = nil;
self.mutableData = [NSMutableData data];
self.mutableData.length = 0;
GTMHTTPFetcher *fetcher =
[[self driveService].fetcherService fetcherWithURLString:song.filePath];
[fetcher setReceivedDataBlock:^(NSData *dataReceivedSoFar) {
[self.mutableData appendData:dataReceivedSoFar];
if (!self.audioPlayer.playing && self.mutableData.length > 1000000)
{
self.audioPlayer = nil;
self.audioPlayer = [[AVAudioPlayer alloc] initWithData:self.mutableData error:nil];
[self.audioPlayer play];
}
}];
[fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error) {
if (error == nil)
{
NSLog(#"NSURLResponse: %#", fetcher.response.);
}
else
{
NSLog(#"An error occurred: %#", error);
}
}];
But it starts playing the first 1-3 seconds and then the app either crashes or it plays that 1-3 seconds over and over again until the download finishes and then it plays the whole song.
Can I somehow achieve this with AVPlayer, or fix this problem?
AVAudioPlayer cannot play files that are open for writing / downloading, but AVPlayer can. Switch to AVPlayer.
To stream audio, you should use AVPlayer or Audio Queue Services.
initWithData isn't intended for this purpose. You can use initWithData after you have downloaded the entire file.
Related
Should AVAudioPlayer be used to play sound in background or main thread? Is it a better practice to use background thread to play sound to not block the UI?
play your sound on a separate thread and I suspect your blocking issues will go away.
The easiest way to make this happen is to do:
- (void)playingAudioOnSeparateThread: (NSString *) path
{
if(_audioPlayer)
{
_audioPlayer = nil; // doing this while a sound is playing might crash...
}
NSLog(#"start playing audio at path %#", path);
NSError *error = nil;
NSData *audioData = [NSData dataWithContentsOfFile:path];
_audioPlayer = [[AVAudioPlayer alloc] initWithData:audioData error:&error];
if (error == nil)
{
[_audioPlayer play];
}
}
- (void)playAudio:(NSString *)path
{
[NSThread detachNewThreadSelector: #selector(playingAudioOnSeparateThread:) toTarget: self withObject: path];
}
First, to avoid posting a great deal of code on here, I have created a very basic example of what I am after - https://github.com/opheliadesign/AudioTest.
I am brand new to iOS development and I'm having a little trouble understanding the best way to load and play a static MP3 file on a remote server. The audio is not streaming live, is less than a megabyte - about 30 seconds long in average (they are radio dispatches).
It has been suggested that I use NSURLSession to load the MP3 file and then play it within the completion block. This seems to be working but I have a feeling that I could be handling it better, just do not know how. Here is the block where I grab the audio and begin playing:
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:YES];
NSLog(#"View loaded");
// Register to receive notification from AppDelegate when entering background
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(stopPlayingAudio) name:#"stopPlayingAudio" object:nil];
// Assign timer to update progress
self.updateTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(updateSeekBar) userInfo:nil repeats:YES];
// Demo recording URL
NSString *recordingUrl = #"https://s3.amazonaws.com/tevfd-recording/All_Fire_and_EMS_2015-02-0214_48_33_630819.mp3";
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithURL:[NSURL URLWithString:recordingUrl] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (!error) {
NSLog(#"No error..");
self.player = [[AVAudioPlayer alloc]initWithData:data error:nil];
// Setup slider for track position
self.slider.minimumValue = 0;
self.slider.maximumValue = self.player.duration;
[self.player prepareToPlay];
[self.player play];
}
}]resume];
}
For example, have a slider that updates the player's position when it is moved. I initialize the AVAudioPlayer in the completion block but create a property for it in the header. If the player has not been initialized, could moving the slider cause a crash? If so, how should I handle this better?
Also, I have a timer that updates the slider position as the AVAudioPlayer plays. When the track reaches its end, the timer continues - clearly this is a potential memory issue. Not sure of the best way to handle this and I would also like for the user to be able to start playing the recording again after it is completed, for example if they moved the slider to the right.
I have searched and searched, cannot seem to find anything related to specifically what I am doing. Any help would be greatly appreciated.
UPDATE
This is what I first began with but I experienced some lag between clicking on a UITableView cell and presenting the view that loaded/played the audio. Several suggested NSURLSession.
- (void)viewDidLoad {
[super viewDidLoad];
// Get the URL
NSURL *callAudioURL = [NSURL URLWithString:[self.call objectForKey:#"url"]];
NSData *callAudioData = [NSData dataWithContentsOfURL:callAudioURL];
AVAudioPlayer *audio = [[AVAudioPlayer alloc] initWithData: callAudioData error:nil];
self.player = audio;
self.slider.minimumValue = 0;
self.slider.maximumValue = self.player.duration;
[[self player] play];
self.updateTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(updateSeekBar) userInfo:nil repeats:YES];
}
If you're concerned about a small delay, then downloading and caching it is the best solution. You need to download the audio file and then play it locally. Typically playing -- or streaming -- from the network is useful only when the audio is so unique that you cannot store it locally.
To implement something like this (code untested):
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSFileHandle *hFile = [NSFileHandle fileHandleForWritingAtPath:self.localPath];
//did we succeed in opening the existing file?
if (!hFile) { //nope->create that file!
[[NSFileManager defaultManager] createFileAtPath:self.localPath contents:nil attributes:nil];
//try to open it again...
hFile = [NSFileHandle fileHandleForWritingAtPath:self.localPath];
}
//did we finally get an accessable file?
if (!hFile) {
NSLog("could not write to file %#", self.localPath);
return;
}
#try {
//seek to the end of the file
[hFile seekToEndOfFile];
//finally write our data to it
[hFile writeData:data];
} #catch (NSException * e) {
NSLog("exception when writing to file %#", self.localPath);
result = NO;
}
[hFile closeFile];
}
When all of the data has been received, start your audio:
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:self.localPath error:nil];
I'm using this code to load and begin playing a sound:
- (void) buzz {
if (buzzSound == nil) {
[[AVAudioSession sharedInstance] setActive: YES error: nil];
NSError *error;
NSBundle *mainBundle = [NSBundle mainBundle];
NSURL *soundFile = [[NSURL alloc] initFileURLWithPath: [mainBundle pathForResource: #"buzzer_8"
ofType: #"wav"]];
buzzSound = [[AVAudioPlayer alloc] initWithContentsOfURL: soundFile error: &error];
buzzSound.delegate = self;
if (DEBUG_BLE && error != nil)
printf("Error loading sound: %s\n", [[error localizedDescription] cStringUsingEncoding: NSUTF8StringEncoding]);
}
// Play the sound.
buzzSound.numberOfLoops = -1;
[buzzSound prepareToPlay];
[buzzSound setVolume: 1.0];
if (![buzzSound play]) {
self.buzzSound = nil;
[self buzzSound];
}
}
This code is triggered by a touch down event on a button. When the button gets a touch up event (on or off of the button), it stops the sound with:
[buzzSound stop];
The buzz sound itself is a short .wav file.
The first time buzz is called, the sound plays once. It does not repeat. On subsequent calls it does repeat. Why?
Intermittently, if I play the sound for a short period of time that is about the same length of time as one cycle through the sound file, the sound fails on subsequent calls to buzz, and play returns NO. Thereafter, play will always return NO until the sound is reloaded, after which is plays the sound once as noted above and then starts working correctly until the sound is played for a short time, again.
I do have audioPlayerDecodeErrorDidOccur:error: set up; no error is reported.
I'm currently trying to implement a system like the alarm one to alert the phone when an event occurred (in my case a bluetooth event). I want this alert to occur even if the phone is in silent and in background.
I create a local notification but i can't get sound played if the phone is in silent mode (which seems to be normal since we put the phone in silent).
So i tried to manage the sound by myself and i'm struggling with playing sound in background. So far i implement the "App plays audio or streams audio/video using AirPlay" key in my plist and i'm using this code.
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *error = nil;
BOOL result = [audioSession setActive:YES error:&error];
if ( ! result && error) {
NSLog(#"Error For AudioSession Activation: %#", error);
}
error = nil;
result = [audioSession setCategory:AVAudioSessionCategoryPlayback error:&error];
if ( ! result && error) {
NSLog(#"Error For AudioSession Category: %#", error);
}
if (player == nil) {
NSString *path = [[NSBundle mainBundle] pathForResource:#"bell" ofType:#"wav"];
NSURL *url = [NSURL fileURLWithPath:path];
NSError *err = nil;
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&err];
if(err)
{
NSLog(#"Error Player == %#", err);
}
else {
NSLog(#"Play ready");
}
}
[player prepareToPlay];
[player setVolume:1.0];
if([player play])
{
NSLog(#"YAY sound");
}
else {
NSLog(#"Error sound");
}
The sound works great in foreground, even in silent mode, but i got no sound at all in background. Any ideas ?
Thanks
EDIT:
Finally i got it working with the above code. The only missing point is that i was trying to play the sound in somewhat appeared to be a different thread, when i play it right in my bluetooth event and not my function call it's working.
An app that plays audio continuously (even while the app is running in the background) can register as a background audio app by including the UIBackgroundModes key (with the value audio) in its Info.plist file. Apps that include this key must play audible content to the user while in the background.
Apple reference "Playing Background Audio"
Ensuring That Audio Continues When the Screen Locks
Found here
I'm not sure if your problem is that you have not configure correctly the audio session output. I had similar problem while playing .wav in my app. I only listened them with earphones. This method helped me:
- (void) configureAVAudioSession {
//get your app's audioSession singleton object
AVAudioSession *session = [AVAudioSession sharedInstance];
//error handling
BOOL success;
NSError* error;
//set the audioSession category.
//Needs to be Record or PlayAndRecord to use audioRouteOverride:
success = [session setCategory:AVAudioSessionCategoryPlayAndRecord
error:&error];
if (!success) NSLog(#"AVAudioSession error setting category:%#",error);
//set the audioSession override
success = [session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker
error:&error];
if (!success) NSLog(#"AVAudioSession error overrideOutputAudioPort:%#",error);
//activate the audio session
success = [session setActive:YES error:&error];
if (!success) NSLog(#"AVAudioSession error activating: %#",error);
else NSLog(#"audioSession active"); }
Try it out!
You need to make couple of changes in plist file.
for enabling sound when the app enter's Background.
1) Set Required background mode to App plays audio
2) set Application does not run in background to YES.
NSError *setCategoryErr = nil;
NSError *activationErr = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];
[[AVAudioSession sharedInstance] setActive:YES error:&activationErr];
Then, you need to write these much code in AppDelegate
Now, you can easily run audio while phone screen locks or in background.
It works fine for me. :)
for more please refer to Playing Audio in background mode and Audio Session Programming Guide
I use [MPMusicPlayerController iPodMusicPlayer] setting the ipod volumn before playing a local "beep.caf" file which use AVAudioPlayer.
Strange things happened, if I kill the system ipod app before running the following code, the beep.caf played just 1~2 second which should be a 30 second audio.
The code like this:
self.ipodPlayer = [MPMusicPlayerController ipodMusicPlayer];
self.ipodPlayer.volumn = .5;
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: fileName];
NSError *err = nil;
self.trackPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&err];
if(err)
{
//LOG ERR
}
else
{
if ([trackPlayer prepareToPlay]) {
if (![trackPlayer play]) {
//ALog(#"Error:play sound failed.");
}
}
else
{
//ALog(#"Error:prepare play sound failed.");
};
}
This code works well under iOS4.3. I doubt if there is something special done when the iPodMusicPlayer init under iOS 6.0.1.
I need to set ipodPlayer's volumn before play local audio, how can I do this correctly?