【问题标题】:Mute and unmute button? (Still needs an answer)静音和取消静音按钮? (仍然需要答案)
【发布时间】:2015-11-05 05:49:23
【问题描述】:

我已经坚持了一个多小时了。在我的主菜单屏幕中,我有一个静音按钮。我希望它在我的后台服务中调用一个方法,该方法将所有 MediaPlayer 音频静音。

这里我叫静音:

    public void mute(View view){
    mutebutton = (ImageButton) findViewById(R.id.mutebutton);

    currentVolume = manager.getStreamVolume(AudioManager.STREAM_MUSIC);

    if (currentVolume == 0) {
        TwentySeconds.unMute();
        Toast.makeText(Main_Menu.this, "UNMUTED", Toast.LENGTH_SHORT).show();
    } else {
        TwentySeconds.mute();
        Toast.makeText(Main_Menu.this, "MUTED", Toast.LENGTH_SHORT).show();
    }

这里是服务中的方法:

public static void mute(){
    ten.setVolume(0, 0);
    three.setVolume(0, 0);
}

public static void unMute(){
    ten.setVolume(1, 1);
    three.setVolume(1, 1);
}

以下是实际的媒体播放器,它们会间隔播放:

 static `MediaPlayer` ten;
static `MediaPlayer` three;

问题是,我在这里得到一个空指针异常:

currentVolume = manager.getStreamVolume(AudioManager.STREAM_MUSIC);

顺便说一句,经理是这样实例化的:

 AudioManager manager;

以后:

manager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

我非常感谢任何反馈(正面或负面)!非常感谢您的所有帮助,如果您需要更多代码,请告诉我。

 {Rich}

【问题讨论】:

  • 会不会在 manager 初始化之前调用 mute()?可能会向我们展示整个班级(只有相关的部分可以做)也可以提供帮助。

标签: java android audio service nullpointerexception


【解决方案1】:

你的“经理”应该像这样实例化:

AudioManager manager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);

如果这仍然是一个空点,添加

Context context = this;

在您的实例化之前。试试看,希望能帮到你。

【讨论】:

  • 感谢格蕾丝的回答!我仍然收到空指针异常...?
  • 啊...对不起,我没看到你写过“manager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);”到底。对不起!
  • 没关系,我仍然很感激你的回答。你知道那可能是什么问题吗?
  • 因为我的实例化实际上是在倒数计时器内,所以我得到以下信息:
  • 不兼容类型:必需:android.content.Context 找到:android.os.CountDownTimer
猜你喜欢
  • 2020-08-31
  • 2011-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 2018-01-30
相关资源
最近更新 更多