【问题标题】:Play notification default sound only (Android)仅播放通知默认声音 (Android)
【发布时间】:2012-05-07 06:46:39
【问题描述】:

如何只播放通知声音(不触发状态栏通知)?我想要通知默认声音并将其完全作为通知声音播放。是否可以使用 MediaPlayer 来实现?

【问题讨论】:

  • 如果你只是找到声音文件,你可以用媒体播放器来做。您应该能够在 android 源中的某处找到默认声音。查看 Notification-class 和 this 问题。

标签: android audio notifications


【解决方案1】:
Uri defaultRingtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

MediaPlayer mediaPlayer = new MediaPlayer();

try {
      mediaPlayer.setDataSource(context, defaultRingtoneUri);
      mediaPlayer.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
      mediaPlayer.prepare();
      mediaPlayer.setOnCompletionListener(new OnCompletionListener() {

         @Override
         public void onCompletion(MediaPlayer mp)
         {
            mp.release();
         }
      });
  mediaPlayer.start();
} catch (IllegalArgumentException e) {
 e.printStackTrace();
} catch (SecurityException e) {
 e.printStackTrace();
} catch (IllegalStateException e) {
 e.printStackTrace();
} catch (IOException e) {
 e.printStackTrace();
}

【讨论】:

    【解决方案2】:
    Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    Ringtone r = RingtoneManager.getRingtone(context.getApplicationContext(), notification);
    r.play();
    

    【讨论】:

      【解决方案3】:

      你可以试试这个

      private void sendNotification(String title, String messageBody,String tag) {
      
          Intent intent;
          intent = new Intent(this, MainActivity.class);
          intent.setAction(tag);
          intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
          PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                  PendingIntent.FLAG_ONE_SHOT);
          String channel_id=this.getResources().getString(R.string.app_name);
          Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
          NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                  .setSmallIcon(R.mipmap.ic_launcher)
                  .setContentTitle(title)
                  .setContentText(messageBody)
                  .setStyle(new NotificationCompat.BigTextStyle()
                          .bigText(messageBody))
                  .setAutoCancel(true)
                  .setSound(defaultSoundUri)
                  .setChannelId(channel_id)
                  .setContentIntent(pendingIntent);
      
          NotificationManager notificationManager =
                  (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
          NotificationChannel mChannel = null;
          if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
              mChannel = new NotificationChannel(channel_id, channel_id, NotificationManager.IMPORTANCE_DEFAULT);
              notificationManager.createNotificationChannel(mChannel);
          }
          notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
      }
      } 
      

      【讨论】:

        猜你喜欢
        • 2012-01-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多