Android:音频流延迟期间的prepareAsync()

时间:2017-07-23 21:55:55

标签: android android-mediaplayer audio-streaming

我正在尝试播放此网址中的音频:http://mtl2.liveatc.net/lbbg 这是我的代码:

public void setConnection(String url) {
    String url = http://mtl2.liveatc.net/lbbg;
    mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    new Player().execute(url);
}

private class Player extends AsyncTask<String, Void, Void> {
    @Override
    protected Void doInBackground(String... strings) {

        try {
            mediaPlayer.setDataSource(strings[0]);
            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mediaPlayer) {
                    mediaPlayer.stop();
                    mediaPlayer.reset();
                }
            });

            mediaPlayer.prepareAsync();

        } catch (Exception e) {
            Log.e("MyAudioStreamingApp", e.getMessage());
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener(){

            @Override
            public void onPrepared(MediaPlayer mp) {
                progressBar.setVisibility(View.INVISIBLE);
                mp.start();
            }
        });

    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progressBar.setVisibility(View.VISIBLE);
    }
}

一切正常,但真的有很大的延迟。如果我在浏览器中打开此URL,则需要将近20秒才能开始流式传输,但是当我在应用程序中运行它时,最多需要5-7分钟。

UPD还添加我的日志:

07-24 01:12:52.820 25918-25961/com.example I/OpenGLRenderer: Initialized EGL, version 1.4
07-24 01:12:52.843 25918-25961/com.example E/HAL: hw_get_module_by_class: module name gralloc
07-24 01:12:52.843 25918-25961/com.example E/HAL: hw_get_module_by_class: module name gralloc
07-24 01:12:59.171 25918-25918/com.example E/ExtMediaPlayer-JNI: env->IsInstanceOf fails
07-24 01:12:59.171 25918-25918/com.example E/MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0
07-24 01:12:59.171 25918-25918/com.example E/ExtMediaPlayer-JNI: env->IsInstanceOf fails
07-24 01:12:59.171 25918-25918/com.example E/MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0
07-24 01:12:59.188 25918-25931/com.example D/MediaHTTPConnection: filterOutInternalHeaders: key=User-Agent, val= stagefright/1.2 (Linux;Android 6.0.1)
07-24 01:12:59.191 25918-26002/com.example D/MediaHTTPConnection: proxy null port 0
07-24 01:12:59.192 25918-26002/com.example I/DpmTcmClient: RegisterTcmMonitor from: com.android.okhttp.TcmIdleTimerMonitor
07-24 01:15:42.557 25918-25925/com.example W/art: Suspending all threads took: 31.418ms
07-24 01:17:48.107 25918-25918/com.example D/MediaPlayer: setSubtitleAnchor in MediaPlayer

UPD2:我的“解决方案”:我找不到解决这个问题的方法所以我决定使用下一个东西:我放置了小型WebView,我需要播放按钮,并收到完整的音频播放器加载并在10-15秒内播放此流。

2 个答案:

答案 0 :(得分:0)

不使用任务,您可以简单地完成:

MediaPlayer mediaPlayer = new MediaPlayer();

mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
    public boolean onError(MediaPlayer mp, int what, int extra) {
        mp.reset();
        return false;
    }
});

mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
    public void onPrepared(MediaPlayer mp) {
        mp.start();
    }
});

try {
    mediaPlayer.setDataSource("http://yoururl");
    mediaPlayer.prepareAsync();
} catch (IllegalArgumentException e) {
} catch (IllegalStateException e) {
} catch (IOException e) {
}

尝试这种简单的方法并检查延迟是否仍然存在。

答案 1 :(得分:0)

检查您的线程优先级。

Thread.currentThread().getPriority();

根据我的经验,一些旧的(Android 5~6)设备(例如:LG ..)在准备或启动工作线程(如AsyncTask)时会出现这样的问题。在这种情况下,请将priority设置为更高或使用Main thread。就我而言,将prepareAsync移动到主线程。

<强>已更新

我测试了代码,发现优先级不是问题。我认为Android mediaPlayer不适合&#34;&#34;网址。我曾经选择ffmpeg或google / exoplayer进行流媒体项目。

响应标题

HTTP/1.1 200 OK
Server: nginx/1.12.1
Content-Type: audio/mpeg
Connection: close
icy-br: 16
ice-audio-info: ice-samplerate=11025;ice-bitrate=16;ice-channels=1
icy-br: 16
icy-description: LBBG Burgas, Bulgaria
icy-genre: ATC
icy-name: Burgas Tower, Approach
icy-private: 0
icy-pub: 1
icy-url: http://www.liveatc.net
...

媒体资讯

Input #0, mp3, from 'http://mtl2.liveatc.net/lbbg':
Duration: N/A, start: 0.000000, bitrate: 16 kb/s
Stream #0.0: Audio: mp3, 11025 Hz, mono, s16, 16 kb/s