나는 진동 할 때 진동이 멈출 때까지 소리를 계속 반복 재생합니다. 저는 이것을 달성하기 위해 여러 개의 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;
}
이미 문제를 해결 했으므로 댓글을 달아 드리겠습니다. [ObjectAL] (http://kstenerud.github.io/ObjectAL-for-iPhone/)과 같은 오디오 라이브러리를 채택하면 인생을 훨씬 쉽게 할 수 있습니다. 다소 많은 양의 오디오를 가진 프로젝트를 수행하면 내 인생이 훨씬 쉬워졌습니다. – Travis
큰 제안. 전에는 그 라이브러리에 대해 들어 본 적이 없었 습니다만, 반드시 확인해 보겠습니다. 감사! – mike