试试这样
1.定义属性并合成到您的 MPMusicPlayerController 对象,
@property (nonatomic, retain) MPMusicPlayerController *musicPlayer;
@synthesize musicPlayer;
2.取一个浮点变量为,
float volume_control=0;
3.在你的 viewDidLoad 方法中使用 NSNotification 来改变音量,
self.musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
NSNotificationCenter* notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self
selector:@selector(handleExternalVolumeChanged:)
name:MPMusicPlayerControllerVolumeDidChangeNotification
object:self.musicPlayer];
[self.musicPlayer beginGeneratingPlaybackNotifications];
4.define Method for handleExternalVolumeChanged: as,
- (void)handleExternalVolumeChanged:(id)notification {
volume_control = self.musicPlayer.volume;
}
5.现在定义你的音量控制按钮来增加/减少音量,
-(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;
}
}
希望它会帮助你..