无法在android中加载视频流
Posted
技术标签:
【中文标题】无法在android中加载视频流【英文标题】:failed to load video stream in android 【发布时间】:2017-09-12 11:20:16 【问题描述】:嘿,伙计们,我正在开发一个电视台直播的 android 应用程序,现在我有了流媒体引擎的 url,看起来 http://197.243.19.131:1935/rtv/rtv/playlist.m3u8
任何人都可以帮助我了解如何在 android 中从该 url 播放视频请帮助我在这里尝试编写一些代码:
String stream_url="http://197.243.19.131:1935/rtv/rtv/playlist.m3u8";
View rootView = inflater.inflate(R.layout.fragment_two, container, false);
videoView=(VideoView)rootView.findViewById(R.id.videoStream);
Uri uri= Uri.parse(stream_url);
videoView.setVideoURI(uri);
videoView.start();
【问题讨论】:
此流在外部应用程序上是否适合您?浏览器? VLC 播放器?另外,您正在测试哪种设备?并非所有 Android 操作系统和设备都完全支持 HLS。 是的,这个流在 vlc 网络流上工作 【参考方案1】:尽管您已经声明了要渲染视频的视图,但您还没有处理过媒体,但您需要更深入地研究媒体和流媒体。 您需要使用 Android 提供的 Media Player Api 来流式传输这个 m3u8。按照此代码播放 Live 内容非常简单。
public class PlayerActivity extends AppCompatActivity implements
SurfaceHolder.Callback, MediaPlayer.OnPreparedListener
private String Downloadurl;
private int playminute;
private SurfaceView mSurfaceView;
private MediaPlayer mMediaPlayer;
private SurfaceHolder mSurfaceHolder;
ProgressBar p_bar;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.player);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
View decorView = getWindow().getDecorView();
// Hide both the navigation bar and the status bar.
// SYSTEM_UI_FLAG_FULLSCREEN is only available on Android 4.1 and higher, but as
// a general rule, you should design your app to hide the status bar whenever you
// hide the navigation bar.
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
// thumburl = getIntent().getStringExtra("thumburl");
// p_bar=(ProgressBar)findViewById(R.id.p_bar);
// entryid = getIntent().getStringExtra("entryid");
// playminute = getIntent().getIntExtra("playminute",0);
//fragmenttype = getIntent().getStringExtra("fragmenttype");
// Downloadurl = getIntent().getStringExtra("Downloadurl");
Downloadurl = "http://197.243.19.131:1935/rtv/rtv/playlist.m3u8";
// Description = getIntent().getStringExtra("Description");
Log.e("kanish","Download Url inside player:"+Downloadurl);
mSurfaceView = (SurfaceView) findViewById(R.id.surface_view);
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(PlayerActivity.this);
@Override
public void surfaceCreated(SurfaceHolder holder)
p_bar.setVisibility(View.VISIBLE);
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDisplay(mSurfaceHolder);
try
mMediaPlayer.setDataSource(Downloadurl);
mMediaPlayer.prepare();
mMediaPlayer.setOnPreparedListener(PlayerActivity.this);
//mMediaPlayer.setAudiostreamType(AudioManager.STREAM_MUSIC);
catch (IOException e)
e.printStackTrace();
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
@Override
public void surfaceDestroyed(SurfaceHolder holder)
@Override
public void onBackPressed()
super.onBackPressed();
@Override
public void onPrepared(MediaPlayer mp)
p_bar.setVisibility(View.GONE);
mMediaPlayer.start();
@Override
protected void onPause()
super.onPause();
releaseMediaPlayer();
@Override
protected void onDestroy()
super.onDestroy();
releaseMediaPlayer();
private void releaseMediaPlayer()
if (mMediaPlayer != null)
mMediaPlayer.release();
mMediaPlayer = null;
如果有帮助,请告诉我。
在电视模拟器上测试过,流媒体很好:
【讨论】:
如果有帮助,您可以选择正确答案。它将帮助其他人解决这个问题以找到可行的解决方案 在日志中播放失败说 ava.io.IOException: Prepare failed.: status=0x64 09-15 05:03:06.397 22633-22633/com.dukire.dukireapp W/System .err:在 android.media.MediaPlayer._prepare(Native Method)【参考方案2】:你错过了OnPreparedListener
。
videoview.setOnPreparedListener(new OnPreparedListener()
@Override
public void onPrepared(MediaPlayer mp)
videoview.start();
);
关注这个tutorial。
【讨论】:
以上是关于无法在android中加载视频流的主要内容,如果未能解决你的问题,请参考以下文章
无法在 Fancybox 中加载 YouTube 视频 - 空白页
在 WebView 中加载 YouTube 视频时,全屏选项不可用