【问题标题】:Setting notification sound volume设置通知音量
【发布时间】:2019-04-06 07:08:13
【问题描述】:

我使用NotificationChannel 来定义我的应用程序的通知。我用以下代码设置它的声音:

AudioAttributes.Builder constructeurAttributsAudio=new AudioAttributes.Builder();
constructeurAttributsAudio.setUsage(AudioAttributes.USAGE_NOTIFICATION_EVENT);
canalNotification.setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE    + "://" + contexte.getPackageName() + "/raw/cloche"),constructeurAttributsAudio.build());

当通知出现时,声音是正确发出的,但它的音量设置为最大并且没有考虑用户为通知设置的声音级别。有谁知道我如何让我的应用将通知声音级别设置为用户选择的值?

编辑#1:如果我复制代码以在应用程序主体中执行它(由Button 点击触发)而不是在我的BroadcastReceiveronReceive 方法中,通知声音会在用户为通知选择的声音级别。

编辑#2:奇怪的是,当应用程序在模拟器上执行时,通知声音级别是正确的!原因可能是手机配置中的参数吗? (它们都在 Android 9 下运行)。

【问题讨论】:

    标签: android android-notifications


    【解决方案1】:

    我使用下面的代码,希望对你也有用:

     private void setvolume(int volume)
     {
    AudioManager manager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
    manager.setStreamVolume(AudioManager.STREAM_MUSIC, volume, 0);
    
    Uri notification = RingtoneManager
            .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    
    MediaPlayer player = MediaPlayer.create(getApplicationContext(), notification);
    player.start();
    

    }

    【讨论】:

    • 我只想在用户设置的通知级别发出通知声音。如果可以以编程方式获得此级别,您的解决方案可能对我有用。你知道这是否可能吗?
    【解决方案2】:

    卸载并重新安装应用程序解决了问题!

    【讨论】:

      猜你喜欢
      • 2011-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-13
      • 1970-01-01
      • 2012-02-06
      • 1970-01-01
      相关资源
      最近更新 更多