Mediaplayer从互联网完美流出,但不是从localhost流出

时间:2014-09-02 09:22:10

标签: android android-mediaplayer

我有一台接收音频流的媒体播放器。当我使用互联网流时,一切都很完美。当我为测试目的切换到我的mamp localhost url时。它给了我错误。

这是我的代码:

  private void initMediaPlayer() {
        mPlayer = new MediaPlayer();
        mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        startMP3(URL);
    }

    private void startMP3(String URL) {
        if (intialStage) {
            new Player().execute(URL);
        }
    }

    class Player extends AsyncTask<String, Void, Boolean> {

        @Override
        protected Boolean doInBackground(String... params) {
            Boolean prepared;
            try {
                Log.d(TAG, "MP3 URL: " + params[0]);
                mPlayer.setDataSource(params[0]);
                mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        intialStage = true;
                        mPlayer.stop();
                        mPlayer.reset();
                        songPositionHandler.removeCallbacks(UpdateSongTime);
                    }
                });

                mPlayer.prepare();
                prepared = true;
            } catch (IllegalArgumentException e) {
                prepared = false;
                e.printStackTrace();
            } catch (SecurityException e) {
                prepared = false;
                e.printStackTrace();
            } catch (IllegalStateException e) {
                prepared = false;
                e.printStackTrace();
            } catch (IOException e) {
                prepared = false;
                e.printStackTrace();
            }
            return prepared;
        }

        @Override
        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);
            Log.d(TAG, "Prepared status:" + " " + result);
            mPlayer.start();
            intialStage = false;
            songPositionHandler = new Handler();
            songPositionHandler.postDelayed(UpdateSongTime, 1000);
        }
    }

    private Runnable UpdateSongTime = new Runnable() {
        public void run() {
            Log.d(TAG, "Current sec in song: " + TimeUnit.MILLISECONDS.toSeconds(mPlayer.getCurrentPosition()));
            songPositionHandler.postDelayed(this, 1000);
        }
    };

这是我的网址:

public static final String URL = "http://localhost:8888/test.mp3";

它给了我这个例外:

09-02 09:17:19.294    1835-1847/com.example.test E/MediaPlayer﹕ error (1, -2147483648)
09-02 09:17:19.294    1835-1848/com.example.test W/System.err﹕ java.io.IOException: Prepare failed.: status=0x1
09-02 09:17:19.294    1835-1848/com.example.test W/System.err﹕ at android.media.MediaPlayer.prepare(Native Method)
09-02 09:17:19.294    1835-1848/com.example.test W/System.err﹕ at com.example.test.AudioWaveForm$Player.doInBackground(AudioWaveForm.java:122)
09-02 09:17:19.294    1835-1848/com.example.test W/System.err﹕ at com.example.test.AudioWaveForm$Player.doInBackground(AudioWaveForm.java:104)
09-02 09:17:19.294    1835-1848/com.example.test W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288)
09-02 09:17:19.294    1835-1848/com.example.test W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
09-02 09:17:19.294    1835-1848/com.example.test W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
09-02 09:17:19.294    1835-1848/com.example.test W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
09-02 09:17:19.294    1835-1848/com.example.test W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
09-02 09:17:19.294    1835-1848/com.example.test W/System.err﹕ at java.lang.Thread.run(Thread.java:841)

有人可以解释为什么它与在线网址合作而不是我的mamp?

1 个答案:

答案 0 :(得分:2)

localhost 是您的Android主机。如果您正在运行模拟器:How to connect to my http://localhost web server from Android Emulator in Eclipse或者如果您使用的是Android设备,请指定您的电脑的IP,请参考此项。