我可以播放系统声音,但是,我想播放几个系统声音而不重叠它们。我有以下代码,使用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
永远不会被调用。没有flagDelegate
和flagPlayingOnce
,两个声音重叠。
我的目标是播放系统声音而不重叠它们。
非常感谢。
HQ。
答案 0 :(得分:0)
回调不会停止执行程序,因此标志不起作用。 解决方案是从回调函数中播放第二个wav,soundId应该是不同的。