Objective-C静音和取消静音音量的方法

时间:2011-02-03 01:42:45

标签: objective-c cocoa xcode itunes volume

我有点陷入困境,我正在尝试编写按下按钮的方法;将获得iTunes的当前音量,将音量存储为声明为x的int。然后使iTunes音量等于0,这将基本上使iTunes音量静音,但是如果再次按下该按钮,我希望iTunes音量返回到int x,这将基本上取消静音iTunes音量并将其恢复为原始音量体积。

这是我到目前为止所拥有的:

- (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]];}
    }

任何帮助都会非常感激,我认为这很容易做到,但是我不能理解它,感谢提前,萨米。

2 个答案:

答案 0 :(得分:2)

将您的音量值(vol)设为类变量而非本地,然后

// 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

答案 1 :(得分:0)

你可以用这个:

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

//do whatever you want

[MPMusicPlayerController iPodMusicPlayer].volume = fVolume;