AVAssetReader终止播放(在AVAudioPlayer中) [英] AVAssetReader kills playback (in AVAudioPlayer)

查看:109
本文介绍了AVAssetReader终止播放(在AVAudioPlayer中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AVAssetReader读取ipod库资产音频数据并渲染波形图像.这是使用我在此问题的答案中描述的代码进行的.

有时这是在AVAudioPlayer实例播放音频时发生的.

无论我打到那一刻,播放的音频与正在读取的资产是否相同

[reader startReading];

正在播放的音频淡出". (就像AVAudioPlayer被告知停止播放一样).这很奇怪,因为我实际上并没有播放音频,只是阅读它.

我在SO上进行了搜索,找到了这种可能的解决方案但是我发现这似乎无法解决问题.

注意-我可以播放AVAudioPlayer的多个实例,并且启动它们似乎不会互相干扰-但是

[reader startReading];

甚至会杀死多个AVAudioPlayer并发实例,使它们全部同步消失.

有什么想法吗?

解决方案

回答我自己的问题....

对SO的进一步搜索使我实现了这种替代解决方案:

- (void)setupAudio {
    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: nil];
    UInt32 doSetProperty = 1;
    AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(doSetProperty), &doSetProperty);
    [[AVAudioSession sharedInstance] setActive: YES error: nil];
}

这是从此处收集的

**编辑**已更新****

此后,我将其设置为一个类,该类还预初始化了音频队列(在模拟器和设备中都有用,因为它消除了第一个音频文件的播放的启动延迟.

您可以在此处找到point1sec.mp3: 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];
    }
}

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

这篇关于AVAssetReader终止播放(在AVAudioPlayer中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