无法在Android中加载视频流

时间:2017-09-12 11:20:17

标签: android video-streaming

嘿伙计们我正在开发一个电视台直播Android应用程序现在我有流媒体的网址看起来 http://197.243.19.131:1935/rtv/rtv/playlist.m3u8

任何人都可以帮我找到如何从Android中的网址播放视频请帮助我试着写一些代码在这里:

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();

2 个答案:

答案 0 :(得分:1)

你需要更多地潜入媒体和流媒体,尽管你已经宣布视图渲染视频你尚未处理媒体。 您需要使用Android提供的Media Player Api来传输此m3u8.it非常简单,播放实时内容请遵循此代码。

  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;
    }
  }

}

如果有帮助,请告诉我。

在电视模拟器上测试,溪流嘛: Streaming on emulator

答案 1 :(得分:0)

您遗失OnPreparedListener

videoview.setOnPreparedListener(new OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer mp) {
        videoview.start();
    }
});

关注此tutorial