PageRenderTime 115ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/app/src/main/java/com/vbnhat/teststreamingapp/MainActivity.java

https://gitlab.com/vbnhat/seek-in-vod-stream
Java | 132 lines | 103 code | 28 blank | 1 comment | 2 complexity | 96aa21756941d10eaad7d64d2a2bb463 MD5 | raw file
  1. package com.vbnhat.teststreamingapp;
  2. import android.media.MediaPlayer;
  3. import android.os.Handler;
  4. import android.os.Message;
  5. import android.os.SystemClock;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.os.Bundle;
  8. import android.util.Log;
  9. import android.view.SurfaceHolder;
  10. import android.view.SurfaceView;
  11. import android.widget.SeekBar;
  12. import android.widget.Toast;
  13. import java.io.IOException;
  14. public class MainActivity extends AppCompatActivity {
  15. final String url = "http://qthttp.apple.com.edgesuite.net/1010qwoeiuryfg/0640_vod.m3u8";
  16. //http://123.30.185.126/hls/vtv1.m3u8
  17. private static final int MESSAGE_HANDLE_SEEK_BAR = 1;
  18. final MediaPlayer mPlayer = new MediaPlayer();
  19. private SurfaceView sfvStream = null;
  20. private SurfaceHolder sfHolder;
  21. private static final String TAG = MainActivity.class.getCanonicalName();
  22. private SeekBar seekBar;
  23. private Handler handler;
  24. @Override
  25. protected void onCreate(Bundle savedInstanceState) {
  26. super.onCreate(savedInstanceState);
  27. setContentView(R.layout.activity_main);
  28. seekBar = (SeekBar) findViewById(R.id.seekbar);
  29. sfvStream = (SurfaceView) findViewById(R.id.sfvStream);
  30. sfvStream.getHolder().addCallback(new SurfaceHolder.Callback() {
  31. @Override
  32. public void surfaceCreated(SurfaceHolder holder) {
  33. sfHolder = holder;
  34. mPlayer.setDisplay(sfHolder);
  35. }
  36. @Override
  37. public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
  38. }
  39. @Override
  40. public void surfaceDestroyed(SurfaceHolder holder) {
  41. }
  42. });
  43. try {
  44. mPlayer.setDataSource(url);
  45. mPlayer.prepare();
  46. } catch (IOException e) {
  47. e.printStackTrace();
  48. }
  49. mPlayer.start();
  50. mPlayer.setScreenOnWhilePlaying(true);
  51. handler = new Handler(){
  52. @Override
  53. public void handleMessage(Message msg) {
  54. super.handleMessage(msg);
  55. switch (msg.what) {
  56. case MESSAGE_HANDLE_SEEK_BAR:
  57. updateSeekbar();
  58. break;
  59. }
  60. }
  61. };
  62. initSeekbar();
  63. seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
  64. @Override
  65. public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
  66. if(fromUser){
  67. updateSeekbar(progress);
  68. mPlayer.seekTo(progress);
  69. Toast.makeText(MainActivity.this,"Seek to: "+progress/1000+ "/ "+ mPlayer.getDuration()/1000 +" (s)",Toast.LENGTH_SHORT).show();
  70. }
  71. }
  72. @Override
  73. public void onStartTrackingTouch(SeekBar seekBar) {
  74. }
  75. @Override
  76. public void onStopTrackingTouch(SeekBar seekBar) {
  77. }
  78. });
  79. }
  80. public void updateSeekbar(){
  81. seekBar.setMax(mPlayer.getDuration());
  82. handler.removeMessages(MESSAGE_HANDLE_SEEK_BAR);
  83. int currentPosition = seekBar.getProgress();
  84. seekBar.setProgress(currentPosition+1000);
  85. handler.sendEmptyMessageDelayed(MESSAGE_HANDLE_SEEK_BAR,1000);
  86. }
  87. public void updateSeekbar(int position){
  88. seekBar.setMax(mPlayer.getDuration());
  89. handler.removeMessages(MESSAGE_HANDLE_SEEK_BAR);
  90. seekBar.setProgress(position+1000);
  91. handler.sendEmptyMessageDelayed(MESSAGE_HANDLE_SEEK_BAR,1000);
  92. }
  93. public void initSeekbar(){
  94. seekBar.setProgress(0);
  95. seekBar.setMax(mPlayer.getDuration());
  96. Toast.makeText(MainActivity.this,"Length: "+mPlayer.getDuration(),Toast.LENGTH_SHORT).show();
  97. handler.sendEmptyMessageDelayed(MESSAGE_HANDLE_SEEK_BAR,1000);
  98. }
  99. @Override
  100. protected void onPause(){
  101. mPlayer.release();
  102. super.onPause();
  103. }
  104. }