Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVAssetReader kills playback (in AVAudioPlayer)

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?

like image 377
unsynchronized Avatar asked Sep 14 '11 05:09

unsynchronized


Video Answer


1 Answers

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];
    }
}
like image 100
unsynchronized Avatar answered Sep 28 '22 01:09

unsynchronized