当音乐响起时,很难听到声调.我的理想解决方案类似于iPod应用程序处理传入短信或电子邮件的方式.音乐音量降低,声音播放,然后音乐音量淡出.
以下是我迄今为止所尝试的方法:
>我已经使用AudioServicesPlaySystemSound播放声音.
我初始化了这样的声音:
- CFBundleRef mainBundle = CFBundleGetMainBundle();
- soundFileURLRef = CFBundleCopyResourceURL(mainBundle,CFSTR ("bell"),CFSTR ("wav"),NULL);
- AudioServicesCreateSystemSoundID (soundFileURLRef,&soundFileID);
我在适当的时候播放声音,使用:
- AudioServicesPlaySystemSound (self.soundFileID);
这听起来很好,但听到音乐太难听了.在尝试2 …
>我试图降低iPod音量,播放声音,然后将音量恢复到上一级.
如果当前步骤剩下1秒,开始降低音量:
- if ([self.intervalSet currentStepTimeRemainingInSeconds] == 1) {
- self.volumeIncrement = originalVolume/5.0f;
- [NSTimer scheduledTimerWithTimeInterval:0.5f
- target:self
- selector:@selector(fadeVolumeOut:)
- userInfo:[NSNumber numberWithInt:1]
- repeats:NO];
- }
这是fadeVolumeOut:方法:
- - (void) fadeVolumeOut:(NSTimer *)timer {
- if (!TARGET_IPHONE_SIMULATOR) {
- NSNumber *volumeStep = (NSNumber *)[timer userInfo];
- int vStep = [(NSNumber *)volumeStep intValue];
- float volume = [[MPMusicPlayerController iPodMusicPlayer] volume];
- volume = volume - self.volumeIncrement;
- if (volume < 0.0f) {
- volume = 0.0f;
- }
- [[MPMusicPlayerController iPodMusicPlayer] setVolume:volume];
- if (vStep < 5) {
- vStep = vStep + 1;
- [NSTimer scheduledTimerWithTimeInterval:0.1f
- target:self
- selector:@selector(fadeVolumeOut:)
- userInfo:[NSNumber numberWithInt:vStep]
- repeats:NO];
- }
- }
- }
然后,当步骤结束时,播放警报声音并将音量淡出:
- [NSTimer scheduledTimerWithTimeInterval:0.1f
- target:self
- selector:@selector(alertAndFadeVolumeIn)
- userInfo:nil
- repeats:NO];
这里是alertAndFadeVolumeIn方法:
- - (void) alertAndFadeVolumeIn {
- [self alert];
- [NSTimer scheduledTimerWithTimeInterval:0.25f
- target:self
- selector:@selector(fadeVolumeIn:)
- userInfo:[NSNumber numberWithInt:1]
- repeats:NO];
- }
和fadeVolumeIn:基本上是相反的fadeVolumeOut:上面.
这样做,音量消失,音量消失.问题是音量降低了与iPod相同的音量,所以不会让音乐听起来更容易.
>我切换到AVAudioSession播放声音,并设置会话,以便iPod音乐将在应用程序正在使用中继续播放.以下是我初始化会话的方式:
- AVAudioSession *session = [AVAudioSession sharedInstance];
- [session setCategory:AVAudioSessionCategoryPlayback error:nil];
- OSStatus propertySetError = 0;
- UInt32 allowMixing = true;
- propertySetError = AudioSessionSetProperty (
- kAudioSessionProperty_OverrideCategoryMixWithOthers,sizeof (allowMixing),&allowMixing
- );
- NSError *activationError = nil;
- [session setActive:YES error:&activationError];
- NSString *audioFile = [[NSBundle mainBundle] pathForResource:@"bell"
- ofType:@"wav"];
- player = [[AVAudioPlayer alloc] initWithContentsOfURL:
- [NSURL fileURLWithPath:audioFile] error:NULL];
要播放声音,我在适当的时候打电话给[self.player play].再次,音量随着iPod音量的降低而下降,音调不会更容易听到.
我试过把[[MPMusicPlayerController applicationMusicPlayer] setVolume:1.0f];在警报声音播放之前.这有不同的结果.声音第一次按照我的期望全部播放,但随后的音量要低得多.此外,音乐不会顺利淡出.看来iPodMusicPlayer和applicationMusicPlayer共享相同的音量.这是使用[AVAudioSession sharedInstance]的结果吗?还有另一种初始化AVAudioSession的方法吗?
>接下来,我试过使用AVAudioSession“ducking”:
不幸的是,当音频会话被激活时,iPod音乐“ducks”,一旦viewController加载了我有事情的方式.
>最后,我更改了我的代码,使得音频会话在步骤结束前一秒钟被激活,声音在步骤结束时播放,一秒钟后,会话被禁用.在这一点上,我已经删除了所有的退出代码.以下是相关代码段:
…
- if (self.shouldRing) {
- [self.player play];
- [NSTimer scheduledTimerWithTimeInterval:1.0f
- target:self
- selector:@selector(stopSound)
- userInfo:nil
- repeats:NO];
- }
…
这让我陷入困境.第一步结束后,这样做完美. iPod音量duck duck,loud loud loud loud,the the……….然而,第二次,一声一声的声音播放的声音稍微低一些,第三次几乎听不到声音,第四次无声.然后,第五次,它再次大声响起,周期重复.
最重要的是,激活和停用似乎是相当沉重的操作,并导致计时器稍微点燃.
有人试图做类似的事情吗?是否有首选方法来播放iPod音乐的声音?
解决方法
播放声音(AVAudioPlayer的播放/ prepareToPlay将为activate the audio session for you):
>停止声音:
标志kAudioSessionSetActiveFlag_NotifyOthersOnDeactivation(iOS 4的新功能)告诉系统通知背景音频恢复播放.