Commit 4c9b109a5b1666e1a5b8675653bf181019814104

Authored by 朱显松
1 parent b209f48f

更新 VideoSDK2.0

.idea/gradle.xml
... ... @@ -5,7 +5,6 @@
5 5 <GradleProjectSettings>
6 6 <option name="distributionType" value="DEFAULT_WRAPPED" />
7 7 <option name="externalProjectPath" value="$PROJECT_DIR$" />
8   - <option name="gradleHome" value="$APPLICATION_HOME_DIR$/gradle/gradle-2.14.1" />
9 8 <option name="modules">
10 9 <set>
11 10 <option value="$PROJECT_DIR$" />
... ...
app/libs/libcnliveutil.jar
No preview for this file type
app/libs/libcnlivevideo.jar
No preview for this file type
app/src/main/AndroidManifest.xml
... ... @@ -26,17 +26,16 @@
26 26  
27 27  
28 28 <activity
29   - android:name=".CNPlayerActivity"
  29 + android:name=".MediaPlayerActivity"
30 30 android:configChanges="keyboardHidden|screenSize|orientation"
31   - android:launchMode="singleTask"
32 31 android:screenOrientation="portrait"
33 32 android:theme="@style/Theme.AppCompat.Light.NoActionBar" />
34 33 <activity
35   - android:name=".PlayerActivity"
  34 + android:name=".VideoViewActivity"
36 35 android:configChanges="keyboardHidden|screenSize|orientation"
37 36 android:screenOrientation="portrait"/>
38 37 <activity
39   - android:name=".CNBarrageActivity"
  38 + android:name=".BarrageActivity"
