如何使用AudioServicesRemoveSystemSoundCompletion回调

时间:2013-12-06 08:28:32

标签: ios objective-c audio playback

我正在尝试在队列中播放两个不同的文件。一个是介绍轨道,另一个是主要轨道。

设置音乐播放的方法

-(void)musicPlayBack:(CFStringRef )soundName{

    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;
    soundFileURLRef = CFBundleCopyResourceURL(mainBundle
                                              , (CFStringRef) soundName, CFSTR ("mp3"), NULL);
    SystemSoundID soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesAddSystemSoundCompletion(soundID, NULL, NULL, FinishedSoundPlayback, NULL);
    AudioServicesPlaySystemSound(soundID);

}

-(void)playIntro:(NSString *)introTrackName{
    NSArray *introTracks = [NSArray arrayWithObjects:@"heybaby",@"dumbson",@"heybadass",
                            @"heydarling",@"heyhoney",@"madam",@"sir", nil];
    [self musicPlayBack:(__bridge CFStringRef)([introTracks objectAtIndex:2])];
}

-(void)playNotification:(NSString *)notificationTrackName{

    [self musicPlayBack:(__bridge CFStringRef)(notificationTrackName)];
}

-(void)notifyWithSound:(int)batteryCase{
    NSArray *mainSoundTracks = [NSArray arrayWithObjects:@"ten",@"twenty",@"twentyfive",@"thirty",
                                @"fourty",@"fifty",@"sixty",@"seventy",@"seventyfive",
                                @"eigthy",@"ninty",@"hundred",nil];
    [self playIntro:@"someRandomIntro"];
    while (introFinished==NO) {
        if (introFinished==YES) {
            break;
        }
    }
    [self playNotification:[mainSoundTracks objectAtIndex:batteryCase]];


}

这是我在@implementation之前所拥有的

BOOL introFinished = NO;

static void FinishedSoundPlayback(SystemSoundID  mySSID, void* data){
    introFinished = YES;
    AudioServicesRemoveSystemSoundCompletion (mySSID);
    NSLog(@"completion Callback");
}

问题是FinishedSoundPlayback永远不会被调用。

1 个答案:

答案 0 :(得分:0)

您在#34; soundId"的名称下注册了系统声音并且你希望回调" mySSID"。这假设是同一个名字。顺便说一句,由于其局限性,我不建议使用此API播放音乐。