我正在开发一款应用程序,当它被震动时,会一遍又一遍地发出声音,直到震动停止。我正在使用多个AVAudioPlayers来实现这一点,它主要起作用,但有时候声音会失真,就像霸天虎会在变形金刚中发出的声音一样。有没有人知道为什么会发生这种情况?我使用的文件格式是mp3,代码如下:
财产声明:
@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;
}
答案 0 :(得分: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/找到了这个解决方案,所以非常感谢他们提供的代码。我希望这有助于其他人试图在设备震动时发出声音。