如何在runnable线程中创建Media Player

时间:2015-02-21 22:47:10

标签: android media-player runnable

我想在Runnable Thread中创建一个媒体播放器,但在创建时我收到此警告消息“应该有字幕控制器已设置错误(-19,0)”。我实际上不知道这个警告信息是什么。

我发布了我的代码,请仔细阅读并提出一些解决方案。

    private void beginListenForData() {

    final Handler handler = new Handler(); 
    final byte delimiter = 10;  

    stopWorker = false;
    readBufferPosition = 0;
    readBuffer = new byte[1024];
    workerThread = new Thread(new Runnable()
    {
        public void run()
        {                
           while(!Thread.currentThread().isInterrupted() && !stopWorker)
           {
                try 
                {
                    int bytesAvailable = inStream.available();                        
                    if(bytesAvailable > 0)
                    {
                        byte[] packetBytes = new byte[bytesAvailable];
                        inStream.read(packetBytes);
                        for(int i=0;i<bytesAvailable;i++)
                        {
                            byte b = packetBytes[i];
                            if(b == delimiter)
                            {
                                byte[] encodedBytes = new byte[readBufferPosition];

                                System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);

                                final String data = new String(encodedBytes, "US-ASCII");
                                readBufferPosition = 0;

                                handler.post(new Runnable()
                                {
                                    public void run()
                                    {
                                        result.setText(data);

                                        if(data.contains("a")){
                                           //MediaPlayer
                                           mPlayer =   MediaPlayer.create(getApplicationContext(), R.raw.a);
                                           mPlayer.start();
                                        }else if(data.contains("b")){
                                            if (mPlayer != null) {
                                                mPlayer.stop();
                                                mPlayer.release();
                                                mPlayer = null;
                                           }
                                        }
                                    }
                                });
                            }
                            else
                            {
                                readBuffer[readBufferPosition++] = b;
                            }
                        }
                    }
                } 
                catch (IOException ex) 
                {
                    stopWorker = true;
                }
           }
        }
    });

    workerThread.start();
}

0 个答案:

没有答案