I am using AVAssetReader to read ipod library asset audio data and render a waveform image. this takes place using code I have described in my answer to this question
this sometimes takes place while audio is being played by an instance of AVAudioPlayer.
regardless of wether the audio being played is the same asset that is being read, the moment i hit
[reader startReading];
the audio being played "fades out". (as if the AVAudioPlayer has somehow been told to stop playback). This is odd, as I am not actually playing the audio, just reading it.
I did a search on SO and found this possible solution however i have found that this does not appear to solve the problem.
note - I am able to have several instances of AVAudioPlayer playing, and starting these do not seem to interfere with each other - however
[reader startReading];
will even kill multiple simultaneous instances of AVAudioPlayer, causing them all to synchronously fade out.
any ideas?
answering my own question....
further searching on SO led me to implementing this alternate solution:
- (void)setupAudio {
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];
UInt32 doSetProperty = 1;
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(doSetProperty), &doSetProperty);
[[AVAudioSession sharedInstance] setActive: YES error: nil];
}
this was gleaned from here
**EDIT **UPDATED****
I have since made this into a class that also pre-initialises the audio queue (useful in both simulator and device as it eliminates the startup lag from the playback of the first audio file.
you can find the point1sec.mp3 here: http://www.xamuel.com/blank-mp3s/
#import <AVFoundation/AVFoundation.h>
#import "AudioToolbox/AudioServices.h"
@interface sw_AVAudioPlayerSetup : NSObject
<AVAudioPlayerDelegate> {
}
+ (void)setupAudio ;
+ (void)setupSharedSession ;
@end
@implementation sw_AVAudioPlayerSetup
+ (void)setupSharedSession {
static BOOL audioSessionSetup = NO;
if (audioSessionSetup) {
return;
}
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];
UInt32 doSetProperty = 1;
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(doSetProperty), &doSetProperty);
[[AVAudioSession sharedInstance] setActive: YES error: nil];
audioSessionSetup = YES;
}
+ (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
// delegate callback to release player
[player release];
}
+ (void)setupAudio {
[self setupSharedSession];
NSString *filepath = [[NSBundle mainBundle]
pathForResource:@"point1sec"
ofType:@"mp3"];
if ([[NSFileManager defaultManager] fileExistsAtPath:filepath]) {
AVAudioPlayer* player = [[AVAudioPlayer alloc]
initWithContentsOfURL:
[NSURL fileURLWithPath:filepath]
error:nil];
player.delegate = (id <AVAudioPlayerDelegate>) self;
[player play];
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With