我有一个应用程序,它使用 MPAudioPlayerController 来访问 iPod 音乐库,并使用 AVAudioPlayer 将音频覆盖在音乐之上。我用过本文档 http://developer.apple.com/iphone/library/DOCUMENTATION/Audio/Conceptual/AudioSessionProgrammingGuide/Configuration/Configuration.html#//apple_ref/doc/uid/TP40007875-CH3-SW2作为指导。具体来说:
最后,您可以增强一个类别,以在播放您的音频时自动降低其他音频的音量。例如,这可以用于锻炼应用程序。假设用户正在边听着 iPod 边锻炼,您的应用程序想要叠加一条口头消息,例如“您已经划船 10 分钟了”。为了确保应用程序中的消息易于理解,请将 kAudioSessionProperty_OtherMixableAudioShouldDuck 属性应用于音频会话。发生闪避时,设备上的所有其他音频(电话音频除外)的音量都会降低。
但我没有看到这种行为。事实上,我看到(或听到)的是,如果我将 AudioSession 设置为 kAudioSessionProperty_OtherMixableAudioShouldDuck 设置为 true,则 MPAudioPlayerController 初始音量会减小,如果我随后在 MPAudioPlayerController 上调用暂停(然后再次播放)音量级别增加到“正常”水平。播放 AVAudioPlayer 对音频电平没有任何影响...
所以我设置了一个简单的测试用例来重现这一点。
在 ViewController 标头中:
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
@interface MusicPlayerVolumeTestViewController : UIViewController <AVAudioPlayerDelegate>
{
UIButton *musicButton;
UIButton *soundButton;
AVAudioPlayer *audioPlayer;
MPMusicPlayerController *musicPlayerController;
}
@property (nonatomic, retain) IBOutlet UIButton *musicButton;
@property (nonatomic, retain) IBOutlet UIButton *soundButton;
@property (nonatomic, retain) MPMusicPlayerController *musicPlayerController;
- (IBAction)musicAction;
- (IBAction)soundAction;
@end
并在实施中:
- (void)viewDidLoad
{
[super viewDidLoad];
//Setup our Audio Session
OSStatus status = AudioSessionInitialize(NULL, NULL, NULL, NULL);
//We want our audio to play if the screen is locked or the mute switch is on
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
status = AudioSessionSetProperty (kAudioSessionProperty_AudioCategory, sizeof (sessionCategory), &sessionCategory);
//We want our audio to mix with other app's audio
UInt32 shouldMix = true;
status = AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof (shouldMix), &shouldMix);
//Enable "ducking" of the iPod volume level while our sounds are playing
UInt32 shouldDuck = true;
AudioSessionSetProperty(kAudioSessionProperty_OtherMixableAudioShouldDuck, sizeof(shouldDuck), &shouldDuck);
//Activate our audio session
AudioSessionSetActive(YES);
//Setup the Music Player to access the iPod music library
self.musicPlayerController = [MPMusicPlayerController applicationMusicPlayer];
[self.musicPlayerController setShuffleMode: MPMusicShuffleModeSongs];
[self.musicPlayerController setRepeatMode: MPMusicRepeatModeNone];
[self.musicPlayerController setQueueWithQuery:[MPMediaQuery songsQuery]];
//Setup a AVAudioPlayer sound to overlay against the Music Player audio
NSURL *soundURL = [NSURL URLWithString:[[NSBundle mainBundle] pathForResource:@"overlay" ofType:@"mp3"]];
NSError *error = nil;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error: &error];
if (!audioPlayer)
{
NSLog(@"Could not create audio effect player: %@", [error localizedDescription]);
}
[audioPlayer prepareToPlay];
}
- (IBAction)musicAction
{
if (self.musicPlayerController.playbackState == MPMusicPlaybackStatePlaying)
{
[self.musicPlayerController pause];
}
else if (self.musicPlayerController.playbackState == MPMusicPlaybackStateStopped
|| self.musicPlayerController.playbackState == MPMusicPlaybackStatePaused)
{
[self.musicPlayerController play];
}
}
- (IBAction)soundAction
{
if (audioPlayer.playing)
{
[audioPlayer pause];
}
else
{
[audioPlayer play];
}
}
我已经连接了几个 UIButton。一种用于 musicAction(用于播放/暂停 MPMusicPlayerController),另一种用于 soundAction(用于播放/暂停 AVAudioPlayer)。
如前所述,如果我点击 musicAction 按钮,则会播放音乐,但音量会降低;如果我点击 soundAction 按钮,则会播放叠加层,但不会影响 MPMusicPlayerController 的音量。而且,更类似错误的是,当我暂停然后播放 MPMusicPlayerController 时,音乐的音量会增加到我没有设置 AudioSession 时的水平。
我很想知道其他人是否有过这种经历,如果有的话,您是否找到了解决方法(或者可以告诉我我做错了什么)。否则,我想我要去雷达了。
非常感谢,
Levi