40 39 android:configChanges="keyboardHidden|screenSize|orientation"
41 40 android:screenOrientation="portrait"/>
42 41  
... ...
app/src/main/java/com/cnlive/demo/video/CNBarrageActivity.java renamed to app/src/main/java/com/cnlive/demo/video/BarrageActivity.java
... ... @@ -10,10 +10,7 @@ import com.cnlive.demo.video.barrage.BarrageGiftMessage;
10 10 import com.cnlive.demo.video.barrage.BarrageGiftView;
11 11 import com.cnlive.libs.video.barrage.BarrageLayout;
12 12  
13   -/**
14   - * Created by gujun on 2016/8/10.
15   - */
16   -public class CNBarrageActivity extends Activity implements View.OnClickListener{
  13 +public class BarrageActivity extends Activity implements View.OnClickListener{
17 14  
18 15 private BarrageLayout barrageLayout, barrageGiftLayout;
19 16  
... ...
app/src/main/java/com/cnlive/demo/video/CNLiveApplication.java
... ... @@ -11,8 +11,7 @@ public class CNLiveApplication extends Application {
11 11 @Override
12 12 public void onCreate() {
13 13 super.onCreate();
14   - //TODO 换成项目申请的对应的key
15   - // 设置SDK AppID 及SpKey
  14 + //换成项目申请的对应的key
16 15 Config.init(this, "60_irn9edco29", "69fe70bda67e059286ad509d9f5377f815955645b52f94");
17 16 }
18 17 }
... ...
app/src/main/java/com/cnlive/demo/video/CNPlayerActivity.java deleted 100644 → 0
1   -package com.cnlive.demo.video;
2   -
3   -import android.app.Activity;
4   -import android.content.Intent;
5   -import android.os.Bundle;
6   -import android.os.Handler;
7   -import android.os.Message;
8   -import android.util.Log;
9   -import android.view.View;
10   -import android.widget.ImageView;
11   -import android.widget.SeekBar;
12   -import android.widget.TextView;
13   -import android.widget.Toast;
14   -
15   -import com.cnlive.libs.video.player.CNMediaPlayerView;
16   -import com.cnlive.libs.video.util.OnPlayStateListener;
17   -import com.ksyun.media.player.IMediaPlayer;
18   -import com.ksyun.media.player.KSYMediaPlayer;
19   -
20   -/**
21   - * Created by gujun on 2016/8/1.
22   - */
23   -public class CNPlayerActivity extends Activity implements View.OnClickListener, SeekBar.OnSeekBarChangeListener,OnPlayStateListener {
24   - private static final String TAG = "CNPlayerActivity";
25   -
26   - private final int UPDATE_SEEKBAR = 1;
27   -
28   - private boolean isPlay = false;
29   - private int mVideoProgress = 0;
30   -
31   - private CNMediaPlayerView cnPlayerView;
32   - private SeekBar videoProgressBar;
33   - private TextView videoTime;
34   - private ImageView playerPlayPause;
35   -
36   - private Handler mHandler = new Handler() {
37   - @Override
38   - public void handleMessage(Message msg) {
39   - super.handleMessage(msg);
40   - switch (msg.what) {
41   - case UPDATE_SEEKBAR:
42   - setVideoProgress(0);
43   - break;
44   - }
45   - }
46   - };
47   -
48   - private IMediaPlayer.OnBufferingUpdateListener mOnBufferingUpdateListener = new IMediaPlayer.OnBufferingUpdateListener() {
49   -
50   - @Override
51   - public void onBufferingUpdate(IMediaPlayer iMediaPlayer, int percent) {
52   - long duration = cnPlayerView.getDuration();
53   - long progress = duration * percent / 100;
54   - if(videoProgressBar != null) videoProgressBar.setSecondaryProgress((int)progress);
55   - }
56   - };
57   -
58   - private IMediaPlayer.OnPreparedListener mOnPrepareListener = new IMediaPlayer.OnPreparedListener() {
59   - @Override
60   - public void onPrepared(IMediaPlayer iMediaPlayer) {
61   - setPlayState(true);
62   - isPlay = true;
63   - }
64   - };
65   -
66   - private IMediaPlayer.OnCompletionListener mOnCompletionListener = new IMediaPlayer.OnCompletionListener() {
67   - @Override
68   - public void onCompletion(IMediaPlayer iMediaPlayer) {
69   - Toast.makeText(CNPlayerActivity.this, "OnCompletionListener, play complete.", Toast.LENGTH_LONG).show();
70   - isPlay = false;
71   - if (playerPlayPause != null)
72   - playerPlayPause.setImageResource(R.drawable.cnlive_video_play);
73   - if (mHandler != null)
74   - mHandler.removeMessages(UPDATE_SEEKBAR);
75   - }
76   - };
77   -
78   - private IMediaPlayer.OnInfoListener mOnInfoListener = new IMediaPlayer.OnInfoListener() {
79   - @Override
80   - public boolean onInfo(IMediaPlayer iMediaPlayer, int what, int extra) {
81   - switch (what) {
82   - case KSYMediaPlayer.MEDIA_INFO_BUFFERING_START:
83   - Log.d(TAG, "Buffering Start.");
84   - break;
85   - case KSYMediaPlayer.MEDIA_INFO_BUFFERING_END:
86   - Log.d(TAG, "Buffering End.");
87   - break;
88   - case KSYMediaPlayer.MEDIA_INFO_AUDIO_RENDERING_START:
89   - Toast.makeText(CNPlayerActivity.this, "Audio Rendering Start", Toast.LENGTH_SHORT).show();
90   - break;
91   - case KSYMediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START:
92   - Toast.makeText(CNPlayerActivity.this, "Video Rendering Start", Toast.LENGTH_SHORT).show();
93   - break;
94   - case KSYMediaPlayer.MEDIA_INFO_RELOADED:
95   - Log.d(TAG, "Reload Success");
96   - break;
97   - }
98   - return false;
99   - }
100   - };
101   -
102   - private IMediaPlayer.OnErrorListener mOnErrorListener = new IMediaPlayer.OnErrorListener() {
103   - @Override
104   - public boolean onError(IMediaPlayer mp, int what, int extra) {
105   - switch (what) {
106   - case KSYMediaPlayer.MEDIA_ERROR_UNKNOWN:
107   - Log.e(TAG, "OnErrorListener, Error Unknown:" + what + ",extra:" + extra);
108   - break;
109   - default:
110   - Log.e(TAG, "OnErrorListener, Error:" + what + ",extra:" + extra);
111   - }
112   -
113   - return false;
114   - }
115   - };
116   -
117   - private IMediaPlayer.OnSeekCompleteListener mOnSeekCompleteListener = new IMediaPlayer.OnSeekCompleteListener() {
118   - @Override
119   - public void onSeekComplete(IMediaPlayer iMediaPlayer) {
120   -
121   - }
122   - };
123   -
124   - private IMediaPlayer.OnVideoSizeChangedListener mOnVideoSizeChangedListener = new IMediaPlayer.OnVideoSizeChangedListener() {
125   - @Override
126   - public void onVideoSizeChanged(IMediaPlayer mp, int width, int height, int sarNum, int sarDen) {
127   -
128   - }
129   - };
130   -
131   - @Override
132   - protected void onCreate(Bundle savedInstanceState) {
133   - super.onCreate(savedInstanceState);
134   - setContentView(R.layout.activity_cnplayer_new);
135   - init();
136   - }
137   -
138   - @Override
139   - protected void onNewIntent(Intent intent) {
140   - super.onNewIntent(intent);
141   - init();
142   - }
143   -
144   - private void init() {
145   - videoProgressBar = (SeekBar) findViewById(R.id.videoProgressBar);
146   - if (videoProgressBar != null) videoProgressBar.setOnSeekBarChangeListener(this);
147   - videoTime = (TextView) findViewById(R.id.time);
148   - playerPlayPause = (ImageView) findViewById(R.id.playerPlayPause);
149   - playerPlayPause.setOnClickListener(this);
150   - cnPlayerView = (CNMediaPlayerView) findViewById(R.id.cnPlayerView);
151   - cnPlayerView.setKeepScreenOn(true);
152   - initVideo();
153   - }
154   -
155   - private void initVideo() {
156   - cnPlayerView.stop();
157   - cnPlayerView.release();
158   -
159   - cnPlayerView.build();
160   - cnPlayerView.setOnBufferingUpdateListener(mOnBufferingUpdateListener);
161   - cnPlayerView.setOnCompletionListener(mOnCompletionListener);
162   - cnPlayerView.setOnPreparedListener(mOnPrepareListener);
163   - cnPlayerView.setOnInfoListener(mOnInfoListener);
164   - cnPlayerView.setOnErrorListener(mOnErrorListener);
165   - cnPlayerView.setOnSeekCompleteListener(mOnSeekCompleteListener);
166   - cnPlayerView.setOnVideoSizeChangedListener(mOnVideoSizeChangedListener);
167   - cnPlayerView.setOnPlayStateListener(this);
168   - cnPlayerView.setScreenOnWhilePlaying(true);
169   - cnPlayerView.setBufferTimeMax(5);
170   -
171   - //TODO 获取完epg信息后调用开始播放方法,切换视角和码率的时候也调用以下方法
172   -
173   -
174   - String channelId = "news";
175   - //TODO 播放直播
176   - //cnPlayerView.startPlayLiveVideo(channelId,"2");
177   -
178   - String vId = "0e12b70817c440509d88f67d4e975fe0";
179   -// "a9c5f9a2d6114bf98f2aff89a55d4f98";
180   - //TODO 播放点播
181   - cnPlayerView.startPlayVodVideo(vId,"2");//播放点播节目及切换视角
182   -
183   - findViewById(R.id.retry).setOnClickListener(new View.OnClickListener() {
184   - @Override
185   - public void onClick(View v) {
186   - if(cnPlayerView != null) cnPlayerView.retryToPlay();
187   - }
188   - });
189   -
190   -
191   - findViewById(R.id.newActivity).setOnClickListener(new View.OnClickListener() {
192   - @Override
193   - public void onClick(View v) {
194   - startActivity(new Intent(CNPlayerActivity.this, CNPlayerActivity.class));
195   - }
196   - });
197   -
198   - }
199   -
200   - @Override
201   - protected void onPause() {
202   - super.onPause();
203   - if (cnPlayerView != null && cnPlayerView.isPlaying()) {
204   - setPlayState(false);
205   - }
206   - }
207   -
208   - @Override
209   - protected void onResume() {
210   - super.onResume();
211   - if (cnPlayerView != null && !cnPlayerView.isPlaying() && isPlay) {
212   - setPlayState(true);
213   - }
214   - }
215   -
216   - @Override
217   - protected void onDestroy() {
218   - if (mHandler != null)
219   - mHandler.removeMessages(UPDATE_SEEKBAR);
220   - stopPlayBack();
221   - super.onDestroy();
222   - }
223   -
224   - private void stopPlayBack() {
225   - if (cnPlayerView != null) {
226   - cnPlayerView.stop();
227   - cnPlayerView.release();
228   - }
229   - }
230   -
231   - public int setVideoProgress(int currentProgress) {
232   -
233   - if (cnPlayerView == null)
234   - return -1;
235   -
236   - long time = currentProgress > 0 ? currentProgress : cnPlayerView.getCurrentPosition();
237   - long length = cnPlayerView.getDuration();
238   -
239   - // Update all view elements
240   - if (videoProgressBar != null) {
241   - videoProgressBar.setMax((int) length);
242   - videoProgressBar.setProgress((int) time);
243   - }
244   -
245   - if (time >= 0) {
246   - String progress = Util.stringForTime(time / 1000) + "/" + Util.stringForTime(length / 1000);
247   - if (videoTime != null)
248   - videoTime.setText(progress);
249   - }
250   -
251   - Message msg = new Message();
252   - msg.what = UPDATE_SEEKBAR;
253   -
254   - if (mHandler != null) {
255   - mHandler.removeMessages(UPDATE_SEEKBAR);
256   - mHandler.sendMessageDelayed(msg, 1000);
257   - }
258   -
259   - return (int) time;
260   - }
261   -
262   - @Override
263   - public void onClick(View v) {
264   - switch (v.getId()){
265   - case R.id.playerPlayPause:
266   - if (cnPlayerView != null) {
267   - isPlay = !isPlay;
268   - setPlayState(isPlay);
269   - }
270   - break;
271   - }
272   - }
273   -
274   - private void setPlayState(boolean isPlay) {
275   - if (isPlay) cnPlayerView.start();
276   - else cnPlayerView.pause();
277   - }
278   -
279   - protected void updateProgress(boolean isPlay) {
280   - if (isPlay) setVideoProgress(0);
281   - else if (mHandler != null) mHandler.removeMessages(UPDATE_SEEKBAR);
282   - }
283   -
284   - @Override
285   - public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
286   - if (fromUser) {
287   - mVideoProgress = progress;
288   - }
289   - }
290   -
291   - @Override
292   - public void onStartTrackingTouch(SeekBar seekBar) {
293   -
294   - }
295   -
296   - @Override
297   - public void onStopTrackingTouch(SeekBar seekBar) {
298   - cnPlayerView.seekTo(mVideoProgress);
299   - setVideoProgress(mVideoProgress);
300   - }
301   -
302   - @Override
303   - public void onPlayState(boolean isPlay) {
304   - if (isPlay) playerPlayPause.setImageResource(R.drawable.cnlive_video_pause);
305   - else playerPlayPause.setImageResource(R.drawable.cnlive_video_play);
306   - updateProgress(isPlay);
307   - }
308   -
309   -}
app/src/main/java/com/cnlive/demo/video/MainActivity.java
... ... @@ -5,7 +5,7 @@ import android.os.Bundle;
5 5 import android.support.v7.app.AppCompatActivity;
6 6 import android.view.View;
7 7  
8   -public class MainActivity extends AppCompatActivity implements View.OnClickListener{
  8 +public class MainActivity extends AppCompatActivity implements View.OnClickListener {
9 9  
10 10 @Override
11 11 protected void onCreate(Bundle savedInstanceState) {
... ... @@ -18,15 +18,18 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
18 18  
19 19 @Override
20 20 public void onClick(View view) {
21   - switch (view.getId()){
  21 + Intent intent;
  22 + switch (view.getId()) {
22 23 case R.id.play:
23   - startActivity(new Intent(this,PlayerActivity.class));
  24 + intent = new Intent(this, MediaPlayerActivity.class);
  25 + intent.putExtra("path", "rtmp://live.hkstv.hk.lxdns.com/live/hks");
  26 + startActivity(intent);
24 27 break;
25 28 case R.id.videoId:
26   - startActivity(new Intent(this, CNPlayerActivity.class));
  29 + startActivity(new Intent(this, VideoViewActivity.class));
27 30 break;
28 31 case R.id.barrageBtn:
29   - startActivity(new Intent(this,CNBarrageActivity.class));
  32 + startActivity(new Intent(this, BarrageActivity.class));
30 33 break;
31 34 }
32 35 }
... ...
app/src/main/java/com/cnlive/demo/video/MediaPlayerActivity.java 0 → 100644
  1 +package com.cnlive.demo.video;
  2 +
  3 +import android.app.Activity;
  4 +import android.content.Context;
  5 +import android.content.res.Configuration;
  6 +import android.graphics.Bitmap;
  7 +import android.media.AudioManager;
  8 +import android.os.Bundle;
  9 +import android.os.Environment;
  10 +import android.os.Handler;
  11 +import android.os.Message;
  12 +import android.util.Log;
  13 +import android.view.KeyEvent;
  14 +import android.view.MotionEvent;
  15 +import android.view.SurfaceHolder;
  16 +import android.view.SurfaceHolder.Callback;
  17 +import android.view.SurfaceView;
  18 +import android.view.View;
  19 +import android.view.ViewGroup.LayoutParams;
  20 +import android.view.WindowManager;
  21 +import android.widget.ImageView;
  22 +import android.widget.LinearLayout;
  23 +import android.widget.RelativeLayout;
  24 +import android.widget.SeekBar;
  25 +import android.widget.TextView;
  26 +import android.widget.Toast;
  27 +
  28 +import com.cnlive.libs.video.video.MediaPlayer;
  29 +import com.cnlive.libs.video.video.base.IMediaPlayer;
  30 +
  31 +import java.io.File;
  32 +import java.io.FileNotFoundException;
  33 +import java.io.FileOutputStream;
  34 +import java.io.IOException;
  35 +import java.text.DecimalFormat;
  36 +import java.text.NumberFormat;
  37 +import java.util.Locale;
  38 +
  39 +
  40 +/**
  41 + * @author xinbaicheng@kingsoft.com
  42 + * 2016/7/27.
  43 + */
  44 +public class MediaPlayerActivity extends Activity implements View.OnClickListener {
  45 +
  46 + private static final String TAG = "VideoPlayerActivity";
  47 +
  48 + public static final int UPDATE_SEEKBAR = 0;
  49 + public static final int HIDDEN_SEEKBAR = 1;
  50 +
  51 +
  52 + private Context mContext;
  53 + private MediaPlayer mMediaPlayer;
  54 +
  55 + private SurfaceView mVideoSurfaceView = null;
  56 + private SurfaceHolder mSurfaceHolder = null;
  57 +
  58 + private Handler mHandler;
  59 +
  60 + private LinearLayout mPlayerPanel;
  61 + private ImageView mPlayerStartBtn;
  62 + private SeekBar mPlayerSeekbar;
  63 + private TextView mPlayerPosition;
  64 + private TextView mLoadText;
  65 +
  66 + private RelativeLayout toppanel;
  67 +
  68 + private boolean mPlayerPanelShow = false;
  69 + private boolean mPause = false;
  70 + private boolean mmute = false;
  71 +
  72 + private int mVideoWidth = 0;
  73 + private int mVideoHeight = 0;
  74 +
  75 + private int mVideoScaleIndex = 0;
  76 +
  77 + private int rotatenum = 0;
  78 +
  79 + private String mDataSource;
  80 +
  81 + private MediaPlayer.OnPreparedListener mOnPreparedListener = new MediaPlayer.OnPreparedListener() {
  82 + @Override
  83 + public void onPrepared(IMediaPlayer mediaplayer) {
  84 + mVideoWidth = mMediaPlayer.getVideoWidth();
  85 + mVideoHeight = mMediaPlayer.getVideoHeight();
  86 +
  87 + // Set Video Scaling Mode
  88 + mMediaPlayer.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);
  89 +
  90 + //start player
  91 + mMediaPlayer.start();
  92 +
  93 + //set progress
  94 + setVideoProgress(0);
  95 +
  96 + mPlayerPosition.setVisibility(View.VISIBLE);
  97 + mLoadText.setVisibility(View.VISIBLE);
  98 + }
  99 + };
  100 +
  101 + private IMediaPlayer.OnBufferingUpdateListener mOnBufferingUpdateListener = new IMediaPlayer.OnBufferingUpdateListener() {
  102 + @Override
  103 + public void onBufferingUpdate(IMediaPlayer mp, int percent) {
  104 + long duration = mMediaPlayer.getDuration();
  105 + long progress = duration * percent / 100;
  106 + mPlayerSeekbar.setSecondaryProgress((int) progress);
  107 + }
  108 + };
  109 +
  110 + private IMediaPlayer.OnVideoSizeChangedListener mOnVideoSizeChangeListener = new IMediaPlayer.OnVideoSizeChangedListener() {
  111 + @Override
  112 + public void onVideoSizeChanged(IMediaPlayer mp, int width, int height, int sarNum, int sarDen) {
  113 + if (mVideoWidth > 0 && mVideoHeight > 0) {
  114 + if (width != mVideoWidth || height != mVideoHeight) {
  115 + mVideoWidth = mp.getVideoWidth();
  116 + mVideoHeight = mp.getVideoHeight();
  117 +
  118 + if (mMediaPlayer != null)
  119 + mMediaPlayer.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);
  120 + }
  121 + }
  122 + }
  123 + };
  124 +
  125 + private IMediaPlayer.OnSeekCompleteListener mOnSeekCompletedListener = new IMediaPlayer.OnSeekCompleteListener() {
  126 + @Override
  127 + public void onSeekComplete(IMediaPlayer mp) {
  128 + Log.e(TAG, "onSeekComplete...............");
  129 + }
  130 + };
  131 +
  132 + private IMediaPlayer.OnCompletionListener mOnCompletionListener = new IMediaPlayer.OnCompletionListener() {
  133 + @Override
  134 + public void onCompletion(IMediaPlayer mp) {
  135 + Toast.makeText(mContext, "OnCompletionListener, play complete.", Toast.LENGTH_LONG).show();
  136 + videoPlayEnd();
  137 + }
  138 + };
  139 +
  140 + private IMediaPlayer.OnErrorListener mOnErrorListener = new IMediaPlayer.OnErrorListener() {
  141 + @Override
  142 + public boolean onError(IMediaPlayer mp, int what, int extra) {
  143 + switch (what) {
  144 + case MediaPlayer.MEDIA_ERROR_UNKNOWN:
  145 + Log.e(TAG, "OnErrorListener, Error Unknown:" + what + ",extra:" + extra);
  146 + break;
  147 + default:
  148 + Log.e(TAG, "OnErrorListener, Error:" + what + ",extra:" + extra);
  149 + }
  150 +
  151 + videoPlayEnd();
  152 +
  153 + return false;
  154 + }
  155 + };
  156 +
  157 + public IMediaPlayer.OnInfoListener mOnInfoListener = new IMediaPlayer.OnInfoListener() {
  158 + @Override
  159 + public boolean onInfo(IMediaPlayer iMediaPlayer, int i, int i1) {
  160 + switch (i) {
  161 + case MediaPlayer.MEDIA_INFO_BUFFERING_START:
  162 + Log.d(TAG, "Buffering Start.");
  163 + break;
  164 + case MediaPlayer.MEDIA_INFO_BUFFERING_END:
  165 + Log.d(TAG, "Buffering End.");
  166 + break;
  167 + case MediaPlayer.MEDIA_INFO_AUDIO_RENDERING_START:
  168 + Toast.makeText(mContext, "Audio Rendering Start", Toast.LENGTH_SHORT).show();
  169 + break;
  170 + case MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START:
  171 + Toast.makeText(mContext, "Video Rendering Start", Toast.LENGTH_SHORT).show();
  172 + break;
  173 + case MediaPlayer.MEDIA_INFO_SUGGEST_RELOAD:
  174 + // Player find a new stream(video or audio), and we could reload the video.
  175 + if (mMediaPlayer != null)
  176 + mMediaPlayer.reload(mDataSource, false);
  177 + break;
  178 + case MediaPlayer.MEDIA_INFO_RELOADED:
  179 + Toast.makeText(mContext, "Succeed to reload video.", Toast.LENGTH_SHORT).show();
  180 + Log.d(TAG, "Succeed to reload video.");
  181 + return false;
  182 + }
  183 + return false;
  184 + }
  185 + };
  186 +
  187 + public IMediaPlayer.OnPlayStateListener mOnPlayStateListener = new IMediaPlayer.OnPlayStateListener() {
  188 + @Override
  189 + public void onPlayState(boolean played) {
  190 + mPlayerStartBtn.setBackgroundResource(played ? R.drawable.cnlive_video_pause : R.drawable.cnlive_video_play);
  191 + }
  192 + };
  193 +
  194 + private View.OnClickListener mVideoScaleButton = new View.OnClickListener() {
  195 + @Override
  196 + public void onClick(View v) {
  197 + int mode = mVideoScaleIndex % 2;
  198 + mVideoScaleIndex++;
  199 + mHandler.removeMessages(HIDDEN_SEEKBAR);
  200 + Message msg = new Message();
  201 + msg.what = HIDDEN_SEEKBAR;
  202 + mHandler.sendMessageDelayed(msg, 3000);
  203 + if (mMediaPlayer != null) {
  204 + if (mode == 1)
  205 + mMediaPlayer.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);
  206 + else
  207 + mMediaPlayer.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT);
  208 + }
  209 + }
  210 + };
  211 +
  212 + @Override
  213 + protected void onCreate(Bundle savedInstanceState) {
  214 + super.onCreate(savedInstanceState);
  215 +
  216 + mContext = this.getApplicationContext();
  217 +
  218 + setContentView(R.layout.activity_mediaplayer);
  219 +
  220 + mPlayerPanel = (LinearLayout) findViewById(R.id.player_panel);
  221 + mPlayerStartBtn = (ImageView) findViewById(R.id.player_start);
  222 + mPlayerSeekbar = (SeekBar) findViewById(R.id.player_seekbar);
  223 + mPlayerPosition = (TextView) findViewById(R.id.player_time);
  224 + mLoadText = (TextView) findViewById(R.id.loading_text);
  225 +
  226 + toppanel = (RelativeLayout) findViewById(R.id.topPanel_player);
  227 + findViewById(R.id.player_reload).setOnClickListener(this);
  228 + findViewById(R.id.btn_rotate_player).setOnClickListener(this);
  229 + findViewById(R.id.btn_screen_player).setOnClickListener(this);
  230 + findViewById(R.id.btn_mute_player).setOnClickListener(this);
  231 + findViewById(R.id.player_scale).setOnClickListener(mVideoScaleButton);
  232 +
  233 + mPlayerStartBtn.setOnClickListener(mStartBtnListener);
  234 + mPlayerSeekbar.setOnSeekBarChangeListener(mSeekBarListener);
  235 + mPlayerSeekbar.setEnabled(true);
  236 +
  237 + mVideoSurfaceView = (SurfaceView) findViewById(R.id.player_surface);
  238 + mSurfaceHolder = mVideoSurfaceView.getHolder();
  239 + mSurfaceHolder.addCallback(mSurfaceCallback);
  240 + mVideoSurfaceView.setOnTouchListener(mTouchListener);
  241 + mVideoSurfaceView.setKeepScreenOn(true);
  242 +
  243 + this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
  244 +
  245 +
  246 + mHandler = new Handler() {
  247 + @Override
  248 + public void handleMessage(Message msg) {
  249 + super.handleMessage(msg);
  250 + switch (msg.what) {
  251 + case UPDATE_SEEKBAR:
  252 + setVideoProgress(0);
  253 + break;
  254 + case HIDDEN_SEEKBAR:
  255 + mPlayerPanelShow = false;
  256 + mPlayerPanel.setVisibility(View.GONE);
  257 + toppanel.setVisibility(View.GONE);
  258 + break;
  259 + }
  260 +
  261 + }
  262 + };
  263 +
  264 +
  265 + mDataSource = getIntent().getStringExtra("path");
  266 +
  267 + mMediaPlayer = new MediaPlayer(mContext);
  268 + mMediaPlayer.setOnBufferingUpdateListener(mOnBufferingUpdateListener);
  269 + mMediaPlayer.setOnCompletionListener(mOnCompletionListener);
  270 + mMediaPlayer.setOnPreparedListener(mOnPreparedListener);
  271 + mMediaPlayer.setOnInfoListener(mOnInfoListener);
  272 + mMediaPlayer.setOnVideoSizeChangedListener(mOnVideoSizeChangeListener);
  273 + mMediaPlayer.setOnErrorListener(mOnErrorListener);
  274 + mMediaPlayer.setOnSeekCompleteListener(mOnSeekCompletedListener);
  275 + mMediaPlayer.setOnPlayStateListener(mOnPlayStateListener);
  276 + mMediaPlayer.setScreenOnWhilePlaying(true);
  277 + mMediaPlayer.setBufferTimeMax(3);
  278 + mMediaPlayer.setTimeout(5, 30);
  279 +
  280 + try {
  281 + mMediaPlayer.setDataSource(mDataSource);
  282 + mMediaPlayer.prepareAsync();
  283 + } catch (IOException e) {
  284 + e.printStackTrace();
  285 + }
  286 + }
  287 +
  288 +
  289 + @Override
  290 + protected void onDestroy() {
  291 + super.onDestroy();
  292 + }
  293 +
  294 + @Override
  295 + protected void onPause() {
  296 + super.onPause();
  297 +
  298 + if (mMediaPlayer != null) {
  299 + mMediaPlayer.pause();
  300 + mPause = true;
  301 + }
  302 + }
  303 +
  304 + @Override
  305 + protected void onResume() {
  306 + super.onResume();
  307 + if (mMediaPlayer != null) {
  308 + mMediaPlayer.start();
  309 + mPause = false;
  310 + }
  311 + }
  312 +
  313 + @Override
  314 + public boolean onKeyDown(int keyCode, KeyEvent event) {
  315 + if (keyCode == KeyEvent.KEYCODE_BACK) {
  316 + videoPlayEnd();
  317 + }
  318 +
  319 + return super.onKeyDown(keyCode, event);
  320 + }
  321 +
  322 + @Override
  323 + public int getChangingConfigurations() {
  324 + return super.getChangingConfigurations();
  325 + }
  326 +
  327 + @Override
  328 + public void onConfigurationChanged(Configuration newConfig) {
  329 + super.onConfigurationChanged(newConfig);
  330 + }
  331 +
  332 + private void scaleVideoView() {
  333 + if (mMediaPlayer == null || mMediaPlayer.getVideoHeight() <= 0 || mVideoSurfaceView == null)
  334 + return;
  335 +
  336 + WindowManager wm = this.getWindowManager();
  337 + int sw = wm.getDefaultDisplay().getWidth();
  338 + int sh = wm.getDefaultDisplay().getHeight();
  339 + int videoWidth = mVideoWidth;
  340 + int videoHeight = mVideoHeight;
  341 + int visibleWidth = 0;
  342 + int visibleHeight = 0;
  343 +
  344 + if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
  345 + visibleWidth = sw > sh ? sh : sw;
  346 + visibleHeight = (int) Math.ceil(visibleWidth * videoHeight / videoWidth);
  347 + } else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
  348 +
  349 + if (videoHeight * sw > videoWidth * sh) {
  350 + visibleHeight = sh;
  351 + visibleWidth = (int) Math.ceil(videoWidth * visibleHeight / videoHeight);
  352 + } else {
  353 + visibleWidth = sw;
  354 + visibleHeight = (int) Math.ceil(visibleWidth * videoHeight / videoWidth);
  355 + }
  356 + }
  357 +
  358 + LayoutParams lp = mVideoSurfaceView.getLayoutParams();
  359 + lp.width = visibleWidth;
  360 + lp.height = visibleHeight;
  361 + mVideoSurfaceView.setLayoutParams(lp);
  362 +
  363 + mVideoSurfaceView.invalidate();
  364 + }
  365 +
  366 + // Maybe we could support gesture detect
  367 + private void dealTouchEvent(View view, MotionEvent event) {
  368 + mPlayerPanelShow = !mPlayerPanelShow;
  369 +
  370 + if (mPlayerPanelShow) {
  371 + mPlayerPanel.setVisibility(View.VISIBLE);
  372 + toppanel.setVisibility(View.VISIBLE);
  373 +
  374 + Message msg = new Message();
  375 + msg.what = HIDDEN_SEEKBAR;
  376 + mHandler.sendMessageDelayed(msg, 3000);
  377 + } else {
  378 + mPlayerPanel.setVisibility(View.GONE);
  379 + toppanel.setVisibility(View.GONE);
  380 + mHandler.removeMessages(HIDDEN_SEEKBAR);
  381 + }
  382 + }
  383 +
  384 + public int setVideoProgress(int currentProgress) {
  385 +
  386 + if (mMediaPlayer == null)
  387 + return -1;
  388 +
  389 + long time = currentProgress > 0 ? currentProgress : mMediaPlayer.getCurrentPosition();
  390 + long length = mMediaPlayer.getDuration();
  391 +
  392 + // Update all view elements
  393 + mPlayerSeekbar.setMax((int) length);
  394 + mPlayerSeekbar.setProgress((int) time);
  395 +
  396 + if (time >= 0) {
  397 + String progress = millisToString(time, false) + "/" + millisToString(length, false);
  398 + mPlayerPosition.setText(progress);
  399 + }
  400 +
  401 + Message msg = new Message();
  402 + msg.what = UPDATE_SEEKBAR;
  403 +
  404 + if (mHandler != null)
  405 + mHandler.sendMessageDelayed(msg, 1000);
  406 + return (int) time;
  407 + }
  408 +
  409 + String millisToString(long millis, boolean text) {
  410 + boolean negative = millis < 0;
  411 + millis = Math.abs(millis);
  412 +
  413 + millis /= 1000;
  414 + int sec = (int) (millis % 60);
  415 + millis /= 60;
  416 + int min = (int) (millis % 60);
  417 + millis /= 60;
  418 + int hours = (int) millis;
  419 +
  420 + String time;
  421 + DecimalFormat format = (DecimalFormat) NumberFormat.getInstance(Locale.US);
  422 + format.applyPattern("00");
  423 + if (text) {
  424 + if (millis > 0)
  425 + time = (negative ? "-" : "") + hours + "h" + format.format(min) + "min";
  426 + else if (min > 0)
  427 + time = (negative ? "-" : "") + min + "min";
  428 + else
  429 + time = (negative ? "-" : "") + sec + "s";
  430 + } else {
  431 + if (millis > 0)
  432 + time = (negative ? "-" : "") + hours + ":" + format.format(min) + ":" + format.format(sec);
  433 + else
  434 + time = (negative ? "-" : "") + min + ":" + format.format(sec);
  435 + }
  436 + return time;
  437 + }
  438 +
  439 + private void videoPlayEnd() {
  440 + if (mMediaPlayer != null) {
  441 + mMediaPlayer.release();
  442 + mMediaPlayer = null;
  443 + }
  444 +
  445 +
  446 + mHandler = null;
  447 +
  448 + finish();
  449 + }
  450 +
  451 + private View.OnClickListener mStartBtnListener = new View.OnClickListener() {
  452 + @Override
  453 + public void onClick(View v) {
  454 + mPause = !mPause;
  455 + mHandler.removeMessages(HIDDEN_SEEKBAR);
  456 + Message msg = new Message();
  457 + msg.what = HIDDEN_SEEKBAR;
  458 + mHandler.sendMessageDelayed(msg, 3000);
  459 + if (mPause) {
  460 + mMediaPlayer.pause();
  461 + } else {
  462 + mMediaPlayer.start();
  463 + }
  464 + }
  465 + };
  466 +
  467 + private int mVideoProgress = 0;
  468 + private SeekBar.OnSeekBarChangeListener mSeekBarListener = new SeekBar.OnSeekBarChangeListener() {
  469 + @Override
  470 + public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
  471 + if (fromUser) {
  472 + mVideoProgress = progress;
  473 + mHandler.removeMessages(HIDDEN_SEEKBAR);
  474 + Message msg = new Message();
  475 + msg.what = HIDDEN_SEEKBAR;
  476 + mHandler.sendMessageDelayed(msg, 3000);
  477 + }
  478 + }
  479 +
  480 + @Override
  481 + public void onStartTrackingTouch(SeekBar seekBar) {
  482 +
  483 + }
  484 +
  485 + @Override
  486 + public void onStopTrackingTouch(SeekBar seekBar) {
  487 + mMediaPlayer.seekTo(mVideoProgress);
  488 + setVideoProgress(mVideoProgress);
  489 + }
  490 + };
  491 +
  492 + private View.OnTouchListener mTouchListener = new View.OnTouchListener() {
  493 + @Override
  494 + public boolean onTouch(View v, MotionEvent event) {
  495 + dealTouchEvent(v, event);
  496 + return false;
  497 + }
  498 + };
  499 +
  500 + private final Callback mSurfaceCallback = new Callback() {
  501 + @Override
  502 + public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
  503 + if (mMediaPlayer != null && mMediaPlayer.isPlaying())
  504 + mMediaPlayer.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);
  505 + }
  506 +
  507 + @Override
  508 + public void surfaceCreated(SurfaceHolder holder) {
  509 + if (mMediaPlayer != null)
  510 + mMediaPlayer.setDisplay(holder);
  511 + }
  512 +
  513 + @Override
  514 + public void surfaceDestroyed(SurfaceHolder holder) {
  515 + Log.d(TAG, "surfaceDestroyed");
  516 + if (mMediaPlayer != null) {
  517 + mMediaPlayer.setDisplay(null);
  518 + }
  519 + }
  520 + };
  521 +
  522 +
  523 + @Override
  524 + public void onClick(View view) {
  525 + mHandler.removeMessages(HIDDEN_SEEKBAR);
  526 + Message msg = new Message();
  527 + msg.what = HIDDEN_SEEKBAR;
  528 + mHandler.sendMessageDelayed(msg, 3000);
  529 + switch (view.getId()) {
  530 + case R.id.player_reload:
  531 + String mVideoUrl2 = "rtmp://live.hkstv.hk.lxdns.com/live/hks";
  532 + // 播放新的视频
  533 + mMediaPlayer.reload(mVideoUrl2, true);
  534 +
  535 + break;
  536 + case R.id.btn_rotate_player:
  537 + mMediaPlayer.setRotateDegree((rotatenum + 90) % 360);
  538 + rotatenum += 90;
  539 + break;
  540 + case R.id.btn_screen_player:
  541 + Bitmap bitmap = mMediaPlayer.getScreenShot();
  542 + savebitmap(bitmap);
  543 + if (bitmap != null) {
  544 + Toast.makeText(MediaPlayerActivity.this, "截图成功", Toast.LENGTH_SHORT).show();
  545 + }
  546 + break;
  547 + case R.id.btn_mute_player:
  548 + if (mMediaPlayer != null) {
  549 + if (mmute == false) {
  550 + mMediaPlayer.setPlayerMute(1);
  551 + mmute = true;
  552 + } else {
  553 + mMediaPlayer.setPlayerMute(0);
  554 + mmute = false;
  555 + }
  556 + }
  557 + default:
  558 + break;
  559 + }
  560 + }
  561 +
  562 + public void savebitmap(Bitmap bitmap) {
  563 + File appDir = new File(Environment.getExternalStorageDirectory(), "com.ksy.recordlib.demo.demo");
  564 + if (!appDir.exists()) {
  565 + appDir.mkdir();
  566 + }
  567 + String fileName = System.currentTimeMillis() + ".jpg";
  568 + File file = new File(appDir, fileName);
  569 + try {
  570 + FileOutputStream fos = new FileOutputStream(file);
  571 + bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
  572 + fos.flush();
  573 + fos.close();
  574 + } catch (FileNotFoundException e) {
  575 + e.printStackTrace();
  576 + } catch (IOException e) {
  577 + e.printStackTrace();
  578 + }
  579 + }
  580 +}
