ios – 降低iPod音量以播放应用程序声音(如本机SMS应用程序)

前端之家收集整理的这篇文章主要介绍了ios – 降低iPod音量以播放应用程序声音(如本机SMS应用程序)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在做运动时制作了一个iPhone应用程序.它发出一个铃声,表明你(用户)应该从你的锻炼的一个步骤切换到下一个.我设计了这个应用程序,以便您可以在使用应用程序的同时聆听iPod上的音乐,而且我想让音色在音乐上充分地听到.我已经得到这个工作…有点…

当音乐响起时,很难听到声调.我的理想解决方案类似于iPod应用程序处理传入短信或电子邮件的方式.音乐音量降低,声音播放,然后音乐音量淡出.

以下是我迄今为止所尝试的方法

>我已经使用AudioServicesPlaySystemSound播放声音.

我初始化了这样的声音:

  1. CFBundleRef mainBundle = CFBundleGetMainBundle();
  2. soundFileURLRef = CFBundleCopyResourceURL(mainBundle,CFSTR ("bell"),CFSTR ("wav"),NULL);
  3. AudioServicesCreateSystemSoundID (soundFileURLRef,&soundFileID);

我在适当的时候播放声音,使用:

  1. AudioServicesPlaySystemSound (self.soundFileID);

这听起来很好,但听到音乐太难听了.在尝试2 …
>我试图降低iPod音量,播放声音,然后将音量恢复到上一级.

如果当前步骤剩下1秒,开始降低音量:

  1. if ([self.intervalSet currentStepTimeRemainingInSeconds] == 1) {
  2. self.volumeIncrement = originalVolume/5.0f;
  3. [NSTimer scheduledTimerWithTimeInterval:0.5f
  4. target:self
  5. selector:@selector(fadeVolumeOut:)
  6. userInfo:[NSNumber numberWithInt:1]
  7. repeats:NO];
  8. }

这是fadeVolumeOut:方法

  1. - (void) fadeVolumeOut:(NSTimer *)timer {
  2. if (!TARGET_IPHONE_SIMULATOR) {
  3. NSNumber *volumeStep = (NSNumber *)[timer userInfo];
  4. int vStep = [(NSNumber *)volumeStep intValue];
  5. float volume = [[MPMusicPlayerController iPodMusicPlayer] volume];
  6. volume = volume - self.volumeIncrement;
  7. if (volume < 0.0f) {
  8. volume = 0.0f;
  9. }
  10. [[MPMusicPlayerController iPodMusicPlayer] setVolume:volume];
  11. if (vStep < 5) {
  12. vStep = vStep + 1;
  13. [NSTimer scheduledTimerWithTimeInterval:0.1f
  14. target:self
  15. selector:@selector(fadeVolumeOut:)
  16. userInfo:[NSNumber numberWithInt:vStep]
  17. repeats:NO];
  18. }
  19. }
  20. }

然后,当步骤结束时,播放警报声音并将音量淡出:

  1. [NSTimer scheduledTimerWithTimeInterval:0.1f
  2. target:self
  3. selector:@selector(alertAndFadeVolumeIn)
  4. userInfo:nil
  5. repeats:NO];

这里是alertAndFadeVolumeIn方法

  1. - (void) alertAndFadeVolumeIn {
  2. [self alert];
  3. [NSTimer scheduledTimerWithTimeInterval:0.25f
  4. target:self
  5. selector:@selector(fadeVolumeIn:)
  6. userInfo:[NSNumber numberWithInt:1]
  7. repeats:NO];
  8. }

和fadeVolumeIn:基本上是相反的fadeVolumeOut:上面.

