铃声一遍又一遍地播放(无限循环播放)

时间:2013-01-20 23:52:00

标签: android android-mediaplayer ringtone

我有一个Android应用,当某些事件发送到BroadcastReceiver时会播放通知铃声(RingtoneManager.TYPE_NOTIFICATION)。

播放铃声的代码基本上是这样的:

    onReceive(Context context, Intent intent)
    {
        ...
        Uri ringtoneUri = someFunctionToLookupAValidNotificationRingtoneUri();
        ...
        Ringtone tone = RingtoneManager.getRingtone(context, uri);
        Log.v(TAG, "About to play ringtone");
        tone.play();
    }

当这段代码运行时,铃声开始无限次地重复播放。有时它会在大量事件紧密聚集在一起时发生,但是当只有一个事件进入时也会发生。日志消息(和调试)验证每个事件只发生一次tone.play()调用,并且没有无限的事件流。

阻止无限循环的唯一方法就是杀死我的应用程序。

几乎就像每隔一段时间一样,Android会忘记刷新声音输出缓冲区,因此它会通过播放内部的任何内容来保持循环。

有关如何调试和/或解决此问题的任何想法?

1 个答案:

答案 0 :(得分:4)

我有类似的问题。事实证明,当播放铃声时,它会无限重复直到停止,而当播放通知声音时,它只会播放一次。所以我的猜测是你的情况不同在于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);
    }

其中REQUESTCODE_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()停止或应用程序被杀死。

'铃声'的两个含义

请注意,Android API中的术语会增加混乱,因为“铃声”一词使用了两种不同的含义(cf. the documentation of RingtoneManager):

  1. 任何声音都会引起用户的注意,例如手机响铃时反复播放的声音,通知声音或类似的声音。这个含义用在名称RingtoneManager

  2. 电话响铃时反复播放的声音,反对发出通知声音或类似声音。这个含义在TYPE_RINGTONE中的RingtoneManager.TYPE_RINGTONE名称中使用。

相关问题