MediaPlayer prepareAsync()方法抛出IllegalStateException

时间:2016-03-05 10:19:09

标签: java android

我尝试使用MediaPlayer从网络服务器流式传输音乐,它看起来像这样:

try {
                MediaPlayer player = new MediaPlayer();
                player.setAudioStreamType(AudioManager.STREAM_MUSIC);
                player.setDataSource(audioUrl);
                player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        mp.start();
                    }
                });
                player.prepareAsync();

            } catch (Exception e) {
                Log.d("PLAY", "can't play it");
                // TODO: handle exception
            }

Unfortinatelly prepareAsync()在调试时总是抛出IllegalStateException,所以我永远不会使用onPrepared()方法。我尝试使用prepare()而不是prepareAsync(),但它会抛出IOException。我查了一下这个问题,但我能找到的一切都是补充 android.permission.INTERNET到清单或从start()(我已经完成)调用setOnPrepareListener

最奇怪的是,我的应用程序在运行时正常运行,尽管我在调试期间从不调用start()方法。有谁知道这意味着什么?

编辑1: 这是我在调试级别上使用logcat的原因(我希望你的意思是这样):

D/OpenGLRenderer: Enabling debug mode 0
D/dalvikvm: GC_FOR_ALLOC freed 2292K, 18% free 11063K/13392K, paused 14ms, total 14ms
E/MediaPlayer: prepareAsync called in state 1
D/dalvikvm:   JDWP invocation returning with exceptObj=0x43d24770 (Ljava/lang/IllegalStateException;)
E/MediaPlayer: prepareAsync called in state 1
D/dalvikvm:   JDWP invocation returning with exceptObj=0x43af0d50 (Ljava/lang/IllegalStateException;)
E/MediaPlayer: prepareAsync called in state 4
03-05 21:03:20.316 5459-5459/com.example.vkmusic D/dalvikvm:   JDWP invocation returning with exceptObj=0x43dee6c0 (Ljava/lang/IllegalStateException;)
E/MediaPlayer: prepareAsync called in state 8

编辑2:好的,事实证明只有在我将一个方法放入Watches中时才会在调试期间抛出异常,否则它会正常工作。例如,如果我将player.setDataSource(audioUrl)放在那里,当它到达代码中的同一行时会抛出异常。完全相同的事情发生在player.prepareAsync(),现在我完全糊涂了。为什么会这样?

0 个答案:

没有答案