... ...
app/src/main/java/com/cnlive/demo/video/PlayerActivity.java deleted 100644 → 0
1   -package com.cnlive.demo.video;
2   -
3   -import android.app.Activity;
4   -import android.os.Bundle;
5   -import android.os.Handler;
6   -import android.os.Message;
7   -import android.util.Log;
8   -import android.view.View;
9   -import android.widget.ImageView;
10   -import android.widget.SeekBar;
11   -import android.widget.TextView;
12   -import android.widget.Toast;
13   -
14   -import com.cnlive.libs.video.player.CNMediaPlayerView;
15   -import com.cnlive.libs.video.util.OnPlayStateListener;
16   -import com.ksyun.media.player.IMediaPlayer;
17   -import com.ksyun.media.player.KSYMediaPlayer;
18   -
19   -import java.io.IOException;
20   -
21   -/**
22   - * Created by gujun on 2016/8/1.
23   - */
24   -public class PlayerActivity extends Activity implements View.OnClickListener, SeekBar.OnSeekBarChangeListener,OnPlayStateListener {
25   - private static final String TAG = "PlayerActivity";
26   -
27   - private final int UPDATE_SEEKBAR = 1;
28   -
29   - private boolean isPlay = false;
30   - private int mVideoProgress = 0;
31   -
32   - private CNMediaPlayerView cnPlayerView;
33   - private SeekBar videoProgressBar;
34   - private TextView videoTime;
35   - private ImageView playerPlayPause;
36   -
37   - private Handler mHandler = new Handler() {
38   - @Override
39   - public void handleMessage(Message msg) {
40   - super.handleMessage(msg);
41   - switch (msg.what) {
42   - case UPDATE_SEEKBAR:
43   - setVideoProgress(0);
44   - break;
45   - }
46   - }
47   - };
48   -
49   - private IMediaPlayer.OnBufferingUpdateListener mOnBufferingUpdateListener = new IMediaPlayer.OnBufferingUpdateListener() {
50   -
51   - @Override
52   - public void onBufferingUpdate(IMediaPlayer iMediaPlayer, int percent) {
53   - long duration = cnPlayerView.getDuration();
54   - long progress = duration * percent / 100;
55   - if(videoProgressBar != null) videoProgressBar.setSecondaryProgress((int)progress);
56   - }
57   - };
58   -
59   - private IMediaPlayer.OnPreparedListener mOnPrepareListener = new IMediaPlayer.OnPreparedListener() {
60   - @Override
61   - public void onPrepared(IMediaPlayer iMediaPlayer) {
62   - setPlayState(true);
63   - isPlay = true;
64   - }
65   - };
66   -
67   - private IMediaPlayer.OnCompletionListener mOnCompletionListener = new IMediaPlayer.OnCompletionListener() {
68   - @Override
69   - public void onCompletion(IMediaPlayer iMediaPlayer) {
70   - Toast.makeText(PlayerActivity.this, "OnCompletionListener, play complete.", Toast.LENGTH_LONG).show();
71   - isPlay = false;
72   - if (playerPlayPause != null)
73   - playerPlayPause.setImageResource(R.drawable.cnlive_video_play);
74   - if (mHandler != null)
75   - mHandler.removeMessages(UPDATE_SEEKBAR);
76   - }
77   - };
78   -
79   - private IMediaPlayer.OnInfoListener mOnInfoListener = new IMediaPlayer.OnInfoListener() {
80   - @Override
81   - public boolean onInfo(IMediaPlayer iMediaPlayer, int what, int extra) {
82   - switch (what) {
83   - case KSYMediaPlayer.MEDIA_INFO_BUFFERING_START:
84   - Log.d(TAG, "Buffering Start.");
85   - break;
86   - case KSYMediaPlayer.MEDIA_INFO_BUFFERING_END:
87   - Log.d(TAG, "Buffering End.");
88   - break;
89   - case KSYMediaPlayer.MEDIA_INFO_AUDIO_RENDERING_START:
90   - Toast.makeText(PlayerActivity.this, "Audio Rendering Start", Toast.LENGTH_SHORT).show();
91   - break;
92   - case KSYMediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START:
93   - Toast.makeText(PlayerActivity.this, "Video Rendering Start", Toast.LENGTH_SHORT).show();
94   - break;
95   - case KSYMediaPlayer.MEDIA_INFO_RELOADED:
96   - Log.d(TAG, "Reload Success");
97   - setPlayState(true);
98   - isPlay = true;
99   - break;
100   - }
101   - return false;
102   - }
103   - };
104   -
105   - private IMediaPlayer.OnErrorListener mOnErrorListener = new IMediaPlayer.OnErrorListener() {
106   - @Override
107   - public boolean onError(IMediaPlayer mp, int what, int extra) {
108   - switch (what) {
109   - case KSYMediaPlayer.MEDIA_ERROR_UNKNOWN:
110   - Log.e(TAG, "OnErrorListener, Error Unknown:" + what + ",extra:" + extra);
111   - break;
112   - default:
113   - Log.e(TAG, "OnErrorListener, Error:" + what + ",extra:" + extra);
114   - }
115   -
116   - return false;
117   - }
118   - };
119   -
120   - private IMediaPlayer.OnSeekCompleteListener mOnSeekCompleteListener = new IMediaPlayer.OnSeekCompleteListener() {
121   - @Override
122   - public void onSeekComplete(IMediaPlayer iMediaPlayer) {
123   -
124   - }
125   - };
126   -
127   - private IMediaPlayer.OnVideoSizeChangedListener mOnVideoSizeChangedListener = new IMediaPlayer.OnVideoSizeChangedListener() {
128   - @Override
129   - public void onVideoSizeChanged(IMediaPlayer mp, int width, int height, int sarNum, int sarDen) {
130   -
131   - }
132   - };
133   -
134   - @Override
135   - protected void onCreate(Bundle savedInstanceState) {
136   - super.onCreate(savedInstanceState);
137   - setContentView(R.layout.activity_cnplayer);
138   - init();
139   - }
140   -
141   - private void init() {
142   - videoProgressBar = (SeekBar) findViewById(R.id.videoProgressBar);
143   - if (videoProgressBar != null) videoProgressBar.setOnSeekBarChangeListener(this);
144   - videoTime = (TextView) findViewById(R.id.time);
145   - playerPlayPause = (ImageView) findViewById(R.id.playerPlayPause);
146   - playerPlayPause.setOnClickListener(this);
147   - cnPlayerView = (CNMediaPlayerView) findViewById(R.id.cnPlayerView);
148   - cnPlayerView.setKeepScreenOn(true);
149   - initVideo();
150   - }
151   -
152   - private void initVideo() {
153   - cnPlayerView.build();
154   - cnPlayerView.setOnBufferingUpdateListener(mOnBufferingUpdateListener);
155   - cnPlayerView.setOnCompletionListener(mOnCompletionListener);
156   - cnPlayerView.setOnPreparedListener(mOnPrepareListener);
157   - cnPlayerView.setOnInfoListener(mOnInfoListener);
158   - cnPlayerView.setOnErrorListener(mOnErrorListener);
159   - cnPlayerView.setOnSeekCompleteListener(mOnSeekCompleteListener);
160   - cnPlayerView.setOnVideoSizeChangedListener(mOnVideoSizeChangedListener);
161   - cnPlayerView.setOnPlayStateListener(this);
162   - cnPlayerView.setScreenOnWhilePlaying(true);
163   - cnPlayerView.setBufferTimeMax(5);
164   - try {
165   - cnPlayerView.setDataSource("http://img.ksbbs.com/asset/Mon_1605/0ec8cc80112a2d6.mp4");
166   - cnPlayerView.prepareAsync();
167   - } catch (IOException e) {
168   - e.printStackTrace();
169   - }
170   - }
171   -
172   - @Override
173   - protected void onPause() {
174   - super.onPause();
175   - if (cnPlayerView != null && cnPlayerView.isPlaying()) {
176   - setPlayState(false);
177   - }
178   - }
179   -
180   - @Override
181   - protected void onResume() {
182   - super.onResume();
183   - if (cnPlayerView != null && !cnPlayerView.isPlaying() && isPlay) {
184   - setPlayState(true);
185   - }
186   - }
187   -
188   - @Override
189   - protected void onDestroy() {
190   - if (mHandler != null)
191   - mHandler.removeMessages(UPDATE_SEEKBAR);
192   - stopPlayBack();
193   - super.onDestroy();
194   - }
195   -
196   - private void stopPlayBack() {
197   - if (cnPlayerView != null) {
198   - cnPlayerView.stop();
199   - cnPlayerView.release();
200   - }
201   - }
202   -
203   - public int setVideoProgress(int currentProgress) {
204   -
205   - if (cnPlayerView == null)
206   - return -1;
207   -
208   - long time = currentProgress > 0 ? currentProgress : cnPlayerView.getCurrentPosition();
209   - long length = cnPlayerView.getDuration();
210   -
211   - // Update all view elements
212   - if (videoProgressBar != null) {
213   - videoProgressBar.setMax((int) length);
214   - videoProgressBar.setProgress((int) time);
215   - }
216   -
217   - if (time >= 0) {
218   - String progress = Util.stringForTime(time / 1000) + "/" + Util.stringForTime(length / 1000);
219   - if (videoTime != null)
220   - videoTime.setText(progress);
221   - }
222   -
223   - Message msg = new Message();
224   - msg.what = UPDATE_SEEKBAR;
225   -
226   - if (mHandler != null) {
227   - mHandler.removeMessages(UPDATE_SEEKBAR);
228   - mHandler.sendMessageDelayed(msg, 1000);
229   - }
230   -
231   - return (int) time;
232   - }
233   -
234   - @Override
235   - public void onClick(View v) {
236   - switch (v.getId()){
237   - case R.id.playerPlayPause:
238   - if (cnPlayerView != null) {
239   - isPlay = !isPlay;
240   - setPlayState(isPlay);
241   - }
242   - break;
243   - }
244   - }
245   -
246   - private void setPlayState(boolean isPlay) {
247   - if (isPlay) cnPlayerView.start();
248   - else cnPlayerView.pause();
249   - }
250   -
251   - protected void updateProgress(boolean isPlay) {
252   - if (isPlay) setVideoProgress(0);
253   - else if (mHandler != null) mHandler.removeMessages(UPDATE_SEEKBAR);
254   - }
255   -
256   - @Override
257   - public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
258   - if (fromUser) {
259   - mVideoProgress = progress;
260   - }
261   - }
262   -
263   - @Override
264   - public void onStartTrackingTouch(SeekBar seekBar) {
265   -
266   - }
267   -
268   - @Override
269   - public void onStopTrackingTouch(SeekBar seekBar) {
270   - cnPlayerView.seekTo(mVideoProgress);
271   - setVideoProgress(mVideoProgress);
272   - }
273   -
274   - @Override
275   - public void onPlayState(boolean isPlay) {
276   - if (isPlay) playerPlayPause.setImageResource(R.drawable.cnlive_video_pause);
277   - else playerPlayPause.setImageResource(R.drawable.cnlive_video_play);
278   - updateProgress(isPlay);
279   - }
280   -}
app/src/main/java/com/cnlive/demo/video/Util.java deleted 100644 → 0
1   -package com.cnlive.demo.video;
2   -
3   -import java.util.Locale;
4   -
5   -/**
6   - * Created by xiansong on 2016/7/4.
7   - */
8   -public class Util {
9   -
10   - public static String stringForTime(long second) {
11   - int totalSeconds = (int) second;
12   -
13   - int seconds = totalSeconds % 60;
14   - int minutes = (totalSeconds / 60) % 60;
15   - int hours = totalSeconds / 3600;
16   -
17   - if (hours > 0) {
18   - return String.format(Locale.getDefault(), "%d:%02d:%02d", hours, minutes, seconds);
19   - } else {
20   - return String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds);
21   - }
22   - }
23   -
24   -
25   -
26   -}
app/src/main/java/com/cnlive/demo/video/VideoViewActivity.java 0 → 100644
  1 +package com.cnlive.demo.video;
  2 +
  3 +import android.app.Activity;
  4 +import android.os.Bundle;
  5 +
  6 +import com.cnlive.demo.video.model.ProgramVod;
  7 +import com.cnlive.libs.video.video.VideoView;
  8 +
  9 +
  10 +/**
  11 + * Created by xiansong on 2016/10/8.
  12 + */
  13 +
  14 +public class VideoViewActivity extends Activity {
  15 +
  16 + protected VideoView videoView;
  17 +
  18 + @Override
  19 + protected void onCreate(Bundle savedInstanceState) {
  20 + super.onCreate(savedInstanceState);
  21 + setContentView(R.layout.activity_videoview);
  22 + initPlay();
  23 + }
  24 +
  25 + @Override
  26 + protected void onPause() {
  27 + super.onPause();
  28 + videoView.pause();
  29 + }
  30 +
  31 + @Override
  32 + protected void onResume() {
  33 + super.onResume();
  34 + videoView.start();
  35 + }
  36 +
  37 + @Override
  38 + protected void onDestroy() {
  39 + super.onDestroy();
  40 + videoView.release();
  41 + }
  42 +
  43 + private void initPlay(){
  44 + videoView = (VideoView) findViewById(R.id.videoview);
  45 +
  46 + //视讯云 直播
  47 +// videoView.setDataSource(new ProgramLive(),false);
  48 +
  49 + //视讯云 点播
  50 + videoView.setDataSourceByAuth(new ProgramVod(), false);
  51 +
  52 + //本地视频
  53 +// try {
  54 +// videoView.setDataSource("rtmp://live.hkstv.hk.lxdns.com/live/hks");
  55 +// } catch (IOException e) {
  56 +// e.printStackTrace();
  57 +// }
  58 + }
  59 +
  60 +
  61 +}
... ...
app/src/main/java/com/cnlive/demo/video/model/ProgramLive.java 0 → 100644
  1 +package com.cnlive.demo.video.model;
  2 +
  3 +import com.cnlive.libs.util.Config;
  4 +import com.cnlive.libs.video.video.base.IDataSource;
  5 +
  6 +/**
  7 + * Created by xiansong on 2016/11/23.
  8 + */
  9 +
  10 +public class ProgramLive implements IDataSource {
  11 +
  12 + //返回直播视频ID
  13 + @Override
  14 + public String getId() {
  15 + return "news";
  16 + }
  17 +
  18 + //返回视频类型 live
  19 + @Override
  20 + public String getType() {
  21 + return Config.TYPE_LIVE;
  22 + }
  23 +
  24 + //返回视频码率
  25 + @Override
  26 + public String getRate() {
  27 + return Config.TYPE_PLAY_RATE_2;
  28 + }
  29 +}
... ...
app/src/main/java/com/cnlive/demo/video/model/ProgramVod.java 0 → 100644
  1 +package com.cnlive.demo.video.model;
  2 +
  3 +import com.cnlive.libs.util.Config;
  4 +import com.cnlive.libs.video.video.base.IDataSource;
  5 +
  6 +/**
  7 + * Created by xiansong on 2016/11/23.
  8 + */
  9 +
  10 +public class ProgramVod implements IDataSource {
  11 +
  12 + //返回点播视频ID
  13 + @Override
  14 + public String getId() {
  15 + return "56328";
  16 + }
  17 +
  18 + //返回视频类型 vod
  19 + @Override
  20 + public String getType() {
  21 + return Config.TYPE_VOD;
  22 + }
  23 +
  24 + //返回视频码率
  25 + @Override
  26 + public String getRate() {
  27 + return Config.TYPE_PLAY_RATE_2;
  28 + }
  29 +}
... ...
app/src/main/jniLibs/arm64-v8a/libksyplayer.so
No preview for this file type
app/src/main/jniLibs/armeabi-v7a/libksyplayer.so
No preview for this file type
app/src/main/jniLibs/armeabi/libksyplayer.so 0 → 100644
No preview for this file type
app/src/main/jniLibs/x86/libksyplayer.so
No preview for this file type
app/src/main/jniLibs/x86_64/libksyplayer.so 0 → 100644
No preview for this file type
app/src/main/res/layout/activity_cnplayer.xml deleted 100644 → 0
1   -<?xml version="1.0" encoding="utf-8"?>
2   -<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3   - android:layout_width="match_parent"
4   - android:layout_height="match_parent"
5   - android:orientation="vertical">
6   -
7   - <RelativeLayout
8   - android:layout_width="match_parent"
9   - android:layout_height="260dp"
10   - android:background="@android:color/black">
11   -
12   - <com.cnlive.libs.video.player.CNMediaPlayerView
13   - android:id="@+id/cnPlayerView"
14   - android:layout_width="match_parent"
15   - android:layout_height="match_parent"
16   - android:layout_centerInParent="true" />
17   - </RelativeLayout>
18   -
19   - <RelativeLayout
20   - android:id="@+id/mediaControllerBottomLayout"
21   - android:layout_width="match_parent"
22   - android:layout_height="58dp"
23   - android:layout_alignParentBottom="true"
24   - android:background="#88000000">
25   -
26   - <ImageView
27   - android:id="@+id/playerPlayPause"
28   - android:layout_width="30dp"
29   - android:layout_height="30dp"
30   - android:src="@drawable/cnlive_video_pause"
31   - android:layout_centerVertical="true"/>
32   -
33   - <SeekBar
34   - android:id="@+id/videoProgressBar"
35   - android:layout_width="match_parent"
36   - android:layout_height="wrap_content"
37   - android:layout_centerVertical="true"
38   - android:layout_toRightOf="@+id/playerPlayPause"
39   - android:maxHeight="4dp"
40   - android:minHeight="4dp"
41   - android:layout_toLeftOf="@+id/time"
42   - android:progressDrawable="@drawable/cnlive_video_seekbar"
43   - android:thumb="@drawable/cnlive_video_seekbar_progress"
44   - android:layout_marginRight="8dp"
45   - android:layout_marginLeft="8dp"
46   - android:thumbOffset="0dip"/>
47   -
48   - <TextView
49   - android:id="@+id/time"
50   - android:layout_width="wrap_content"
51   - android:layout_height="wrap_content"
52   - android:layout_marginBottom="8dp"
53   - android:text="00:00:00"
54   - android:layout_marginRight="10dp"
55   - android:textSize="16sp"
56   - android:textColor="#ffffff"
57   - android:layout_alignParentRight="true"
58   - android:layout_centerVertical="true"/>
59   -
60   - </RelativeLayout>
61   -
62   -</RelativeLayout>
63 0 \ No newline at end of file
app/src/main/res/layout/activity_cnplayer_new.xml deleted 100644 → 0
1   -<?xml version="1.0" encoding="utf-8"?>
2   -<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3   - android:layout_width="match_parent"
4   - android:layout_height="match_parent"
5   - android:orientation="vertical">
6   -
7   - <RelativeLayout
8   - android:layout_width="match_parent"
9   - android:layout_height="260dp"
10   - android:background="@android:color/black">
11   -
12   - <com.cnlive.libs.video.player.CNMediaPlayerView
13   - android:id="@+id/cnPlayerView"
14   - android:layout_width="match_parent"
15   - android:layout_height="match_parent"
16   - android:layout_centerInParent="true" />
17   - </RelativeLayout>
18   -
19   - <Button
20   - android:id="@+id/retry"
21   - android:layout_width="wrap_content"
22   - android:layout_height="wrap_content"
23   - android:text="重试按钮"/>
24   -
25   - <Button
26   - android:id="@+id/newActivity"
27   - android:layout_width="wrap_content"
28   - android:layout_height="wrap_content"
29   - android:layout_toRightOf="@+id/retry"
30   - android:text="开启新窗口"/>
31   - <RelativeLayout
32   - android:id="@+id/mediaControllerBottomLayout"
33   - android:layout_width="match_parent"
34   - android:layout_height="58dp"
35   - android:layout_alignParentBottom="true"
36   - android:background="@drawable/cnlive_video_bottom_bg">
37   -
38   - <ImageView
39   - android:id="@+id/playerPlayPause"
40   - android:layout_width="30dp"
41   - android:layout_height="30dp"
42   - android:src="@drawable/cnlive_video_pause"
43   - android:layout_centerVertical="true"/>
44   -
45   - <SeekBar
46   - android:id="@+id/videoProgressBar"
47   - android:layout_width="match_parent"
48   - android:layout_height="wrap_content"
49   - android:layout_centerVertical="true"
50   - android:layout_toRightOf="@+id/playerPlayPause"
51   - android:maxHeight="4dp"
52   - android:minHeight="4dp"
53   - android:layout_toLeftOf="@+id/time"
54   - android:progressDrawable="@drawable/cnlive_video_seekbar"
55   - android:thumb="@drawable/cnlive_video_seekbar_progress"
56   - android:layout_marginRight="8dp"
57   - android:layout_marginLeft="8dp"
58   - android:thumbOffset="0dip"/>
59   -
60   - <TextView
61   - android:id="@+id/time"
62   - android:layout_width="wrap_content"
63   - android:layout_height="wrap_content"
64   - android:layout_marginBottom="8dp"
65   - android:text="00:00:00"
66   - android:layout_marginRight="10dp"
67   - android:textSize="16sp"
68   - android:textColor="#ffffff"
69   - android:layout_alignParentRight="true"
70   - android:layout_centerVertical="true"/>
71   -
72   - </RelativeLayout>
73   -
74   -</RelativeLayout>
75 0 \ No newline at end of file
app/src/main/res/layout/activity_main.xml
... ... @@ -13,12 +13,12 @@
13 13 android:id="@+id/play"
14 14 android:layout_width="wrap_content"
15 15 android:layout_height="wrap_content"
16   - android:text="播放普通视频" />
  16 + android:text="视频基本操作" />
17 17  
18 18 <Button android:id="@+id/videoId"
19 19 android:layout_width="wrap_content"
20 20 android:layout_height="wrap_content"
21   - android:text="播放视讯云视频 点播/直播 ID"
  21 + android:text="播放视讯云视频"
22 22 android:layout_below="@+id/play"/>
23 23  
24 24 <Button
... ...
app/src/main/res/layout/activity_mediaplayer.xml 0 → 100644
  1 +<?xml version="1.0" encoding="utf-8"?>
  2 +<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3 + android:layout_width="match_parent"
  4 + android:layout_height="match_parent"
  5 + xmlns:tools="http://schemas.android.com/tools"
  6 + android:background="#000000">
  7 +
  8 + <SurfaceView
  9 + android:id="@+id/player_surface"
  10 + android:layout_width="match_parent"
  11 + android:layout_height="match_parent"
  12 + android:layout_centerInParent="true"/>
  13 +
  14 + <TextureView
  15 + android:id="@+id/player_texture"
  16 + android:layout_width="match_parent"
  17 + android:layout_height="match_parent"
  18 + tools:targetApi="ice_cream_sandwich"
  19 + android:layout_centerInParent="true"
  20 + android:visibility="gone"/>
  21 +
  22 + <TextView
  23 + android:id="@+id/loading_text"
  24 + android:layout_width="wrap_content"
  25 + android:layout_height="wrap_content"
  26 + android:layout_centerInParent="true"
  27 + android:textColor="#FFCC00"/>
  28 +
  29 + <RelativeLayout
  30 + android:id="@+id/player_qos"
  31 + android:layout_width="wrap_content"
  32 + android:layout_height="wrap_content"
  33 + android:visibility="visible"
  34 + android:layout_alignParentTop="true"
  35 + android:layout_alignParentLeft="true">
  36 +
  37 + <TextView
  38 + android:id="@+id/player_cpu"
  39 + android:layout_width="wrap_content"
  40 + android:layout_height="wrap_content"
  41 + android:textSize="10dp"
  42 + android:textColor="#FFCC00"/>
  43 +
  44 + <TextView
  45 + android:id="@+id/player_mem"
  46 + android:layout_width="wrap_content"
  47 + android:layout_height="wrap_content"
  48 + android:textSize="10dp"
  49 + android:layout_below="@+id/player_cpu"
  50 + android:textColor="#FFCC00"/>
  51 +
  52 + <TextView
  53 + android:id="@+id/player_re"
  54 + android:layout_width="wrap_content"
  55 + android:layout_height="wrap_content"
  56 + android:layout_below="@+id/player_mem"
  57 + android:textSize="10dp"
  58 + android:textColor="#FFCC00"/>
  59 +
  60 + <TextView
  61 + android:id="@+id/player_br"
  62 + android:layout_width="wrap_content"
  63 + android:layout_height="wrap_content"
  64 + android:textColor="#FFCC00"
  65 + android:layout_below="@+id/player_re"
  66 + android:textSize="10dp"/>
  67 +
  68 + <TextView
  69 + android:id="@+id/player_fr"
  70 + android:layout_width="wrap_content"
  71 + android:layout_height="wrap_content"
  72 + android:layout_below="@+id/player_br"
  73 + android:textColor="#FFCC00"
  74 + android:textSize="10dp"/>
  75 +
  76 + <TextView
  77 + android:id="@+id/player_video_time"
  78 + android:layout_width="wrap_content"
  79 + android:layout_height="wrap_content"
  80 + android:layout_below="@+id/player_fr"
  81 + android:textColor="#FFCC00"
  82 + android:textSize="10dp"/>
  83 +
  84 + <TextView
  85 + android:id="@+id/player_audio_time"
  86 + android:layout_width="wrap_content"
  87 + android:layout_height="wrap_content"
  88 + android:layout_below="@+id/player_video_time"
  89 + android:textColor="#FFCC00"
  90 + android:textSize="10dp"/>
  91 +
  92 + <TextView
  93 + android:id="@+id/player_ip"
  94 + android:layout_width="wrap_content"
  95 + android:layout_height="wrap_content"
  96 + android:layout_below="@+id/player_audio_time"
  97 + android:textColor="#FFCC00"
  98 + android:textSize="10dp"/>
  99 +
  100 + <TextView
  101 + android:id="@+id/player_sdk_version"
  102 + android:layout_width="wrap_content"
  103 + android:layout_height="wrap_content"
  104 + android:layout_below="@+id/player_ip"
  105 + android:textColor="#FFCC00"
  106 + android:textSize="10dp"/>
  107 +
  108 + <TextView
  109 + android:id="@+id/player_dns_time"
  110 + android:layout_width="wrap_content"
  111 + android:layout_height="wrap_content"
  112 + android:layout_below="@+id/player_sdk_version"
  113 + android:textColor="#FFCC00"
  114 + android:textSize="10dp"/>
  115 +
  116 + <TextView
  117 + android:id="@+id/player_http_connection_time"
  118 + android:layout_width="wrap_content"
  119 + android:layout_height="wrap_content"
  120 + android:layout_below="@+id/player_dns_time"
  121 + android:textColor="#FFCC00"
  122 + android:textSize="10dp"/>
  123 + </RelativeLayout>
  124 +
  125 + <RelativeLayout
  126 + android:id="@+id/topPanel_player"
  127 + android:layout_width="wrap_content"
  128 + android:layout_height="wrap_content"
  129 + android:layout_alignParentTop="true">
  130 +
  131 +
  132 + <Button
  133 + android:id="@+id/player_reload"
  134 + android:layout_width="wrap_content"
  135 + android:layout_height="wrap_content"
  136 + android:textSize="10dp"
  137 + android:text="reload"
  138 + android:layout_toRightOf="@+id/player_scale" />
  139 +
  140 + <Button
  141 + android:id="@+id/btn_rotate_player"
  142 + android:layout_width="wrap_content"
  143 + android:layout_height="wrap_content"
  144 + android:layout_alignParentRight="true"
  145 + android:layout_marginRight="10dp"
  146 + android:textSize="10dp"
  147 + android:text="旋转"/>
  148 +
  149 + <Button
  150 + android:id="@+id/btn_screen_player"
  151 + android:layout_width="wrap_content"
  152 + android:layout_height="wrap_content"
  153 + android:layout_toLeftOf="@+id/btn_rotate_player"
  154 + android:textSize="10dp"
  155 + android:text="截屏"/>
  156 +
  157 + <Button
  158 + android:id="@+id/btn_mute_player"
  159 + android:layout_width="wrap_content"
  160 + android:layout_height="wrap_content"
  161 + android:layout_marginLeft="10dp"
  162 + android:layout_below="@+id/player_scale"
  163 + android:textSize="10dp"
  164 + android:text="静音"/>
  165 +
  166 + <Button
  167 + android:id="@+id/player_scale"
  168 + android:layout_width="wrap_content"
  169 + android:layout_height="wrap_content"
  170 + android:text="ScaleVideo"
  171 + android:textSize="10dp"
  172 + android:layout_alignParentTop="true"
  173 + android:layout_alignLeft="@+id/btn_mute_player"
  174 + android:layout_alignStart="@+id/btn_mute_player" />
  175 +
  176 +
  177 + </RelativeLayout>
  178 +
  179 +
  180 + <LinearLayout
  181 + android:id="@+id/player_panel"
  182 + android:layout_width="match_parent"
  183 + android:layout_height="wrap_content"
  184 + android:layout_alignParentBottom="true">
  185 +
  186 + <ImageView
  187 + android:id="@+id/player_start"
  188 + android:layout_width="50dp"
  189 + android:layout_height="50dp"
  190 + android:background="@drawable/cnlive_video_pause"/>
  191 +
  192 + <RelativeLayout
  193 + android:layout_width="match_parent"
  194 + android:layout_height="wrap_content">
  195 +
  196 +
  197 + <TextView
  198 + android:id="@+id/player_time"
  199 + android:layout_width="wrap_content"
  200 + android:layout_height="wrap_content"
  201 + android:text="00:00/00:00"
  202 + android:layout_alignParentLeft="true"/>
  203 +
  204 + <SeekBar
  205 + android:id="@+id/player_seekbar"
  206 + android:layout_width="match_parent"
  207 + android:layout_height="wrap_content"
  208 + android:layout_below="@+id/player_time"
  209 + android:focusable="true"/>
  210 +
  211 + </RelativeLayout>
  212 +
  213 + </LinearLayout>
  214 +
  215 +</RelativeLayout>
0 216 \ No newline at end of file
... ...
app/src/main/res/layout/activity_videoview.xml 0 → 100644
  1 +<?xml version="1.0" encoding="utf-8"?>
  2 +<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3 + android:layout_width="match_parent"
  4 + android:layout_height="match_parent"
  5 + android:orientation="vertical">
  6 +
  7 + <RelativeLayout
  8 + android:layout_width="match_parent"
  9 + android:layout_height="match_parent"
  10 + android:background="@android:color/black">
  11 +
  12 + <com.cnlive.libs.video.video.VideoView
  13 + android:id="@+id/videoview"
  14 + android:layout_width="match_parent"
  15 + android:layout_height="match_parent"
  16 + android:layout_centerInParent="true" />
  17 + </RelativeLayout>
  18 +
  19 + <Button
  20 + android:id="@+id/switch_view"
  21 + android:layout_width="wrap_content"
  22 + android:layout_height="wrap_content"
  23 + android:onClick="onClick" />
  24 +</RelativeLayout>
0 25 \ No newline at end of file
... ...