1

私のアプリにはアラーム機能があります。私の要件は、アラームが鳴ったときに自分の音を鳴らすことですが、これを行うことはできません。アラームが鳴ったときにのみ通知が表示されます。再生したいサウンドファイルは、Resのrawフォルダー内にあります。以下に私のコードを投稿しています:

私の活動クラスでは:

Intent AlarmIntent = new Intent(getApplicationContext(), AlarmReceiver.class);
        AlarmIntent.putExtra("Ringtone", 
                Uri.parse("getResources().getResourceName(R.raw.shankh_final_mid)"));
        PendingIntent Sender = PendingIntent.getBroadcast(this, 0, AlarmIntent, 0);
        AlarmManager AlmMgr = (AlarmManager)getSystemService(ALARM_SERVICE);
        AlmMgr.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 
                (60 * 1000), (24 * 60 * 60 * 1000), Sender);

レシーバークラス:

public void onReceive(Context context, Intent intent) { 

    Intent in = new Intent(context, SnoozeEvent.class);
    in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent Sender = PendingIntent.getActivity(context, 0, in, PendingIntent.FLAG_UPDATE_CURRENT);
    manager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    notification = new Notification(R.drawable.icon, "Wake up alarm", System.currentTimeMillis());
    notification.setLatestEventInfo(context, "Hanuman Chalisa", "Wake Up...", Sender);
    notification.flags = Notification.FLAG_INSISTENT;
    notification.sound = (Uri)intent.getParcelableExtra("Ringtone");
    manager.notify(1, notification);  
}
4

4 に答える 4

5

バンドルまたはSDカードにカスタムサウンドを設定できます。サウンドファイルのパスのみを通知サウンドとして設定します。以下のコードを使用してください。

notification.sound = Uri.parse("android.resource://my.package.name/raw/notification");
于 2012-08-13T07:42:10.100 に答える
1

これらの2行を変更してみてください。

//In your activity:
AlarmIntent.putExtra("Ringtone",getResources().getResourceName(R.raw.shankh_final_mid));
....   
//In your reciever
notification.sound = (Uri)(Uri.parse(intent.getStringExtra("Ringtone")));
于 2012-08-13T07:50:35.640 に答える
0

次のコードを試して、独自の通知音を鳴らしてください。

NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(this, YourActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT);
Notification note  = new Notification(R.drawable.icon, "Your Text to Show", System.currentTimeMillis());
note.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");
int nid = 123456789;
manager.notify(nid, note);
于 2012-08-13T07:37:07.120 に答える
0

この答えは少し時代遅れでした。

アラームチャンネルで再生するには、ここで自分の質問を開く必要がありました。

これが私の実用的な解決策です。

mediaPlayerScan = new MediaPlayer();
try {
  mediaPlayerScan.setDataSource(getContext(),
          Uri.parse(getString(R.string.res_path) + R.raw.scan_beep));

  if (Build.VERSION.SDK_INT >= 21) {
    mediaPlayerScan.setAudioAttributes(new AudioAttributes.Builder()
            .setUsage(AudioAttributes.USAGE_ALARM)
            .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
            .build());
  } else {
    mediaPlayerScan.setAudioStreamType(AudioManager.STREAM_ALARM);
  }
  mediaPlayerScan.prepare();
} catch (IOException e) {
  e.printStackTrace();
}
于 2018-06-15T20:00:50.293 に答える