【问题标题】:Mute Button not working静音按钮不起作用
【发布时间】:2018-02-14 11:27:49
【问题描述】:

您好,我正在尝试在我的 android 项目应用程序中添加一个静音按钮,只是学习基础知识,这就是我所做的。我创建了一个按钮(这只是现在,将来我会尝试使用 Switch)id 设置为 android:id="@+id/muteBtn" 并在 java Button muteBtn = (Button) findViewById(R.id.muteBtn) ; 中声明变量 当它点击它时,它必须将模式设置为静音模式我添加了一个user-permission,因为它给了我一个错误Not allowed to change Do Not Disturb state

 <uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />

这里是按钮点击监听器

 muteBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
                    && !notificationManager.isNotificationPolicyAccessGranted()) {

                Intent intent = new Intent(
                        android.provider.Settings
                                .ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);

                startActivity(intent);
            }
            else {
                AudioManager audioManager = (AudioManager) Settings.this.getSystemService(Context.AUDIO_SERVICE);
                audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
            }
        }
    });

这个方法没有任何建议?

更新 1

我刚刚注意到此代码有效,但 ringer 音量下降而不是 media

【问题讨论】:

标签: java android


【解决方案1】:

试试这个:我假设您想降低音乐音量而不是将手机置于静音模式...如果我错了请纠正我..

    if (!isMute) {
        AudioManager mAudioManager = (AudioManager) activity.getSystemService(Context.AUDIO_SERVICE);
        currentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        isMute = true;
        mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);
        imageButtonMute.setImageDrawable(getResources().getDrawable(R.mipmap.mute_off));
     } else {

 imageButtonMute.setImageDrawable(getResources().getDrawable(R.mipmap.mute));
      AudioManager mAudioManager = (AudioManager) 
      activity.getSystemService(Context.AUDIO_SERVICE);
     isMute = false;                       
     mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume, 0);
    }

【讨论】:

  • 为什么它给我同样的错误Non-static method setMode(int) cannot be referenced from a static content im inside onCreate 方法
  • 我没有使用 setMode 方法,你能发布它给出错误的确切行吗?
  • ` AudioManager mAudioManager = (AudioManager) activity.getSystemService(Context.AUDIO_SERVICE);` 我在这里更改单词活动并替换我的Settings java 类,然后getSystemService 变为红色下划线并说出我提到的错误
  • 将其更改为 Settings.this
  • AudioManager mAAudioManager = (AudioManager) Settings.this.getSystemService(Context.AUDIO_SERVICE);
【解决方案2】:

public void onClick(View v) {

  switch (v.getId()) {
  case R.id.wmute:
    AudioManager.setMode(AudioManager.MODE_IN_CALL);
    AudioManager.setStreamSolo(AudioManager.STREAM_VOICE_CALL, true);
     break;
  default:
     break;
  }

} }

   }

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多