同様の問題がありました。着信音が再生されると、停止するまで無限に繰り返されますが、通知音が再生されると、1 回だけ再生されることが判明しました。したがって、あなたの場合の違いは、着信音または通知音のどちらが で選択されたかにあると思いますsomeFunctionToLookupAValidNotificationRingtoneUri()
。のコードが提供されていないためsomeFunctionToLookupAValidNotificationRingtoneUri()
、そこで何が起こっているのかわかりません。
通知音の選択
ユーザーが通知音を選択するために着信音ピッカーを使用する場合、このコードは、着信音ではなく通知音を選択するインテントを開始します。
private void PickANotificationSound() {
Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
// We want a notification sound picked. If we don't add this to the
// intent, a ringtone is picked; this means that when it is played,
// it will keep on playing until it is explicitly stopped. A
// notification sound, however, plays only once.
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE,
RingtoneManager.TYPE_NOTIFICATION);
// Start the intent to pick a notification sound. The result will show
// up later when onActivityResult() is called.
startActivityForResult(intent, REQUESTCODE_NOTIFICATION_SOUND);
}
whereREQUESTCODE_NOTIFICATION_SOUND
は、リクエストを識別する、任意の名前と値を持つ単なるローカル定数です。
private static final int REQUESTCODE_NOTIFICATION_SOUND = 1;
次のonActivityResult()
ようなコールバック関数は、通知サウンド URI を取得して再生します。
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == REQUESTCODE_NOTIFICATION_SOUND) {
try {
if (resultCode == RESULT_OK) {
Uri ringtoneUri = data.getParcelableExtra(
RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
if (ringtoneUri != null) {
PlayRingtoneOrNotificationSoundFromUri(ringtoneUri);
}
}
} catch (Exception e) {
e.printStackTrace();
}
} else
super.onActivityResult(requestCode, resultCode, data);
}
private void PlayRingtoneOrNotificationSoundFromUri(Uri ringtoneUri) {
Ringtone ringtone = RingtoneManager.getRingtone(
getApplicationContext(), ringtoneUri);
if (ringtone != null) {
ringtone.play();
}
}
通知音を選択したいという意図で言ったので、結果の音は通知音であり、したがって の呼び出し後に一度だけ再生されringtone.play()
ます。
着信音を選択したいという意図で言った場合、次のようになります。
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE,
RingtoneManager.TYPE_RINGTONE);
ピッカーは着信音を返します。この着信音は、ringtone.play()
呼び出しの後、停止するringtone.stop()
か、アプリケーションが強制終了されるまで、無期限に再生されます。
「着信音」の2つの意味
「着信音」という言葉は 2 つの異なる意味で使用されるため、Android API の用語が混乱を招くことに注意してください ( RingtoneManager のドキュメントを参照)。
電話が鳴ったときに繰り返し再生される音、通知音、または類似の音など、ユーザーの注意を引くための音。この意味が名前に使われていますRingtoneManager
。
通知音や類似の音とは対照的に、電話が鳴ったときに繰り返し再生される音。この意味は、 の名前TYPE_RINGTONE
に使用されていRingtoneManager.TYPE_RINGTONE
ます。