嘗試這樣,
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控制音樂庫音量。我試圖控制通話的音量。他們不是一個,他們是一樣的嗎? –