how to play audio file from url in android

前端 未结 4 1839
有刺的猬
有刺的猬 2021-01-03 04:26

I need to play audio file from remote server in my app. I could play when I tested with localhost server (using WAMP). When the same file supplied from the server it is not

4条回答
  •  时光说笑
    2021-01-03 05:04

    The answer provided above provides synchronous fetching and playing, meaning currently executing thread will be blocked until prepare() is completed.

    prepareAsync() can be used instead to "prepare" the stream asynchronously. You also will need to handle onPrepared() event to start playing.

    mediaPlayer.setDataSource(URL here);
    mediaPlayer.prepareAsync();
    

    Add OnPrepared event handler:

    mPlayer.setOnPreparedListener(new OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            mPlayer.start();
        }
    });
    

    Still, apparently there is no way to configure streaming buffer size. Frustrating...

提交回复
热议问题