2011-02-03 55 views
0

我有點卡住了,我試圖編寫一個方法,當按下按鈕時;將獲得iTunes的當前音量,將音量存儲爲聲明爲x的int。然後使iTunes音量等於0,這會使iTunes音量基本靜音,但是如果再次按下該按鈕,我希望iTunes音量返回到int x,這將基本取消靜音iTunes音量並將其恢復到原始音量卷。Objective-C靜音和取消靜音的方法

這是我到目前爲止有:

- (IBAction)muteAndUnmute:(id)sender { 
    iTunesApplication *iTunes = [SBApplication applicationWithBundleIdentifier:@"com.apple.iTunes"]; 
    int x; 
    x = [iTunes soundVolume]; 
    if ([iTunes soundVolume] > 0) { 
     [volumeSlider setIntValue:0]; 
     [iTunes setSoundVolume:[volumeSlider intValue]]; 
     [volumeLabel setIntValue:[volumeSlider intValue]];} 
    } 

任何幫助將不勝感激,我認爲這是很容易做到,但就是不能提前讓我的頭周圍,謝謝,薩米。

+0

我已經改變了「靜音」標籤「可可」,因爲是你正在使用的主要框架,並沒有提到任何地方。 – nacho4d 2011-02-03 02:10:15

回答

2

讓你的音量值(體積)爲類變量,而不是本地的,那麼

// MyWindowController.h 
@interface MyWindowController : NSWindowController { 
    int vol; 
} 
- (IBAction)btnPressed:(id)sender; 
@end 


// MyWindowController.m  
@implementation MyWindowController 

- (id)init { 
    if (self = [super init]) { 
     vol = 0; 
    } 
    return self; 
} 


- (IBAction)btnPressed:(id)sender 
{ 

     id iTunes = [SBApplication applicationWithBundleIdentifier:@"com.apple.iTunes"]; 
     if ([iTunes soundVolume] > 0) 
     { 
      vol = [iTunes soundVolume]; 
      [iTunes setSoundVolume:0]; 
     } 
     else 
      [iTunes setSoundVolume:vol]; 

} 

@end 
+0

謝謝,它效果很好。 – Sami 2011-02-03 16:19:58

0

您可以使用此:

fVolume = [MPMusicPlayerController iPodMusicPlayer].volume; 
[MPMusicPlayerController iPodMusicPlayer].volume = 0.0; 

//do whatever you want 

[MPMusicPlayerController iPodMusicPlayer].volume = fVolume; 
相關問題