如何在媒体播放器中播放流(破折号)视频
Posted
技术标签:
【中文标题】如何在媒体播放器中播放流(破折号)视频【英文标题】: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_
android:layout_>
<!--<com.google.android.exoplayer2.ui.SimpleExoPlayerView
android:id="@+id/videoView"
android:layout_
android:layout_/>-->
<SurfaceView
android:id="@+id/surface_view"
android:layout_
android:layout_
android:visibility="gone"/>
<VideoView
android:id="@+id/videoView"
android:layout_
android:layout_
android:visibility="gone"/>
</FrameLayout>
【问题讨论】:
【参考方案1】:我发现的唯一方法是使用第三方库(但是这个是由 Google 完成的,所以不确定你会怎么称呼它……)
http://google.github.io/ExoPlayer/
它有很好的文档,甚至还有关于如何将它与 DASH 视频源一起使用的示例代码!
【讨论】:
您能否提供一些使用 DASH 从 URL 流式传输视频的 sn-p?以上是关于如何在媒体播放器中播放流(破折号)视频的主要内容,如果未能解决你的问题,请参考以下文章
HTML5 视频播放器显示以在浏览器中启用 Flash。如何在不启用 Flash 的情况下播放流视频?
Android:如何将 rtmp 流地址发送到外部视频播放器(例如 MX 播放器)