1

我可以播放系统声音,但是,我想播放多个系统声音而不重叠。我有以下代码,使用 ARC,包括AudioToolbox框架:

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

flagDeletage用来尝试控制重叠,但在循环内部, myAudioCallback永远不会被调用。没有flagDelegateand flagPlayingOnce,这两个声音重叠。

我的目标是播放系统声音而不重叠。

提前非常感谢。

总部。

4

1 回答 1

0

回调不会停止程序的执行,因此标志不起作用。解决方案是从回调函数播放第二个wav,soundId应该不同。

于 2013-08-22T21:36:53.637 回答