2013-12-17 2 views
0

나는 진동 할 때 진동이 멈출 때까지 소리를 계속 반복 재생합니다. 저는 이것을 달성하기 위해 여러 개의 AVAudioPlayers를 사용하고 있습니다. 그러나 대부분은 작동하지만 때때로 Decepticons가 Transformers에서 만들 것 같은 사운드처럼 왜곡 된 사운드가 나옵니다. 왜 이런 일이 일어날 지에 대한 아이디어가 있습니까? 내가 사용하고 파일 형식은 MP3이며, 다음과 같이 코드는 다음과 같습니다iOS에서 여러 사운드를 재생하면 변압기 소리가납니다.

재산권 선언 :

@property (strong, nonatomic) AVAudioPlayer *musicPlayerForRoar; 
@property (strong, nonatomic) AVAudioPlayer *musicPlayerForRoar2; 
@property (strong, nonatomic) AVAudioPlayer *musicPlayerForRoar3; 
@property (strong, nonatomic) AVAudioPlayer *musicPlayerForRoar4; 
@property (strong, nonatomic) AVAudioPlayer *musicPlayerForRoar5; 

PlayRoar 방법. 등 PlayRoar, PlayRoar1, PlayRoar2이있는 오디오 플레이어 내가 초기화를 제외하고 모든면에서 동일합니다

- (void)playRoar { 
NSError *error; 
self.musicPlayerForRoar = [[AVAudioPlayer alloc]initWithContentsOfURL:[self urlForRoarFile] error:&error]; 
self.musicPlayerForRoar.delegate = self; 
[self.musicPlayerForRoar play]; 

} 

/*Idea for the following two methods came from code on http://www.iosing.com/2011/12/making-a-jingle-bells-app-part-2-coding/*/ 
static BOOL L0AccelerationIsShaking(UIAcceleration* last, UIAcceleration* current, double threshold) { 
    double deltaX = fabs(last.x - current.x), deltaY = fabs(last.y - current.y), deltaZ = fabs(last.z - current.z); 

    return (deltaX > threshold && deltaY > threshold) || (deltaX > threshold && deltaZ > threshold) || (deltaY > threshold && deltaZ > threshold); 
} 

-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration{ 
if (self.lastAcceleration) { 
    if (!histeresisExcited && L0AccelerationIsShaking(self.lastAcceleration, acceleration, 0.7)) { 
     histeresisExcited = YES; 
     //Use to prevent all sound files from playing at once initially. Each time the device is shaken, this is incremented. Depending on its value, the audio players are fired 
     shakesCounter++; 

     BOOL nonePlaying = (self.musicPlayerForRoar.isPlaying && self.musicPlayerForRoar2.isPlaying && self.musicPlayerForRoar3.isPlaying && self.musicPlayerForRoar4.isPlaying && self.musicPlayerForRoar5.isPlaying); 
     if (!nonePlaying) { 
      [self playRoar]; 
      NSLog(@"first playing"); 
     } 

     if (!self.musicPlayerForRoar2.isPlaying && shakesCounter > 1) { 
      [self playRoar2]; 
      NSLog(@"second playing"); 
     } 
// 
//   if (!self.musicPlayerForRoar3.isPlaying && shakesCounter > 2) { 
//    [self playRoar3]; 
//    NSLog(@"third playing"); 
//   } 
//    
//    
//   if (!self.musicPlayerForRoar4.isPlaying && shakesCounter > 3) { 
//    [self playRoar4]; 
//    NSLog(@"fourth playing"); 
//   } 
//    
//   if (!self.musicPlayerForRoar5.isPlaying && shakesCounter > 4) { 
//    [self playRoar5]; 
//    NSLog(@"fifth playing"); 
//   } 

     if (shakesCounter > 5) { 
      shakesCounter = 0; 
     } 

    } 
    else if (histeresisExcited && !L0AccelerationIsShaking(self.lastAcceleration, acceleration, 0.2)) { 
     histeresisExcited = NO; 
    } 

} 
    self.lastAcceleration = acceleration; 
} 

답변

1

내가가가는 소리를 달성하기 위해, 나는 C 수준의 오디오 서비스를 사용하여 끝났다. 다음 코드는 내 문제를 해결하고 장치가 흔들릴 때 큰 소리를냅니다. 비동기 메서드 (AudioServicesPlaySystemSound())를 사용하므로 왜곡이 사라지고 사운드가 예상대로 재생됩니다.

- (void)playRoar { 
    CFBundleRef mainBundle = CFBundleGetMainBundle(); /* Define mainBundle as the current app's bundle */ 
    CFURLRef fileURL = CFBundleCopyResourceURL(mainBundle, (CFStringRef)@"BearcatGrowl", CFSTR("mp3"), NULL); /* Set Bundle as Main Bundle, Define Sound Filename, Define Sound Filetype */ 
    UInt32 soundID; /* define soundID as a 32Bit Unsigned Integer */ 
    AudioServicesCreateSystemSoundID (fileURL, &soundID); /* Assign Sound to SoundID */ 
    AudioServicesPlaySystemSound(soundID); /* Now play the sound associated with this sound ID */ 
} 

는 또한 http://www.iosing.com/2011/12/making-a-jingle-bells-app-part-3-coding-the-sound/에서이 솔루션을 발견, 그래서 그들은 제공되는 코드를 그들에게 대단히 감사합니다. 기기가 흔들릴 때 다른 사람이 소리를내는 데 도움이되기를 바랍니다.

+1

이미 문제를 해결 했으므로 댓글을 달아 드리겠습니다. [ObjectAL] (http://kstenerud.github.io/ObjectAL-for-iPhone/)과 같은 오디오 라이브러리를 채택하면 인생을 훨씬 쉽게 할 수 있습니다. 다소 많은 양의 오디오를 가진 프로젝트를 수행하면 내 인생이 훨씬 쉬워졌습니다. – Travis

+0

큰 제안. 전에는 그 라이브러리에 대해 들어 본 적이 없었 습니다만, 반드시 확인해 보겠습니다. 감사! – mike

관련 문제