AVAssetReader kills playback (in AVAudioPlayer)

柔情痞子 提交于 2019-12-29 06:24:25

问题


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?


回答1:


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];
    }
}


来源:https://stackoverflow.com/questions/7411793/avassetreader-kills-playback-in-avaudioplayer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!