为什么不播放铃声?

时间:2019-03-28 15:17:25

标签: java android uri ringtonemanager

我有一个代码,其中会弹出一个通知以及从 RINGTONE_PICKER 中选择的自定义声音。但是由于某些原因,按下btn1按钮后,自定义声音无法播放(仅发生振动)。

选择铃声的代码:

public static Ringtone ringTone;
final int RQS_RINGTONEPICKER = 1;
      .
      .
      .

btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
            startActivity(intent);
        }
    });
           .
           .
           .
@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == RQS_RINGTONEPICKER && resultCode == RESULT_OK) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
                ringTone = RingtoneManager.getRingtone(getApplicationContext(), uri);
                ringTone.play();
            }
        }
    }

用于创建通知的代码:

 long[] pattern = {500,500,500,500,500,500,500,500,500};

 NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_today_black_24dp)
            .setContentTitle(s3)
            .setContentText(message)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setLights(Color.BLUE, 500, 500)
            .setVibrate(pattern);





    NotificationManagerCompat notification=NotificationManagerCompat.from(context);
    notification.notify(NOTIFICATION_ID,builder.build());

0 个答案:

没有答案