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

发布于 2024-10-15 18:50:59 字数 648 浏览 9 评论 0原文

我有点卡住了,我正在尝试编写一种方法,当按下按钮时;将获取 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]];}
    }

任何帮助将不胜感激,我认为这很容易做到,但我无法理解它,提前感谢,萨米。

I'm a little stuck, i'm trying to write a method which when a button is pressed; will get the current volume of iTunes, store the volume as an int declared as x. Then make the iTunes volume equal to 0, which will essentially mute the iTunes volume, but then i want the iTunes volume to return to the int x if the button is pressed again, which will essentially unmute the iTunes volume and restore it to the original volume.

Here's what i have so far:

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

Any help would be much appreciated, i think it's quite easy to do but just can't get my head around it, thanks in advance, Sami.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

杯别 2024-10-22 18:50:59

让你的体积值(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

make your volume value (vol) as class variable and not local, then

// 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
感悟人生的甜 2024-10-22 18:50:59

你可以使用这个:

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

//do whatever you want

[MPMusicPlayerController iPodMusicPlayer].volume = fVolume;

you can use this:

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

//do whatever you want

[MPMusicPlayerController iPodMusicPlayer].volume = fVolume;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文