Вопрос

I can play a System Sound, however, I would like to play several System Sounds without overlapping them. I have the following code, using ARC, including AudioToolbox framework:

SSDViewController.h

#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>

bool            flagDelegate;
SystemSoundID   soundId;
CFURLRef        soundFileURLRef;

@interface SSDViewController : UIViewController
{
}

@property (readwrite)   CFURLRef        soundFileURLRef;

- (IBAction)play:(id)sender;
- (void)tocar:(NSString *)nameSound;

@end

SSDViewController.m

#import "SSDViewController.h"

@interface SSDViewController ()

@end

@implementation SSDViewController

@synthesize soundFileURLRef;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)play:(id)sender
{
    flagDelegate = NO;
    bool flagPlayingOnce = NO;
    while (flagDelegate == NO){
        if(flagPlayingOnce == NO){
            flagPlayingOnce = YES;
            [self tocar:@"HelloWorld"];
        }
    }

    [self tocar:@"HowAreYouWorld"];

} // end of button play

- (void)tocar:(NSString *)nameSound{

// Create the URL for the source audio file. The URLForResource:withExtension:    method is new in iOS 4.0
    NSURL *tapSound   = [[NSBundle mainBundle] URLForResource: nameSound
                                                withExtension: @"wav"];

    // Store the URL as a CFURLRef instance
    self.soundFileURLRef = (CFURLRef) CFBridgingRetain([tapSound self]);

    // Create a system sound object representing the sound file.
    soundId = 0;
    if ( soundId == 0 )
    {
        AudioServicesCreateSystemSoundID (
                                          soundFileURLRef,
                                          &soundId
                                          );
    }

    // Delegate to cacth the end of playing
    AudioServicesAddSystemSoundCompletion(soundId,
                                          NULL,
                                          NULL,
                                          myAudioCallback,
                                          NULL);

    AudioServicesPlaySystemSound (soundId);

    NSLog(@"end of tocar");

//    AudioServicesRemoveSystemSoundCompletion(soundId);
//    AudioServicesDisposeSystemSoundID(soundId);

} // end of tocar


void myAudioCallback(SystemSoundID mysound, void *clientData)
{
    NSLog(@"System sound finished playing!");
    flagDelegate = YES;
}

@end

I´m using flagDeletage to try to control the overlapping, but inside the loop, myAudioCallback is never called. Without flagDelegate and flagPlayingOnce, the two sounds overlap.

My objective is to play system sounds without overlap them.

thanks a lot in advance.

HQ.

Это было полезно?

Решение

The callback back doesn't stop the execution of the program, so the flags don´t work. the solution is to play the second wav from the callback function, soundId should be different.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top