sub*_*ero 2 iphone objective-c iphone-sdk-3.0
我是iphone和Objective c的新手,到目前为止,我已经能够编写一些小例子了.
我想播放声音,并在示例完成播放后继续使用其余代码,即:
printf("hello");
playASound["file.wav"];
printf("world");
Run Code Online (Sandbox Code Playgroud)
实际上我得到了:打印你好,同时播放文件和打印世界,但我想要的是:打印你好,播放文件,打印世界......所以,问题是我如何得到它?
谢谢
顺便说一句.这是playASound代码:
-(void) playASound: (NSString *) file {
//Get the filename of the sound file:
NSString *path = [NSString stringWithFormat:@"%@/%@",
[[NSBundle mainBundle] resourcePath],
file];
SystemSoundID soundID;
//Get a URL for the sound file
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);
//play the file
AudioServicesPlaySystemSound(soundID);
}
Run Code Online (Sandbox Code Playgroud)
Nic*_*ott 10
从文档:
讨论此功能播放短音(持续时间不超过30秒).因为声音可能会播放几秒钟,所以此功能是异步执行的.要知道声音何时播放完毕,请调用AudioServicesAddSystemSoundCompletion函数以注册回调函数.
因此,您需要将您的功能分解为两部分:一个调用PlayASound并打印"Hello"的函数,以及一个在声音播放完毕后由系统调用并打印"World" 的函数.
// Change PlayASound to return the SystemSoundID it created
-(SystemSoundID) playASound: (NSString *) file {
//Get the filename of the sound file:
NSString *path = [NSString stringWithFormat:@"%@/%@",
[[NSBundle mainBundle] resourcePath],
file];
SystemSoundID soundID;
//Get a URL for the sound file
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);
//play the file
AudioServicesPlaySystemSound(soundID);
return soundID;
}
-(void)startSound
{
printf("Hello");
SystemSoundID id = [self playASound:@"file.wav"];
AudioServicesAddSystemSoundCompletion (
id,
NULL,
NULL,
endSound,
NULL
);
}
void endSound (
SystemSoundID ssID,
void *clientData
)
{
printf("world\n");
}
Run Code Online (Sandbox Code Playgroud)
另请参阅AudioServicesAddSystemSoundCompletion和AudioServicesSystemSoundCompletionProc的文档.