-2

我正在使用 Wowza 流媒体云,它使用网络上的 HLS 链接可以正常工作,但是 android 设备不能在网络视图上工作。你能告诉我它是如何工作的吗?

并且 rtsp 实时链接也无法在 android 设备上运行

公共类 AnotherOne 扩展 AppCompatActivity 实现 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.activity_another_one);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    View decorView = getWindow().getDecorView();

    int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_FULLSCREEN;
    decorView.setSystemUiVisibility(uiOptions);


    Downloadurl = "https://7c6ad8.entrypoint.cloud.wowza.com/app-04be/ngrp:f23a3900_all/playlist.m3u8";
    // Description =  getIntent().getStringExtra("Description");
    Log.e("kanish","Download Url inside player:"+Downloadurl);
    mSurfaceView = (SurfaceView) findViewById(R.id.surfaceView);
    mSurfaceHolder = mSurfaceView.getHolder();
    mSurfaceHolder.addCallback(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(AnotherOne.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;
    }
}

}

4

1 回答 1

0

您需要声明一个视图来呈现视频视图,并且您需要使用 Android 提供的 Media Player Api 来流式传输 m3u8。按照此代码播放实时内容非常简单。

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

    int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_FULLSCREEN;
    decorView.setSystemUiVisibility(uiOptions);


    Downloadurl = "YOUR M3U8 URL";
    // 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;
    }
  }

}

让我知道它是否有帮助。

于 2017-09-12T11:59:37.350 回答