问题是视频根本没有播放,但是当我使用http://www.youtubemaza.com/files/data/366/Tom%20And%20Jerry%20055%20Casanova%20Cat%20(1951).mp4时(例子),视频播放器运行良好。
我使用json获取网址 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>
答案 0 :(得分:0)
我发现的唯一方法是使用第三方库(但是这个是由谷歌完成的,所以不确定你真正称之为...)
http://google.github.io/ExoPlayer/
它有很好的文档,甚至是关于如何将它与DASH视频源一起使用的示例代码!