试图在iPhone上播放声音

时间:2009-12-08 18:51:07

标签: 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]);
 }

它只是连续播放,声音不会一直播放,但每次用户移动手指时都会开始播放,我想要的是实际播放,然后再循环播放

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:1)

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

另外请重新格式化您的代码,这很难阅读。