【VLC-Android】vlc-android简例
?
前言
繼續(xù)折騰vlc,做這個例子并不順利,卡在只有聲音沒有圖像這個問題好久,網上的例子有些API已經對不上,繼續(xù)分享,,,
?
聲明
歡迎轉載,但請保留文章原始出處:)
博客園:http://www.cnblogs.com
農民伯伯: http://over140.cnblogs.com
?
正文
截圖
項目
代碼
public class VlcVideoActivity extends Activity implements SurfaceHolder.Callback, IVideoPlayer {private final static String TAG = "[VlcVideoActivity]";private SurfaceView mSurfaceView;private LibVLC mMediaPlayer;private SurfaceHolder mSurfaceHolder;private View mLoadingView;private int mVideoHeight;private int mVideoWidth;private int mVideoVisibleHeight;private int mVideoVisibleWidth;private int mSarNum;private int mSarDen;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_video_vlc);mSurfaceView = (SurfaceView) findViewById(R.id.video);mLoadingView = findViewById(R.id.video_loading);try {mMediaPlayer = VLCInstance.getLibVlcInstance();} catch (LibVlcException e) {e.printStackTrace();}mSurfaceHolder = mSurfaceView.getHolder();mSurfaceHolder.setFormat(PixelFormat.RGBX_8888);mSurfaceHolder.addCallback(this);mMediaPlayer.eventVideoPlayerActivityCreated(true);EventHandler em = EventHandler.getInstance();em.addHandler(mVlcHandler);this.setVolumeControlStream(AudioManager.STREAM_MUSIC);mSurfaceView.setKeepScreenOn(true);// mMediaPlayer.setMediaList();// mMediaPlayer.getMediaList().add(new Media(mMediaPlayer, "http://live.3gv.ifeng.com/zixun.m3u8"), false);// mMediaPlayer.playIndex(0);mMediaPlayer.playMRL("http://live.3gv.ifeng.com/zixun.m3u8");}@Overridepublic void onPause() {super.onPause();if (mMediaPlayer != null) {mMediaPlayer.stop();mSurfaceView.setKeepScreenOn(false);}}@Overrideprotected void onDestroy() {super.onDestroy();if (mMediaPlayer != null) {mMediaPlayer.eventVideoPlayerActivityCreated(false);EventHandler em = EventHandler.getInstance();em.removeHandler(mVlcHandler);}}@Overridepublic void onConfigurationChanged(Configuration newConfig) {setSurfaceSize(mVideoWidth, mVideoHeight, mVideoVisibleWidth, mVideoVisibleHeight, mSarNum, mSarDen);super.onConfigurationChanged(newConfig);}@Overridepublic void surfaceCreated(SurfaceHolder holder) {if (mMediaPlayer != null) {mSurfaceHolder = holder;mMediaPlayer.attachSurface(holder.getSurface(), this);}}@Overridepublic void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {mSurfaceHolder = holder;if (mMediaPlayer != null) {mMediaPlayer.attachSurface(holder.getSurface(), this);//, width, height }if (width > 0) {mVideoHeight = height;mVideoWidth = width;}}@Overridepublic void surfaceDestroyed(SurfaceHolder holder) {if (mMediaPlayer != null) {mMediaPlayer.detachSurface();}}@Overridepublic void setSurfaceSize(int width, int height, int visible_width, int visible_height, int sar_num, int sar_den) {mVideoHeight = height;mVideoWidth = width;mVideoVisibleHeight = visible_height;mVideoVisibleWidth = visible_width;mSarNum = sar_num;mSarDen = sar_den;mHandler.removeMessages(HANDLER_SURFACE_SIZE);mHandler.sendEmptyMessage(HANDLER_SURFACE_SIZE);}private static final int HANDLER_BUFFER_START = 1;private static final int HANDLER_BUFFER_END = 2;private static final int HANDLER_SURFACE_SIZE = 3;private static final int SURFACE_BEST_FIT = 0;private static final int SURFACE_FIT_HORIZONTAL = 1;private static final int SURFACE_FIT_VERTICAL = 2;private static final int SURFACE_FILL = 3;private static final int SURFACE_16_9 = 4;private static final int SURFACE_4_3 = 5;private static final int SURFACE_ORIGINAL = 6;private int mCurrentSize = SURFACE_BEST_FIT;private Handler mVlcHandler = new Handler() {@Overridepublic void handleMessage(Message msg) {if (msg == null || msg.getData() == null)return;switch (msg.getData().getInt("event")) {case EventHandler.MediaPlayerTimeChanged:break;case EventHandler.MediaPlayerPositionChanged:break;case EventHandler.MediaPlayerPlaying:mHandler.removeMessages(HANDLER_BUFFER_END);mHandler.sendEmptyMessage(HANDLER_BUFFER_END);break;case EventHandler.MediaPlayerBuffering:break;case EventHandler.MediaPlayerLengthChanged:break;case EventHandler.MediaPlayerEndReached://播放完成break;}}};private Handler mHandler = new Handler() {@Overridepublic void handleMessage(Message msg) {switch (msg.what) {case HANDLER_BUFFER_START:showLoading();break;case HANDLER_BUFFER_END:hideLoading();break;case HANDLER_SURFACE_SIZE:changeSurfaceSize();break;}}};private void showLoading() {mLoadingView.setVisibility(View.VISIBLE);}private void hideLoading() {mLoadingView.setVisibility(View.GONE);}private void changeSurfaceSize() {// get screen sizeint dw = getWindowManager().getDefaultDisplay().getWidth();int dh = getWindowManager().getDefaultDisplay().getHeight();// calculate aspect ratiodouble ar = (double) mVideoWidth / (double) mVideoHeight;// calculate display aspect ratiodouble dar = (double) dw / (double) dh;switch (mCurrentSize) {case SURFACE_BEST_FIT:if (dar < ar)dh = (int) (dw / ar);elsedw = (int) (dh * ar);break;case SURFACE_FIT_HORIZONTAL:dh = (int) (dw / ar);break;case SURFACE_FIT_VERTICAL:dw = (int) (dh * ar);break;case SURFACE_FILL:break;case SURFACE_16_9:ar = 16.0 / 9.0;if (dar < ar)dh = (int) (dw / ar);elsedw = (int) (dh * ar);break;case SURFACE_4_3:ar = 4.0 / 3.0;if (dar < ar)dh = (int) (dw / ar);elsedw = (int) (dh * ar);break;case SURFACE_ORIGINAL:dh = mVideoHeight;dw = mVideoWidth;break;}mSurfaceHolder.setFixedSize(mVideoWidth, mVideoHeight);ViewGroup.LayoutParams lp = mSurfaceView.getLayoutParams();lp.width = dw;lp.height = dh;mSurfaceView.setLayoutParams(lp);mSurfaceView.invalidate();} }代碼說明?
1、vlc-android的版本為2.2.0-pre1 Weatherwax
2、拷貝vlc-android官網例子下面org.videolan.libvlc所有的類,以及VLCInstance、Preferences。只有聲音沒有畫面就出在VLCInstance這個問題上,少了一些配置,參考VLCInstance.updateLibVlcSettings。
3、playMRL不要用Uri來轉,否則打不開
4、提供的代碼已經編譯好了so,也修改源碼打開了EventHandler.MediaPlayerBuffering(也修改了libvlcjni.c),但是還是沒有緩沖進度的值,還有待進一步探索。
5、實際發(fā)現如果只在surfaceChanged中attachSurface,setSurfaceSize回調不會觸發(fā),需要同時在surfaceCreated中設置。
6、本文不討論Handler非靜態(tài)可能引發(fā)內存泄露的問題,大家寫的時候自己注意一下 :)?
?
下載
https://github.com/over140/vlc-android-demo?(注意工程是Android Studio的工程)
?
文章
1、vlc-android 中調用用libvlcjni.so實現流媒體播放?
2、[Github]vlc-android-macbuild
3、vlc android的編譯及截圖,錄制視頻等功能
4、vlc-android源碼編譯過程記錄
5、vlc-android 獲取MediaPlayerEncounteredError,MediaPlayerBuffering等各種事件的響應
6、Vlc-for-android源碼分析
?
結束
?文章2的GitHub項目是對vlc進行封裝,也挺不錯了,值得參考。vlc-android的例子寫的比較復雜,而且api可能老變,大家看文章的時候注意一下版本問題。
轉載于:https://www.cnblogs.com/over140/p/3932784.html
總結
以上是生活随笔為你收集整理的【VLC-Android】vlc-android简例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 体验魔法师的挥杖体验,主播为《霍格沃兹之
- 下一篇: 努比亚将要发布全球首款3D平板 可实现全