尝试这样,
1.定义属性和合成你MPMusicPlayerController对象,
@property (nonatomic, retain) MPMusicPlayerController *musicPlayer;
@synthesize musicPlayer;
2.take一个浮点变量,
float volume_control=0;
3.使用NSNotification在你的viewDidLoad方法如下改变音量,
self.musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
NSNotificationCenter* notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self
selector:@selector(handleExternalVolumeChanged:)
name:MPMusicPlayerControllerVolumeDidChangeNotification
object:self.musicPlayer];
[self.musicPlayer beginGeneratingPlaybackNotifications];
为handleExternalVolumeChanged
4.define方法:如,
- (void)handleExternalVolumeChanged:(id)notification {
volume_control = self.musicPlayer.volume;
}
5.Now定义你的音量控制按钮增大/减小音量,
-(IBAction)decrease_volume:(id)sender{
if (volume_control>0) {
self.musicPlayer.volume =self.musicPlayer.volume-0.1;
volume_control-=0.1;
}
}
-(IBAction)increase_volume:(id)sender{
if (volume_control<1) {
self.musicPlayer.volume =self.musicPlayer.volume+0.1;
volume_control+=0.1;
}
}
希望它会帮助你..
MPMusicPlayerController控制音乐库音量。我试图控制通话的音量。他们不是一个,他们是一样的吗? –