这样做,音量消失,音量消失.问题是音量降低了与iPod相同的音量,所以不会让音乐听起来更容易.
>我切换到AVAudioSession播放声音,并设置会话,以便iPod音乐将在应用程序正在使用中继续播放.以下是我初始化会话的方式:

  1. AVAudioSession *session = [AVAudioSession sharedInstance];
  2. [session setCategory:AVAudioSessionCategoryPlayback error:nil];
  3.  
  4. OSStatus propertySetError = 0;
  5. UInt32 allowMixing = true;
  6. propertySetError = AudioSessionSetProperty (
  7. kAudioSessionProperty_OverrideCategoryMixWithOthers,sizeof (allowMixing),&allowMixing
  8. );
  9.  
  10. NSError *activationError = nil;
  11. [session setActive:YES error:&activationError];
  12.  
  13. NSString *audioFile = [[NSBundle mainBundle] pathForResource:@"bell"
  14. ofType:@"wav"];
  15. player = [[AVAudioPlayer alloc] initWithContentsOfURL:
  16. [NSURL fileURLWithPath:audioFile] error:NULL];

要播放声音,我在适当的时候打电话给[self.player play].再次,音量随着iPod音量的降低而下降,音调不会更容易听到.
我试过把[[MPMusicPlayerController applicationMusicPlayer] setVolume:1.0f];在警报声音播放之前.这有不同的结果.声音第一次按照我的期望全部播放,但随后的音量要低得多.此外,音乐不会顺利淡出.看来iPodMusicPlayer和applicationMusicPlayer共享相同的音量.这是使用[AVAudioSession sharedInstance]的结果吗?还有另一种初始化AVAudioSession的方法吗?
>接下来,我试过使用AVAudioSession“ducking”:

  1. OSStatus propertySetError = 0;
  2. UInt32 allowDucking = true;
  3. propertySetError = AudioSessionSetProperty (
  4. kAudioSessionProperty_OtherMixableAudioShouldDuck,sizeof (allowDucking),&allowDucking
  5. );

不幸的是,当音频会话被激活时,iPod音乐“ducks”,一旦viewController加载了我有事情的方式.
>最后,我更改了我的代码,使得音频会话在步骤结束前一秒钟被激活,声音在步骤结束时播放,一秒钟后,会话被禁用.在这一点上,我已经删除了所有的退出代码.以下是相关代码段:

  1. if ([self.intervalSet currentStepTimeRemainingInSeconds] == 1) {
  2. AVAudioSession *session = [AVAudioSession sharedInstance];
  3. [session setActive:YES error:nil];
  4. }

  1. if (self.shouldRing) {
  2. [self.player play];
  3. [NSTimer scheduledTimerWithTimeInterval:1.0f
  4. target:self
  5. selector:@selector(stopSound)
  6. userInfo:nil
  7. repeats:NO];
  8. }

  1. - (void) stopSound {
  2. [self.player stop];
  3. AVAudioSession *session = [AVAudioSession sharedInstance];
  4. [session setActive:NO error:nil];
  5. }

这让我陷入困境.第一步结束后,这样做完美. iPod音量duck duck,loud loud loud loud,the the……….然而,第二次,一声一声的声音播放的声音稍微低一些,第三次几乎听不到声音,第四次无声.然后,第五次,它再次大声响起,周期重复.

最重要的是,激活和停用似乎是相当沉重的操作,并导致计时器稍微点燃.

有人试图做类似的事情吗?是否有首选方法来播放iPod音乐的声音?

解决方法

AVAudioSession是处理“ audio behavior at the application,interapplication,and device levels.”的正确方式,我在iOS 4上使用了稍微修改的#6,没有任何问题.背景音频消失,我的声音播放,背景音频消失.

>初始化音频会话(删除错误处理代码):

  1. AVAudioSession* audioSession = [AVAudioSession sharedInstance];
  2. [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil]
  3. [audioSession setActive:NO error:nil];

播放声音(AVAudioPlayer的播放/ prepareToPlay将为activate the audio session for you):

  1. AVAudioPlayer* audioPlayer = [[[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:nil];
  2. [audioPlayer play];

>停止声音:

  1. [audioPlayer stop];
  2. [[AVAudioSession sharedInstance] setActive:NO withFlags:AVAudioSessionSetActiveFlags_NotifyOthersOnDeactivation error:nil];

标志kAudioSessionSetActiveFlag_NotifyOthersOnDeactivation(iOS 4的新功能)告诉系统通知背景音频恢复播放.

猜你在找的iOS相关文章