2012年6月7日木曜日

Cordovaからの音再生その2

http://koyoy-lab.blogspot.jp/2012/06/cordova.html
からの続き。
iPodの音楽再生を継続させつつ、Cordovaアプリで音声を再生することに成功しました。
当初の読み通り、PGLowLatencyAudioプラグインにAVAudioSessionクラスを追加することで対応できました。
強引に組み込んだ感がありますので、動作保証はできませんが・・・。
Objective-Cの知識が無いので組み込みにあたっては
こちらのブログ記事、「RemoteIOでのオーディオ再生」がちょうどAudioSessionを使ってたので参考にさせてもらいました。

PGLowLatencyAudioAsset.hをこんな感じに変更。
#import <Foundation/Foundation.h>
#import <AVFoundation/AVAudioPlayer.h>
#import <AudioToolbox/AudioToolbox.h>
@interface PGLowLatencyAudioAsset : NSObject {
    NSMutableArray* voices;
    int playIndex;
    AudioUnit outputUnit;//aaaa
}
-(id) initWithPath:(NSString*) path withVoices:(NSNumber*) numVoices;
- (void) setupAudioSession;
- (void) play;
- (void) stop;
- (void) loop;
- (void) unload;
@end
あと、PGLowLatencyAudioAsset.mは、
まずinitWithPathの「self = [super init];」の次の行に
    [self setupAudioSession];
を追加。
更に、以下の内容を追加。
void InterruptionListener(void *inUserData,
                          UInt32 inInterruption)
{
    AudioUnit *remoteIO = (AudioUnit*)inUserData;
    if (inInterruption == kAudioSessionEndInterruption) {
        AudioSessionSetActive(true);
        AudioOutputUnitStart(*remoteIO);
        printf("EndInterruption\n");
    }
    if (inInterruption == kAudioSessionBeginInterruption) {
        AudioOutputUnitStop(*remoteIO);
        printf("BeginInterruption\n");
    }
}
- (void) setupAudioSession
{
    AudioSessionInitialize(NULL, NULL, InterruptionListener, &outputUnit);
    UInt32 category = kAudioSessionCategory_AmbientSound;
    AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(category), &category);
    AudioSessionSetActive(true);
}
といった感じでとりあえずはiPodの再生を止めずに音声の再生ができるようになりました。

0 件のコメント:

コメントを投稿