簡體   English   中英

降低iPod音量以播放應用程序聲音(如本機SMS應用程序)

[英]Lower iPod volume to play app sound (like the native SMS app)

我做了一個iPhone應用程序,以便在鍛煉時使用。 它會播放鈴聲,表示您(用戶)應該從練習例程的一個步驟切換到下一個步驟。 我設計了應用程序,以便您在使用應用程序時可以在iPod上聽音樂,我希望音調可以在音樂上充分發聲。 我已經讓這個工作......有點......

當音樂響亮時,很難聽到音調。 我理想的解決方案類似於iPod應用程序處理傳入短信或電子郵件的方式。 音樂音量降低,聲音播放,音樂音量逐漸消失。

以下是我到目前為止嘗試過的方法:

  1. 我用AudioServicesPlaySystemSound來播放聲音。

    我初始化聲音是這樣的:

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

    我在適當的時候使用以下方式播放聲音:

     AudioServicesPlaySystemSound (self.soundFileID); 

    這播放聲音很好,但是聽到嘈雜的音樂太難了。 試圖2 ...

  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:基本上與fadeVolumeIn:相反fadeVolumeOut:上面。

    這有效,音量消失,聲音播放,音量逐漸消失。問題是音量降低了與iPod相同的音量,因此它不會讓音樂更容易聽到。

  3. 我切換到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音量一起降低,音調也不容易聽到。

  4. 我試過把[[MPMusicPlayerController applicationMusicPlayer] setVolume:1.0f]; 在警報聲播放之前。 結果好壞參半。 第一次按照我的預期,音量以滿音量播放,但隨后音量會低很多。 此外,音樂不會平滑淡出。 看起來iPodMusicPlayerapplicationMusicPlayer共享相同的音量。 這是使用[AVAudioSession sharedInstance]; 有沒有其他方法來初始化AVAudioSession

  5. 接下來,我嘗試使用AVAudioSession “躲避”:

      OSStatus propertySetError = 0; UInt32 allowDucking = true; propertySetError = AudioSessionSetProperty ( kAudioSessionProperty_OtherMixableAudioShouldDuck, sizeof (allowDucking), &allowDucking ); 

    不幸的是,當音頻會話被激活時,iPod音樂會“躲避”,就像我一樣裝載viewController一樣。

  6. 最后,我更改了我的代碼,以便音頻會話在步驟結束前一秒激活,聲音在步驟結束時播放,一秒鍾后,會話被停用。 此時我已經刪除了所有淡入淡出的代碼。 以下是相關的代碼段:

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

    ...

     if (self.shouldRing) { [self.player play]; [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(stopSound) userInfo:nil repeats:NO]; } 

    ...

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

    這讓我陷入了困境。 這在第一步結束后完美地起作用。 iPod音量下降,聲音大聲播放,iPod音量在一秒后消失。 然而,第二次步驟結束時,聲音播放的聲音稍微不那么大,第三次幾乎聽不到聲音,第四次聲音靜音。 然后,第五次,它再次大聲播放,循環重復。

    最重要的是,激活和停用似乎是非常繁重的操作,它們導致計時器稍微斷斷續續。

有沒有人試圖做類似的事情? 是否有一種首選的方法來播放iPod音樂的聲音?

AVAudioSession是處理“ 應用程序,應用程序和設備級別的音頻行為 ”的正確方法我在iOS 4上使用了略微修改的#6,沒有任何問題。 背景音頻淡出,我的聲音播放,背景音頻消失了。

  1. 初始化音頻會話(刪除錯誤處理代碼):

     AVAudioSession* audioSession = [AVAudioSession sharedInstance]; [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil] [audioSession setActive:NO error:nil]; 
  2. 播放聲音( AVAudioPlayer的播放/ prepareToPlay將為您激活音頻會話 ):

     AVAudioPlayer* audioPlayer = [[[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:nil]; [audioPlayer play]; 
  3. 停止聲音:

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

    標志kAudioSessionSetActiveFlag_NotifyOthersOnDeactivation (iOS 4新增功能)告訴系統通知后台音頻恢復播放。

做你原來想做的事的正確方法是你的(5) - 使用躲避。 這是交易。 使用允許其他應用播放聲音的音頻會話類型(播放)。 在你即將播放聲音之前不要打開閃避。 當您即將播放聲音時,請打開閃避! 背景音樂會立即消失,因此可以聽到您的聲音。 然后,當你的聲音播放完畢后,關閉閃避現在(這是技巧)停用你的音頻會話並再次激活它,使背景音樂立即恢復以前的響度:

UInt32 duck = 0;
AudioSessionSetProperty(kAudioSessionProperty_OtherMixableAudioShouldDuck, sizeof(duck), &duck);
AudioSessionSetActive(false);
AudioSessionSetActive(true);

編輯:在iOS 6中,您可以(並且應該)僅使用Objective-C API完成所有這些操作。 所以,開始躲避其他音頻:

[[AVAudioSession sharedInstance] 
    setCategory: AVAudioSessionCategoryAmbient
    withOptions: AVAudioSessionCategoryOptionDuckOthers
          error: nil];

播放完聲音后,關閉閃避:

[[AVAudioSession sharedInstance] setActive:NO withOptions:0 error:nil];
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient
                                 withOptions: 0
                                       error: nil];
[[AVAudioSession sharedInstance] setActive:YES withOptions: 0 error:nil];

為什么不回到#2? 代替:

  1. 淡出
  2. 播放提醒
  3. 淡入

使用:

  1. 淡出
  2. 暫停音樂
  3. 提高音量
  4. 播放提醒
  5. 降低音量
  6. 播放音樂
  7. 淡入

對於Xcode 8,iOS 10

目的:

像導航應用程序一樣工作(Waze / Google Maps / RideAustin)播放您的聲音,減少背景音樂音量。 播放聲音后,恢復背景音樂音量

執行:

初始化:

{
    NSError *error = nil;
    [[AVAudioSession sharedInstance] 
        setCategory: AVAudioSessionCategoryPlayback
        withOptions: AVAudioSessionCategoryOptionDuckOthers | AVAudioSessionCategoryOptionMixWithOthers
              error: nil];
    [[AVAudioSession sharedInstance] setPreferredIOBufferDuration:0.005 error:&error];
    ...
}

播放聲音時

{
    ...
    NSError *error = nil;
    AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error];

    //set delegate
    player.delegate = self
    ...
    [player play]
}

確保其他音樂播放器應用程序卷將恢復工具

#pragma mark - AVAudioPlayerDelegate
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
    ...
    [[AVAudioSession sharedInstance] setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:&error];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM