最近專案中有需要用線上視訊播放。之前有關於視訊播放介紹了一些工具使用。如ijkplayer ,ExoPlayer,Vitamio,android-UniversalMusicPlayer.
想要了解更多請參考Androidstarjack的部落格地址:
今天打算介紹一個新的音視訊播放器 :JieCaoVideoPlayer
也是專案中將要用到的。要求能線上進行播放。或者是播放本地視訊
先看一下效果圖吧:
一行程式碼快速實現視訊播放,Android視訊播放,AndroidMP3播放,安卓視訊播放一行程式碼搞定,真正實現Android的全屏功能,立志成為Android平臺使用最廣泛的視訊播放控制元件
一,主要特點
- 1,全屏時啟動新Activity實現播放器真正的全屏功能
- 2,能在ListView、ViewPager和ListView、ViewPager和Fragment等多重巢狀模式下全屏工作
- 3,ListView的拖拽和ViewPager的滑動時如果劃出螢幕會自動重置視訊
- 4,視訊大小的螢幕適配,寬或長至少有兩個對邊是充滿螢幕的,另外兩個方向居中
- 5,可以在載入、暫停、播放等各種狀態中正常進入全屏和退出全屏
- 6,根據自己應用的顏色風格換膚
- 7,播放MP3時顯示縮圖片
二,使用步驟
匯入到專案(建議使用第三方庫匯入的形式)
- 匯入libiary
compile 'fm.jiecao:jiecaovideoplayer:5.5.2'複製程式碼
- 在你的佈局中申明JCVideoPlayer 元件
<fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard
android:id="@+id/videoplayer"
android:layout_width="match_parent"
android:layout_height="200dp"/>複製程式碼
在java程式碼中初始化JCVideoPlayer元件ID,並配置相對應的URL
或者設定視訊地址、縮圖地址、標題
JCVideoPlayerStandard jcVideoPlayerStandard = (JCVideoPlayerStandard) findViewById(R.id.videoplayer);
jcVideoPlayerStandard.setUp("http://2449.vod.myqcloud.com/2449_22ca37a6ea9011e5acaaf51d105342e3.f20.mp4", JCVideoPlayerStandard.SCREEN_LAYOUT_NORMAL, "嫂子閉眼睛");
jcVideoPlayerStandard.thumbImageView.setImage("http://p.qpic.cn/videoyun/0/2449_43b6f696980311e59ed467f22794e792_1/640");複製程式碼
- 在包含播放器的Fragment或Activity的onPause()方法中呼叫JCVideoPlayer.releaseAllVideos()。相對應的生命週期裡面要做相對應的操作:
@Override
public void onBackPressed() {
if (JCVideoPlayer.backPress()) {
return;
}
super.onBackPressed();
}
@Override
protected void onPause() {
super.onPause();
JCVideoPlayer.releaseAllVideos();
}複製程式碼
- 最後在 AndroidManifest.xml要進行相對應的配置
<activity
android:name=".MainActivity"
android:configChanges="orientation|screenSize|keyboardHidden"
android:screenOrientation="portrait" /> <!-- or android:screenOrientation="landscape"-->複製程式碼
用JCVideoPlayer的優勢
全屏時啟動新Activity實現播放器真正的全屏功能
能在ListView、ViewPager和ListView、ViewPager和Fragment等多重巢狀模式下全屏工作
ListView的拖拽和ViewPager的滑動時如果劃出螢幕會自動重置視訊
視訊大小的螢幕適配,寬或長至少有兩個對邊是充滿螢幕的,另外兩個方向居中
可以在載入、暫停、播放等各種狀態中正常進入全屏和退出全屏
根據自己應用的顏色風格換膚
- 播放MP3時顯示縮圖片
- 佔用空間非常小,不到50k
送上一個播放地址:
flashmedia.eastday.com/newdate/new…
[wdl.wallstreetcn.com/41aae4d2-39…)
音樂地址:
o6wf52jln.bkt.clouddn.com/演員.mp3
相關程式碼:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
JCVideoPlayerStandard mJcVideoPlayerStandard;
Button mTinyWindow, mAutoTinyWindow, mAboutListView, mPlayDirectly, mAboutApi, mAboutWebView,play_music;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTinyWindow = (Button) findViewById(R.id.tiny_window);
mAutoTinyWindow = (Button) findViewById(R.id.auto_tiny_window);
mPlayDirectly = (Button) findViewById(R.id.play_directly_without_layout);
mAboutListView = (Button) findViewById(R.id.about_listview);
mAboutApi = (Button) findViewById(R.id.about_api);
play_music = (Button) findViewById(R.id.play_music);
mAboutWebView = (Button) findViewById(R.id.about_webview);
mTinyWindow.setOnClickListener(this);
mAutoTinyWindow.setOnClickListener(this);
mAboutListView.setOnClickListener(this);
mPlayDirectly.setOnClickListener(this);
mAboutApi.setOnClickListener(this);
mAboutWebView.setOnClickListener(this);
play_music.setOnClickListener(this);
mJcVideoPlayerStandard = (JCVideoPlayerStandard) findViewById(R.id.jc_video);
mJcVideoPlayerStandard.setUp("http://www.jmzsjy.com/UploadFile/微課/地方風味小吃——宮廷香酥牛肉餅.mp4"
, JCVideoPlayerStandard.SCREEN_LAYOUT_NORMAL, "視訊播放");
Picasso.with(this)
.load("http://img4.jiecaojingxuan.com/2016/11/23/00b026e7-b830-4994-bc87-38f4033806a6.jpg@!640_360")
.into(mJcVideoPlayerStandard.thumbImageView);
JCVideoPlayer.setJcUserAction(new MyUserActionStandard());
}
@Override
protected void onPause() {
super.onPause();
JCVideoPlayer.releaseAllVideos();
}
@Override
public void onBackPressed() {
if (JCVideoPlayer.backPress()) {
return;
}
super.onBackPressed();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tiny_window:
mJcVideoPlayerStandard.startWindowTiny();
break;
case R.id.auto_tiny_window:
startActivity(new Intent(MainActivity.this, AutoTinyActivity.class));
break;
case R.id.play_directly_without_layout:
startActivity(new Intent(MainActivity.this, PlayDirectlyActivity.class));
break;
case R.id.about_listview:
startActivity(new Intent(MainActivity.this, ListViewActivity.class));
break;
case R.id.about_api:
startActivity(new Intent(MainActivity.this, ApiActivity.class));
break;
case R.id.about_webview:
startActivity(new Intent(MainActivity.this, WebViewActivity.class));
break;
case R.id.play_music:
startActivity(new Intent(MainActivity.this, PlayMusicActivity.class));
break;
}
}
class MyUserActionStandard implements JCUserActionStandard {
@Override
public void onEvent(int type, String url, int screen, Object... objects) {
switch (type) {
case JCUserAction.ON_CLICK_START_ICON:
Log.i("USER_EVENT", "ON_CLICK_START_ICON" + " title is : " + (objects.length == 0 ? "" : objects[0]) + " url is : " + url + " screen is : " + screen);
break;
case JCUserAction.ON_CLICK_START_ERROR:
Log.i("USER_EVENT", "ON_CLICK_START_ERROR" + " title is : " + (objects.length == 0 ? "" : objects[0]) + " url is : " + url + " screen is : " + screen);
break;
case JCUserAction.ON_CLICK_START_AUTO_COMPLETE:
Log.i("USER_EVENT", "ON_CLICK_START_AUTO_COMPLETE" + " title is : " + (objects.length == 0 ? "" : objects[0]) + " url is : " + url + " screen is : " + screen);
break;
case JCUserAction.ON_CLICK_PAUSE:
Log.i("USER_EVENT", "ON_CLICK_PAUSE" + " title is : " + (objects.length == 0 ? "" : objects[0]) + " url is : " + url + " screen is : " + screen);
break;
case JCUserAction.ON_CLICK_RESUME:
Log.i("USER_EVENT", "ON_CLICK_RESUME" + " title is : " + (objects.length == 0 ? "" : objects[0]) + " url is : " + url + " screen is : " + screen);
break;
case JCUserAction.ON_SEEK_POSITION:
Log.i("USER_EVENT", "ON_SEEK_POSITION" + " title is : " + (objects.length == 0 ? "" : objects[0]) + " url is : " + url + " screen is : " + screen);
break;
case JCUserAction.ON_AUTO_COMPLETE:
Log.i("USER_EVENT", "ON_AUTO_COMPLETE" + " title is : " + (objects.length == 0 ? "" : objects[0]) + " url is : " + url + " screen is : " + screen);
break;
case JCUserAction.ON_ENTER_FULLSCREEN:
Log.i("USER_EVENT", "ON_ENTER_FULLSCREEN" + " title is : " + (objects.length == 0 ? "" : objects[0]) + " url is : " + url + " screen is : " + screen);
break;
case JCUserAction.ON_QUIT_FULLSCREEN:
Log.i("USER_EVENT", "ON_QUIT_FULLSCREEN" + " title is : " + (objects.length == 0 ? "" : objects[0]) + " url is : " + url + " screen is : " + screen);
break;
case JCUserAction.ON_ENTER_TINYSCREEN:
Log.i("USER_EVENT", "ON_ENTER_TINYSCREEN" + " title is : " + (objects.length == 0 ? "" : objects[0]) + " url is : " + url + " screen is : " + screen);
break;
case JCUserAction.ON_QUIT_TINYSCREEN:
Log.i("USER_EVENT", "ON_QUIT_TINYSCREEN" + " title is : " + (objects.length == 0 ? "" : objects[0]) + " url is : " + url + " screen is : " + screen);
break;
case JCUserAction.ON_TOUCH_SCREEN_SEEK_VOLUME:
Log.i("USER_EVENT", "ON_TOUCH_SCREEN_SEEK_VOLUME" + " title is : " + (objects.length == 0 ? "" : objects[0]) + " url is : " + url + " screen is : " + screen);
break;
case JCUserAction.ON_TOUCH_SCREEN_SEEK_POSITION:
Log.i("USER_EVENT", "ON_TOUCH_SCREEN_SEEK_POSITION" + " title is : " + (objects.length == 0 ? "" : objects[0]) + " url is : " + url + " screen is : " + screen);
break;
case JCUserActionStandard.ON_CLICK_START_THUMB:
Log.i("USER_EVENT", "ON_CLICK_START_THUMB" + " title is : " + (objects.length == 0 ? "" : objects[0]) + " url is : " + url + " screen is : " + screen);
break;
case JCUserActionStandard.ON_CLICK_BLANK:
Log.i("USER_EVENT", "ON_CLICK_BLANK" + " title is : " + (objects.length == 0 ? "" : objects[0]) + " url is : " + url + " screen is : " + screen);
break;
default:
Log.i("USER_EVENT", "unknow");
break;
}
}
}
}複製程式碼
相關佈局 activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard
android:id="@+id/jc_video"
android:layout_width="300dp"
android:layout_height="300dp" />
</LinearLayout>複製程式碼
資源下載:
GitHub連結下載:複製程式碼
CSDN連結下載:複製程式碼
download.csdn.net/download/an…
想要了解更多請參考Androidstarjack的部落格地址:
如果你覺得此文對您有所幫助,歡迎入群 QQ交流群 :232203809
微信公眾號:終端研發部
(歡迎關注學習和交流)