AudioManager发送消息到处理程序上一个死线程?线程、发送消息、程序、AudioManager

2023-09-07 23:34:40 作者:今天QQ乐园小编为盆友们带来不一样的昵称,与大自然有关的清新

我想以编程方式提高音量到 STREAM_MUSIC 流的最大值,但我有一个的上一个死线程发送消息给一个处理程序 的,当我做的问题。此外,现在看来似乎不提高音量,100%的时候,虽然当我得到这个错误,它会提高它的大部分时间。

I am trying to programmatically raise the volume to the STREAM_MUSIC stream's maximum value, but I am having a "Sending message to a Handler on a dead thread" issue when I do. Also, it seems like it does not raise the volume 100% of the time, although when I get this error, it will raise it MOST of the time.

在code是:

System.out.println("Maximum volume for this stream is: "+maxstreamvol+" and it used to be set to: "+currentvol);     
final AudioManager am = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
am.setStreamVolume(AudioManager.STREAM_MUSIC, maxstreamvol, AudioManager.FLAG_SHOW_UI);
am.setStreamSolo(AudioManager.STREAM_MUSIC, true);
System.out.println("Volume Raised!"); 

围绕谷歌搜索后,似乎这个错误有...此时的code应该在UI线程上运行多线程的情况下做的。

After googling around, it seems that this error has to do with multi-threaded situations... The code at this point should be running on the UI thread.

事实上,我甚至用它包围着:

In fact, I even surrounded it with:

runOnUiThread(new Runnable() {

    public void run() {
        // code_goes_here
    }
});

和,所生产的相同的错误。 ,我看到的错误是这样的:

And, that produced the same error. The error that I am seeing is this:

I/System.out(24949): Maximum volume for this stream is: 15 and it used to be set to: 0
W/MessageQueue(  490): Handler (android.media.AudioManager$FocusEventHandlerDelegate$1) {42b52f28} sending message to a Handler on a dead thread
W/MessageQueue(  490): java.lang.RuntimeException: Handler (android.media.AudioManager$FocusEventHandlerDelegate$1) {42b52f28} sending message to a Handler on a dead thread
W/MessageQueue(  490):  at android.os.MessageQueue.enqueueMessage(MessageQueue.java:294)
W/MessageQueue(  490):  at android.os.Handler.enqueueMessage(Handler.java:618)
W/MessageQueue(  490):  at android.os.Handler.sendMessageAtTime(Handler.java:587)
W/MessageQueue(  490):  at android.os.Handler.sendMessageDelayed(Handler.java:558)
W/MessageQueue(  490):  at android.os.Handler.sendMessage(Handler.java:495)
W/MessageQueue(  490):  at android.media.AudioManager$1.dispatchAudioFocusChange(AudioManager.java:1894)
W/MessageQueue(  490):  at android.media.IAudioFocusDispatcher$Stub.onTransact(IAudioFocusDispatcher.java:57)
W/MessageQueue(  490):  at android.os.Binder.execTransact(Binder.java:351)
W/MessageQueue(  490):  at dalvik.system.NativeStart.run(Native Method)
I/System.out(24949): Volume Raised!

有谁知道这里发生了什么?

Does anybody know what's happening here?

推荐答案

您的问题可能与一个报道,这个线程 - onPostExecute没有被称为AsyncTask的(处理程序运行时异常)

Your problem may be related to the one reported on this thread - onPostExecute not being called in AsyncTask (Handler runtime exception)

对于音频焦点更改显示的触发一个UI事件,Android是无法完成。为什么是这样的情况下,它是目前尚不清楚。也许用一个上下文并显示在另一个上下文中执行音量敬酒获得的音频管理器?一个快速和肮脏的变化是交换的标志FLAG_VIBRATE,并检查是否有差别。这将缩小问题到UI更新,你可以再努力。

The display for the audio focus change is triggering a UI event that android is not able to complete. It is not immediately clear why this is the case. Perhaps the audio manager was obtained with one context and the toast to show the volume is executed in another context ? A quick and dirty change is to swap the flag for FLAG_VIBRATE and check if that makes a difference. That would narrow down the problem to a UI update which you can then work on.