AVAssetReader kills playback (in AVAudioPlayer)

前端 未结 1 772
小鲜肉
小鲜肉 2020-12-17 00:52

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

<
相关标签:
1条回答
  • 2020-12-17 01:56

    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];
        }
    }
    
    0 讨论(0)
提交回复
热议问题