铃声一遍又一遍地播放(无限循环) [英] Ringtone plays over and over again (looping infinitely)

查看:96
本文介绍了铃声一遍又一遍地播放(无限循环)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

播放铃声的代码基本做到了:

 onReceive(上下文上下文,意图意图){...Uri ringtoneUri = someFunctionToLookupAValidNotificationRingtoneUri();...铃声 = RingtoneManager.getRingtone(context, uri);Log.v(TAG, "即将播放铃声");音调播放();}

每隔一段时间运行此代码时,铃声就会无限地一遍又一遍地播放.有时当大量事件聚集在一起时会发生这种情况,但当只有一个事件进入时也会发生这种情况.日志消息(和调试)验证 tone.play() 调用是每个事件只发生一次,并且没有无限的事件流.

停止无限循环的唯一方法是杀死我的应用程序.

Android 几乎每隔一段时间就会忘记刷新声音输出缓冲区,因此它不断循环播放里面的任何内容.

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

解决方案

我遇到了类似的问题.原来,当一个铃声被播放时,它会无限地重复直到停止,而当一个通知声音被播放时,它只会播放一次.所以我的猜测是,你的情况的不同在于在 someFunctionToLookupAValidNotificationRingtoneUri() 中选择了铃声还是通知声音.由于您没有提供 someFunctionToLookupAValidNotificationRingtoneUri() 的代码,我不知道那里会发生什么.

选择通知声音

如果您使用铃声选择器让用户选择通知声音,此代码将启动选择通知声音而不是铃声的意图:

 private void PickANotificationSound() {意图意图 = 新意图(RingtoneManager.ACTION_RINGTONE_PICKER);//我们想要选择一个通知声音.如果我们不将此添加到//意图,选择铃声;这意味着当它播放时,//它将继续播放,直到它被明确停止.一种//然而,通知声音只播放一次.intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE,铃声管理器.TYPE_NOTIFICATION);//开始选择通知声音的意图.结果会显示//稍后在 onActivityResult() 被调用时启动.startActivityForResult(意图,REQUESTCODE_NOTIFICATION_SOUND);}

其中 REQUESTCODE_NOTIFICATION_SOUND 只是一个具有任何名称和值的本地常量,用于标识请求:

 private static final int REQUESTCODE_NOTIFICATION_SOUND = 1;

像这样的 onActivityResult() 回调函数将获取通知声音 URI 并播放它:

 @Overrideprotected void onActivityResult(int requestCode, int resultCode,意图数据){如果(请求代码 == REQUESTCODE_NOTIFICATION_SOUND){尝试 {如果(resultCode == RESULT_OK){Uri ringtoneUri = data.getParcelableExtra(铃声管理器.EXTRA_RINGTONE_PICKED_URI);if (ringtoneUri != null) {PlayRingtoneOrNotificationSoundFromUri(ringtoneUri);}}} 捕获(异常 e){e.printStackTrace();}} 别的super.onActivityResult(requestCode, resultCode, data);}private void PlayRingtoneOrNotificationSoundFromUri(Uri ringtoneUri) {铃声铃声 = RingtoneManager.getRingtone(getApplicationContext(), ringtoneUri);如果(铃声!= null){铃声.播放();}}

因为我们在意图中说我们想要选择一个通知声音,所以产生的声音是一个通知声音,因此只在调用 ringtone.play() 后播放一次.>

如果我们在意图中说我们要选择铃声,如下所示:

 intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE,铃声管理器.TYPE_RINGTONE);

选择器将返回一个铃声,在ringtone.play() 调用后将无限期播放——直到被ringtone.stop() 停止或应用程序被终止.

'铃声'的两种含义

请注意,Android API 中的术语增加了混淆,因为铃声"一词有两种不同的含义(参见 RingtoneManager 的文档):

  1. 任何旨在引起用户注意的声音,例如电话铃响时重复播放的声音、通知声音或类似声音.这个含义用于名称RingtoneManager.

  2. 当电话响起时重复播放的声音,相对于通知声音或类似声音.这个含义用在RingtoneManager.TYPE_RINGTONE中的TYPE_RINGTONE名称中.

I have an Android app that plays a notification ringtone (RingtoneManager.TYPE_NOTIFICATION) when certain events are sent to a BroadcastReceiver.

The code that plays the ringtone basically does:

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

Every so often when this code is run, the ringtone starts playing over and over again infinitely. Sometimes it happens when a large number of events are bunched close together, but it has also happened when only one event came in. The log message (and debugging) verifies that the tone.play() call is happening only once per event, and there isn't an infinite stream of events.

The only way stop the infinite looping is to kill my app.

It's almost as if every so often, Android forgets to flush the sound output buffer and so it keeps looping through playing whatever is inside.

Any ideas how to debug and/or fix this issue?

解决方案

I had a similar problem. It turned out that when a ringtone is played, it will repeat indefinitely until stopped, whereas when a notification sound is played, it will play only once. So my guess is that the difference in your case lies in whether a ringtone or a notification sound was selected in someFunctionToLookupAValidNotificationRingtoneUri(). As you do not supply the code for someFunctionToLookupAValidNotificationRingtoneUri(), I cannot know what happens there.

Picking a notification sound

If you use a ringtone picker for the user to select a notification sound, this code will start the intent to pick a notification sound as opposed to a ringtone:

    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);
    }

where REQUESTCODE_NOTIFICATION_SOUND is just a local constant with any name and value, identifying the request:

    private static final int REQUESTCODE_NOTIFICATION_SOUND = 1;

An onActivityResult() callback function like this will then pick up the notification sound URI and play it:

    @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();
        }
    }

Because we said in the intent that we wanted to pick a notification sound, the resulting sound is a notification sound and is therefore only played once after the call to ringtone.play().

If we had said in the intent that we wanted to pick a ringtone, like this:

    intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE,
        RingtoneManager.TYPE_RINGTONE);

the picker would return a ringtone that would play indefinitely after the ringtone.play() call – until stopped by ringtone.stop() or the application was killed.

Two meanings of 'ringtone'

Note that the terminology in the Android API adds to the confusion, as the word "ringtone" is used with two different meanings (cf. the documentation of RingtoneManager):

  1. Any sound meant to catch the user's attention, such as a sound to play repeatedly when the phone rings, a notification sound, or a similar sound. This meaning is used in the name RingtoneManager.

  2. A sound to play repeatedly when the phone rings, as opposed to a notification sound or a similar sound. This meaning is used in the name TYPE_RINGTONE in RingtoneManager.TYPE_RINGTONE.

这篇关于铃声一遍又一遍地播放(无限循环)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