简体   繁体   English

Android - MediaPlayer即使在Android 4.0+上准备流之前就准备好了

[英]Android - MediaPlayer's on Prepare Called even before the stream is prepared on Android 4.0+

I am facing the issue that whenever a stream is played by my app on Android 4.0+ the OnPrepare method from MediaPlayer.OnPreparedListener is called even before a stream is loaded and thus i am unable to indicated the user that the stream downloading/buffering is in process. 我面临的问题是,每当我的应用程序在Android 4.0 +上播放流时, MediaPlayer.OnPreparedListenerOnPrepare方法甚至在加载流之前就被调用,因此我无法向用户表明流下载/缓冲是在处理。 I have already found a question of the same kind but not answered Here is a what i am doing. 我已经找到了同样的问题,但没有回答这是我正在做的事情。

   @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);  
        playVideo(someRtspUrl);
    }
    private void playVideo(String url) {
        // if app is running on Google TV then change RTSP link to HLS
        if (url.contains("rtsp")) {

            // split the RTSP URL and make it as HLS
            String videoUrlParts[] = url.split("\\?");
            url = videoUrlParts[0].replace("rtsp", "http") + "/playlist.m3u8";

            if (videoUrlParts.length > 1)
                url += "?" + videoUrlParts[1];
        }   mVideoView.setVideoURI(Uri.parse(url));
        mVideoView.requestFocus();
        mVideoView.setOnCompletionListener(this);
        mVideoView.setOnPreparedListener(this); 

    }



    @Override
        public void onPrepared(MediaPlayer player) {
            dismissProgressDialog();
            mVideoView.start();
        }

This Code is Working fine on Google TV and other Android 3.0+ and < 4.0+ device 此代码在Google TV和其他Android 3.0+以及<4.0+设备上正常运行

I don't have much experience with media player. 我对媒体播放器没有多少经验。 But couple of suggestions/queries from my side 但是我的一些建议/疑问

  1. No call to prepare. 没有准备的电话。 If you are doing it, did you try prepareAsync ? 如果您这样做,您是否尝试过prepareAsync?
  2. You are not using the mediaplayer instance which is passed to the onPrepared callback. 您没有使用传递给onPrepared回调的mediaplayer实例。 There can be a chance that you are trying to start the wrong mediaplayer. 您可能有机会尝试启动错误的媒体播放器。

I would suggest using the MediaPlayer class that is available. 我建议使用可用的MediaPlayer类。 Unlike the VideoView MediaPlayer is aware of its state and manages even more things for you that VideoView doesn't offer.. Paired with setting up a SurfaceHolder to display the content in it is pretty simple. VideoView不同, MediaPlayer知道它的状态,并为您管理更多的东西,而VideoView不提供。配置设置SurfaceHolder以显示其中的内容非常简单。

You'll just want to make sure that you are properly handling the state and using the prepareAsync() call of MediaPlayer. 您只需要确保正确处理状态并使用MediaPlayer的prepareAsync()调用。

This is a known issue for .m3u8 with Android < 4.3. 对于Android <4.3的.m3u8,这是一个已知问题。

They fixed this in 4.3. 他们在4.3修正了这个问题。 It will be worth to compare MediaPlayer or VideoView code of different platforms ie 4.3 & < 4.3 值得比较不同平台的MediaPlayer或VideoView代码,即4.3和<4.3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM