【问题标题】:How to play stream(dash) video in media player如何在媒体播放器中播放流(破折号)视频
【发布时间】:2017-10-27 09:30:43
【问题描述】:

问题是视频根本无法播放,但是当我使用http://www.youtubemaza.com/files/data/366/Tom%20And%20Jerry%20055%20Casanova%20Cat%20(1951).mp4(示例)时,视频播放器运行良好。

我正在使用 json 来获取 url https://api.vid.me/videos/featured?limit=1

(当前网址https://api.vid.me/video/18175470/stream?format=dash

public class MediaPlayerActivity extends AppCompatActivity {


private VideoView videoView;
private int position = 0;
private MediaController mediaController;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_video_player);

    videoView = (VideoView) findViewById(R.id.videoView);
    videoView.setVisibility(View.VISIBLE);

    // Set the media controller buttons
    if (mediaController == null) {
        mediaController = new MediaController(MediaPlayerActivity.this);

        // Set the videoView that acts as the anchor for the MediaController.
        mediaController.setAnchorView(videoView);


        // Set MediaController for VideoView
        videoView.setMediaController(mediaController);

    }


    try {

        String videoUrl="https://api.vid.me/video/18175470/stream?format=dash"; // dash

        Uri video = Uri.parse(videoUrl);
        videoView.setVideoURI(video);

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

    videoView.requestFocus();


    // When the video file ready for playback.
    videoView.setOnPreparedListener(new OnPreparedListener() {

        public void onPrepared(MediaPlayer mediaPlayer) {


            videoView.seekTo(position);
            if (position == 0) {
                videoView.start();
            }

            // When video Screen change size.
            mediaPlayer.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() {
                @Override
                public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {

                    // Re-Set the videoView that acts as the anchor for the MediaController
                    mediaController.setAnchorView(videoView);
                }
            });
        }
    });

}
// When you change direction of phone, this method will be called.
// It store the state of video (Current position)
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);

    // Store current position.
    savedInstanceState.putInt("CurrentPosition", videoView.getCurrentPosition());
    videoView.pause();
}


// After rotating the phone. This method is called.
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);

    // Get saved position.
    position = savedInstanceState.getInt("CurrentPosition");
    videoView.seekTo(position);
}
}

xml文件

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:layout_height="match_parent">

    <!--<com.google.android.exoplayer2.ui.SimpleExoPlayerView
        android:id="@+id/videoView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>-->
    <SurfaceView
        android:id="@+id/surface_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone"/>
    <VideoView
        android:id="@+id/videoView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone"/>

</FrameLayout>

【问题讨论】:

    标签: android video-streaming android-mediaplayer android-videoview


    【解决方案1】:

    我发现的唯一方法是使用第三方库(但是这个是由 Google 完成的,所以不确定你会怎么称呼它……)

    http://google.github.io/ExoPlayer/

    它有很好的文档,甚至还有关于如何将它与 DASH 视频源一起使用的示例代码!

    【讨论】:

    • 您能否提供一些使用 DASH 从 URL 流式传输视频的 sn-p?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多