ios - 在 iOS 上播放多种声音会产生变形金刚般的噪音

标签 ios objective-c audio mp3 avaudioplayer

我正在开发一款应用程序,当摇晃时,它会一遍又一遍地播放声音,直到摇晃停止。我正在使用多个 AVAudioPlayer 来完成此操作并且它大部分都能正常工作,但有时声音会失真,就像霸天虎在变形金刚中发出的声音一样。有没有人知道为什么会发生这种情况?我使用的文件格式是mp3,代码如下:

属性(property)声明:

@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;
}

最佳答案

为了获得我想要的声音,我最终使用了 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/ 找到了这个解决方案,非常感谢他们提供的代码。我希望这可以帮助其他人在摇动设备时尝试发出声音。

关于ios - 在 iOS 上播放多种声音会产生变形金刚般的噪音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20641627/

相关文章:

ios - 如何在 UICollectionViewCell 中缩放 UIScrollView?

javascript - 如何在 Qualtrics 上使用 Javascript 控制播放哪些 mp3 文件?

macOS Catalina AudioServerPlugIn 安装

python - 从目录中提取所有音频文件,并将它们放到一个新文件中。 python

iphone - TableView 添加单元格

ios - 未调用 UIWebView webViewDidLoadFinish 方法

objective-c - iOS中的上滑菜单

ios - 如何取消展开已展开的 UITableView 单元格?

ios - 关于监控 iphone 应用程序互联网 (http) 流量

iphone - sizeWithFont:constrainedToSize:lineBreakMode:使用UILineBreakModeClip时不正确?