2009-12-08 89 views
2

好这部作品尝试在iPhone上播放声音

- (id)initWithCoder:(NSCoder*)coder 
{ 
if ((self = [super initWithCoder:coder])) 
    { 
    // Load the sounds 
    NSURL *soundURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Begin Drawing" ofType:@"wav"]]; 
    AudioServicesCreateSystemSoundID((CFURLRef)soundURL, &_boomSoundIDs[0]); 
    soundURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Middle Drawing" ofType:@"wav"]]; 
    AudioServicesCreateSystemSoundID((CFURLRef)soundURL, &_boomSoundIDs[1]); 
    soundURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"End Drawing" ofType:@"wav"]]; 
    AudioServicesCreateSystemSoundID((CFURLRef)soundURL, &_boomSoundIDs[2]); 
    } 
return self; 
} 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
UITouch *touch = [touches anyObject]; 

if ([touches count] == 1) 
    { 
    AudioServicesPlaySystemSound(_boomSoundIDs[0]); 
} 

但是,这就是我想解决

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
if ([touches count] == 1) 
    { 
    AudioServicesPlaySystemSound(_boomSoundIDs[1]); 
} 

它只是起到这样持续的声音不会玩一路过关斩将,但每次用户移动他们的手指开始时,我想要的是实际上一直播放,然后再次循环播放

回答

3

系统声音API可能不是您尝试执行的最佳选择。看看AVAudioPlayer。该API很简单,它可以让你检查声音是否已经播放。

1

在播放声音之前,请检查它是否已播放。

也请重新格式化您的代码,它很难阅读。

+0

好的我修复了格式化,对此抱歉。但我不知道如何检查声音是否在播放。 – Jaba 2009-12-08 18:59:33

+0

如果您查看(AudioServicesPlaySystemSound参考)[http://developer.apple.com/iphone/library/documentation/AudioToolbox/Reference/SystemSoundServicesReference/Reference/reference.html#//apple_ref/c/func/AudioServicesPlaySystemSound]你会看到你可以注册一个回调(通过AudioServicesAddSystemSoundCompletion)。这有帮助吗? – EightyEight 2009-12-09 05:38:26

+0

那么我已经看过,但我只是不明白添加和删除回调especialy这一过程。如果你可以请给我一个关于这个过程的探索和/或他们在这个过程中做什么,那会很棒。我求你。 – Jaba 2009-12-09 14:23:25