以前作過一些關於android播放器的開發工做,當時懵懂無知,只知道一個ffmpeg,而後就費勁巴拉的用ndk把ffmpeg編譯了,而後又本身封裝了個庫調用,那個過程能夠看看這篇文章,而後我這裏也有一個編譯好的庫,能夠拿來用。html
好了,歷史就說到這裏,下面開始說一些今天的正題了,MediaPlayer類。android
MediaPlayer不單單可以播放本地文件,還可以播放一些網絡流。git
不過,對於網絡流的支持仍是比較有限的,遠不如ffmpeg的支持好。
HTTP/HTTPS live streaming draft protocol:github
Note: HTTPS is not supported before Android 3.1.
見官方給出的附錄api
設置權限網絡
<uses-permission android:name="android.permission.INTERNET" /> 網絡權限
佈局文件app
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.hankun.myapplication.MainActivity"> <SurfaceView android:id="@+id/big_screen" android:layout_width="200dp" android:layout_height="200dp" android:layout_marginTop="8dp" app:layout_constraintTop_toBottomOf="@+id/btn_play" android:layout_marginLeft="8dp" app:layout_constraintLeft_toLeftOf="@+id/btn_play" /> </android.support.constraint.ConstraintLayout>
代碼部分ide
public class MainActivity extends AppCompatActivity { private String stream_url = "http://127.0.0.1:8080/live/livestream.m3u8"; private SurfaceHolder sh; private SurfaceView sfv; private MediaPlayer player; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sfv = (SurfaceView)findViewById(R.id.big_screen); player = new MediaPlayer(); try { player.setDataSource(this, Uri.parse(stream_url)); sh =sfv.getHolder(); sh.addCallback(new MyCallBack()); player.prepare(); player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { player.start(); player.setLooping(true); } }); } catch (Exception e) { e.printStackTrace(); } } private class MyCallBack implements SurfaceHolder.Callback { @Override public void surfaceCreated(SurfaceHolder holder) { player.setDisplay(holder); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceDestroyed(SurfaceHolder holder) { } } }
代碼說明:函數
MediaPlayer 的基本使用工具
播放一個網絡流的基本流程以下:
String url = "http://........"; // your URL here MediaPlayer mediaPlayer = new MediaPlayer(); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.setDataSource(url); mediaPlayer.prepare(); // might take long! (for buffering, etc) mediaPlayer.start();
可是這種設置只可以播放音頻,由於沒有給MediaPlayer綁定view去顯示圖像。
添加視頻播放,主要是知足MediaPlayer.SetDisplay(SurfaceHolder sh)
Tips:
- 用於生成hls流的工具,可使用開源工具SRS。
- 使用android模擬器不可以顯示視頻,須要使用真機,也是讓人挺不開心呢。
像上文中提到的MediaPlayer的最簡單用法,封裝的十分嚴密,基本上不知道發生了啥。尤爲是當咱們想要拿到視頻的某一幅畫面的時候,就變得更加困難。所以咱們就須要使用TextureView這個控件了。廢話很少說,直接貼代碼
佈局文件
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.hankun.myapplication.MainActivity"> <TextureView android:id="@+id/text_screen" android:layout_width="380dp" android:layout_height="204dp" android:layout_weight="1" tools:layout_editor_absoluteY="0dp" tools:layout_editor_absoluteX="0dp" /> <Button android:layout_width="200dp" android:layout_height="50dp" tools:layout_editor_absoluteX="0dp" android:id="@+id/button" android:layout_marginTop="8dp" app:layout_constraintTop_toBottomOf="@+id/text_screen" /> <ImageView android:id="@+id/image_screen" android:layout_width="380dp" android:layout_height="250dp" app:layout_constraintRight_toRightOf="parent" android:layout_marginTop="8dp" app:layout_constraintTop_toBottomOf="@+id/button" android:layout_marginRight="0dp" /> </android.support.constraint.ConstraintLayout>
代碼部分
public class MainActivity extends AppCompatActivity { private MediaPlayer player; private TextureView ttv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); player = new MediaPlayer(); player.setVolume(0, 0); ttv = (TextureView) findViewById(R.id.text_screen); ttv.setVisibility(0); ttv.setSurfaceTextureListener(new TextureListen()); try { String stream_url = "http://192.168.199.245:8080/live/livestream.m3u8"; player.setDataSource(this, Uri.parse(stream_url)); player.prepare(); player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { player.start(); player.setLooping(true); } }); } catch (Exception e) { e.printStackTrace(); } Button btn_image = (Button)findViewById(R.id.button); btn_image.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { ImageView img = (ImageView) findViewById(R.id.image_screen); img.setImageBitmap(ttv.getBitmap()); } }); } private class TextureListen implements TextureView.SurfaceTextureListener { @Override public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i1) { try { player.setSurface(new Surface(surfaceTexture)); } catch (Exception e) { e.printStackTrace();; } } @Override public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i1) { } @Override public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) { return false; } @Override public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) { } } }
在使用TextureView的時候,再也不調用MediaPlayer 的setDisplay
函數,而是調用了setSurface
函數。由於SurfaceTexture那裏涉及到了不少OpenGL ES的東西,還沒來得及看,就先略過不說了。
Tips: 在不可以理OpenGL ES的時候,咱們還想獲取到一個視頻中的畫面,那麼不妨使用這種形式:將TextureView置於最底層並使用控件遮住。而後開啓獨立線程去刷新(就是代碼中按鈕的操做),這樣就能獲取到Bitmap,而後進行操做。雖然會浪費一些性能,可是最起碼可用。
我還沒研究,待續吧