├── .gitignore ├── .idea ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── encodings.xml ├── gradle.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── VideoList ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── trs │ │ └── videolist │ │ └── ApplicationTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── trs │ │ │ └── videolist │ │ │ ├── CustomMediaContoller.java │ │ │ ├── VideoPlayView.java │ │ │ └── media │ │ │ ├── IMediaController.java │ │ │ ├── IRenderView.java │ │ │ ├── IjkVideoView.java │ │ │ ├── InfoHudViewHolder.java │ │ │ ├── MeasureHelper.java │ │ │ ├── MediaPlayerCompat.java │ │ │ ├── MediaPlayerService.java │ │ │ ├── Settings.java │ │ │ ├── SurfaceRenderView.java │ │ │ ├── TableLayoutBinder.java │ │ │ └── TextureRenderView.java │ └── res │ │ ├── drawable │ │ ├── app_video_center_bg.xml │ │ ├── bg_seek.xml │ │ └── bg_thumb.xml │ │ ├── layout │ │ ├── media_contoller.xml │ │ ├── table_media_info.xml │ │ ├── table_media_info_row1.xml │ │ ├── table_media_info_row2.xml │ │ ├── table_media_info_section.xml │ │ └── view_video_item.xml │ │ ├── mipmap-xhdpi │ │ ├── full_screen_icon.png │ │ ├── ic_launcher.png │ │ ├── sound_mult_icon.png │ │ ├── sound_open_icon.png │ │ ├── video_play_btn.png │ │ └── video_stop_btn.png │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ ├── strings_pref.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── trs │ └── videolist │ └── ExampleUnitTest.java ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── zhuguohui │ │ └── videodemo │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── zhuguohui │ │ │ └── videodemo │ │ │ ├── MyApp.java │ │ │ ├── activity │ │ │ ├── FullscreenActivity.java │ │ │ ├── MainActivity.java │ │ │ ├── NewsDetailActivity.java │ │ │ └── VideoDetailActivity.java │ │ │ ├── adapter │ │ │ ├── NewsListAdapter.java │ │ │ ├── NewsTabAdapter.java │ │ │ └── VideoAdapter.java │ │ │ ├── bean │ │ │ ├── VideoItem.java │ │ │ └── VideoPage.java │ │ │ ├── fragment │ │ │ ├── NewsListFragment.java │ │ │ ├── VideoListFragment.java │ │ │ └── base │ │ │ │ └── BaseListFragment.java │ │ │ ├── rx │ │ │ └── RxBus.java │ │ │ ├── service │ │ │ └── NetworkStateService.java │ │ │ ├── util │ │ │ ├── AppUtil.java │ │ │ └── ToastUtil.java │ │ │ ├── video │ │ │ ├── VideoPlayManager.java │ │ │ └── ViedoPlayChecker.java │ │ │ └── view │ │ │ └── DividerItemDecoration.java │ └── res │ │ ├── drawable-xxhdpi │ │ ├── bg_dialog.xml │ │ ├── bg_dialog_left_bottom.xml │ │ ├── bg_dialog_right_bottom.xml │ │ ├── ic_waring.png │ │ ├── shape_dialog_left_bottom_normal.xml │ │ ├── shape_dialog_left_bottom_press.xml │ │ ├── shape_dialog_right_bottom_normal.xml │ │ └── shape_dialog_right_bottom_press.xml │ │ ├── drawable │ │ ├── bg_item_common.xml │ │ └── bg_toast.xml │ │ ├── layout │ │ ├── activity_fullscreen.xml │ │ ├── activity_main.xml │ │ ├── activity_news_detail.xml │ │ ├── activity_video_detail.xml │ │ ├── fragment_play_net_allow.xml │ │ ├── fragment_simple_news_list.xml │ │ ├── item_news.xml │ │ ├── list_item_video.xml │ │ ├── view_small_holder.xml │ │ └── view_toast.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_video_close.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ ├── raw │ │ └── video_list.json │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── zhuguohui │ └── videodemo │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── version.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 1.7 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /VideoList/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /VideoList/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion rootProject.ext.android.compileSdkVersion 5 | buildToolsVersion rootProject.ext.android.buildToolsVersion 6 | 7 | defaultConfig { 8 | minSdkVersion rootProject.ext.android.minSdkVersion 9 | targetSdkVersion rootProject.ext.android.targetSdkVersion 10 | versionCode rootProject.ext.android.versionCode 11 | versionName rootProject.ext.android.versionName 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | } 20 | 21 | dependencies { 22 | compile fileTree(dir: 'libs', include: ['*.jar']) 23 | testCompile deps.jUnit 24 | compile deps.appcompatV7 25 | 26 | compile 'tv.danmaku.ijk.media:ijkplayer-java:0.5.1' 27 | compile 'tv.danmaku.ijk.media:ijkplayer-armv7a:0.5.1' 28 | 29 | } 30 | -------------------------------------------------------------------------------- /VideoList/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in D:\Android_SDK/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /VideoList/src/androidTest/java/com/trs/videolist/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.trs.videolist; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /VideoList/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /VideoList/src/main/java/com/trs/videolist/CustomMediaContoller.java: -------------------------------------------------------------------------------- 1 | package com.trs.videolist; 2 | 3 | import android.app.Activity; 4 | import android.content.Context; 5 | import android.content.pm.ActivityInfo; 6 | import android.graphics.Bitmap; 7 | import android.graphics.PointF; 8 | import android.graphics.Rect; 9 | import android.media.AudioManager; 10 | import android.os.Handler; 11 | import android.os.Message; 12 | import android.util.DisplayMetrics; 13 | import android.util.Log; 14 | import android.view.MotionEvent; 15 | import android.view.Surface; 16 | import android.view.View; 17 | import android.widget.ImageView; 18 | import android.widget.MediaController; 19 | import android.widget.ProgressBar; 20 | import android.widget.SeekBar; 21 | import android.widget.TextView; 22 | 23 | import com.trs.videolist.media.IMediaController; 24 | import com.trs.videolist.media.IjkVideoView; 25 | 26 | import tv.danmaku.ijk.media.player.IMediaPlayer; 27 | 28 | /** 29 | * @Description ${控制器} 30 | */ 31 | public class CustomMediaContoller implements IMediaController { 32 | 33 | 34 | private static final int SET_VIEW_HIDE = 1; 35 | private static final int TIME_OUT = 5000; 36 | private static final int MESSAGE_SHOW_PROGRESS = 2; 37 | private static final int PAUSE_IMAGE_HIDE = 3; 38 | private View itemView; 39 | private View view; 40 | private boolean isShow; 41 | private IjkVideoView videoView; 42 | private boolean isScroll; 43 | 44 | private SeekBar seekBar; 45 | AudioManager audioManager; 46 | private ProgressBar progressBar; 47 | 48 | private boolean isSound; 49 | private boolean isDragging; 50 | 51 | private boolean isPause; 52 | 53 | private boolean isShowContoller; 54 | private ImageView sound, full, play; 55 | private TextView time, allTime; 56 | private PointF lastPoint; 57 | private Context context; 58 | private ImageView pauseImage; 59 | private Bitmap bitmap; 60 | private Handler handler = new Handler() { 61 | @Override 62 | public void handleMessage(Message msg) { 63 | super.handleMessage(msg); 64 | switch (msg.what) { 65 | case SET_VIEW_HIDE: 66 | isShow = false; 67 | itemView.setVisibility(View.GONE); 68 | break; 69 | case MESSAGE_SHOW_PROGRESS: 70 | setProgress(); 71 | if (!isDragging && isShow) { 72 | msg = obtainMessage(MESSAGE_SHOW_PROGRESS); 73 | sendMessageDelayed(msg, 1000); 74 | } 75 | break; 76 | case PAUSE_IMAGE_HIDE: 77 | pauseImage.setVisibility(View.GONE); 78 | break; 79 | } 80 | } 81 | }; 82 | 83 | public CustomMediaContoller(Context context, View view) { 84 | this.view = view; 85 | itemView = view.findViewById(R.id.media_contoller); 86 | this.videoView = (IjkVideoView) view.findViewById(R.id.main_video); 87 | itemView.setVisibility(View.GONE); 88 | isShow = false; 89 | isDragging = false; 90 | 91 | isShowContoller = true; 92 | this.context = context; 93 | audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); 94 | initView(); 95 | initAction(); 96 | } 97 | 98 | public void initView() { 99 | progressBar = (ProgressBar) view.findViewById(R.id.loading); 100 | seekBar = (SeekBar) itemView.findViewById(R.id.seekbar); 101 | allTime = (TextView) itemView.findViewById(R.id.all_time); 102 | time = (TextView) itemView.findViewById(R.id.time); 103 | full = (ImageView) itemView.findViewById(R.id.full); 104 | sound = (ImageView) itemView.findViewById(R.id.sound); 105 | play = (ImageView) itemView.findViewById(R.id.player_btn); 106 | pauseImage = (ImageView) view.findViewById(R.id.pause_image); 107 | } 108 | 109 | public void start() { 110 | pauseImage.setVisibility(View.GONE); 111 | itemView.setVisibility(View.GONE); 112 | play.setImageResource(R.mipmap.video_stop_btn); 113 | progressBar.setVisibility(View.VISIBLE); 114 | } 115 | 116 | public void pause() { 117 | play.setImageResource(R.mipmap.video_play_btn); 118 | videoView.pause(); 119 | bitmap = videoView.getBitmap(); 120 | if (bitmap != null) { 121 | pauseImage.setImageBitmap(bitmap); 122 | pauseImage.setVisibility(View.VISIBLE); 123 | } 124 | } 125 | 126 | public void reStart() { 127 | play.setImageResource(R.mipmap.video_stop_btn); 128 | videoView.start(); 129 | if (bitmap != null) { 130 | handler.sendEmptyMessageDelayed(PAUSE_IMAGE_HIDE, 100); 131 | bitmap.recycle(); 132 | bitmap = null; 133 | // pauseImage.setVisibility(View.GONE); 134 | } 135 | } 136 | 137 | private long duration; 138 | 139 | private void initAction() { 140 | isSound = false; 141 | seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 142 | @Override 143 | public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 144 | String string = generateTime((long) (duration * progress * 1.0f / 100)); 145 | time.setText(string); 146 | } 147 | 148 | @Override 149 | public void onStartTrackingTouch(SeekBar seekBar) { 150 | setProgress(); 151 | isDragging = true; 152 | audioManager.setStreamMute(AudioManager.STREAM_MUSIC, true); 153 | handler.removeMessages(MESSAGE_SHOW_PROGRESS); 154 | show(); 155 | handler.removeMessages(SET_VIEW_HIDE); 156 | } 157 | 158 | @Override 159 | public void onStopTrackingTouch(SeekBar seekBar) { 160 | isDragging = false; 161 | videoView.seekTo((int) (duration * seekBar.getProgress() * 1.0f / 100)); 162 | handler.removeMessages(MESSAGE_SHOW_PROGRESS); 163 | audioManager.setStreamMute(AudioManager.STREAM_MUSIC, false); 164 | isDragging = false; 165 | handler.sendEmptyMessageDelayed(MESSAGE_SHOW_PROGRESS, 1000); 166 | show(); 167 | } 168 | }); 169 | 170 | itemView.setOnTouchListener(new View.OnTouchListener() { 171 | @Override 172 | public boolean onTouch(View v, MotionEvent event) { 173 | Rect seekRect = new Rect(); 174 | seekBar.getHitRect(seekRect); 175 | 176 | if ((event.getY() >= (seekRect.top - 50)) && (event.getY() <= (seekRect.bottom + 50))) { 177 | 178 | float y = seekRect.top + seekRect.height() / 2; 179 | //seekBar only accept relative x 180 | float x = event.getX() - seekRect.left; 181 | if (x < 0) { 182 | x = 0; 183 | } else if (x > seekRect.width()) { 184 | x = seekRect.width(); 185 | } 186 | MotionEvent me = MotionEvent.obtain(event.getDownTime(), event.getEventTime(), 187 | event.getAction(), x, y, event.getMetaState()); 188 | return seekBar.onTouchEvent(me); 189 | 190 | } 191 | return false; 192 | } 193 | }); 194 | 195 | videoView.setOnInfoListener(new IMediaPlayer.OnInfoListener() { 196 | @Override 197 | public boolean onInfo(IMediaPlayer mp, int what, int extra) { 198 | 199 | Log.e("setOnInfoListener", what + ""); 200 | switch (what) { 201 | case IMediaPlayer.MEDIA_INFO_BUFFERING_START: 202 | //开始缓冲 203 | if (progressBar.getVisibility() == View.GONE) 204 | progressBar.setVisibility(View.VISIBLE); 205 | break; 206 | case IMediaPlayer.MEDIA_INFO_BUFFERING_END: 207 | //开始播放 208 | progressBar.setVisibility(View.GONE); 209 | break; 210 | 211 | case IMediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START: 212 | // statusChange(STATUS_PLAYING); 213 | progressBar.setVisibility(View.GONE); 214 | break; 215 | 216 | case IMediaPlayer.MEDIA_INFO_AUDIO_RENDERING_START: 217 | progressBar.setVisibility(View.GONE); 218 | break; 219 | } 220 | return false; 221 | } 222 | }); 223 | 224 | sound.setOnClickListener(new View.OnClickListener() { 225 | @Override 226 | public void onClick(View v) { 227 | if (isSound) { 228 | //静音 229 | sound.setImageResource(R.mipmap.sound_mult_icon); 230 | audioManager.setStreamMute(AudioManager.STREAM_MUSIC, true); 231 | } else { 232 | //取消静音 233 | sound.setImageResource(R.mipmap.sound_open_icon); 234 | audioManager.setStreamMute(AudioManager.STREAM_MUSIC, false); 235 | } 236 | isSound = !isSound; 237 | } 238 | }); 239 | 240 | 241 | play.setOnClickListener(new View.OnClickListener() { 242 | @Override 243 | public void onClick(View v) { 244 | if (videoView.isPlaying()) { 245 | pause(); 246 | } else { 247 | reStart(); 248 | } 249 | } 250 | }); 251 | 252 | full.setOnClickListener(new View.OnClickListener() { 253 | @Override 254 | public void onClick(View v) { 255 | /* Log.e("full","full"); 256 | if (getScreenOrientation((Activity) context) == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) { 257 | ((Activity)context).setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 258 | } else { 259 | ((Activity)context).setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 260 | }*/ 261 | if (fullScreenChangeListener != null) { 262 | fullScreenChangeListener.change(); 263 | } 264 | 265 | } 266 | 267 | }); 268 | } 269 | 270 | FullScreenChangeListener fullScreenChangeListener; 271 | 272 | public void setFullScreenChangeListener(FullScreenChangeListener listener) { 273 | this.fullScreenChangeListener = listener; 274 | } 275 | 276 | public interface FullScreenChangeListener { 277 | void change(); 278 | } 279 | 280 | public void setShowContoller(boolean isShowContoller) { 281 | this.isShowContoller = isShowContoller; 282 | handler.removeMessages(SET_VIEW_HIDE); 283 | itemView.setVisibility(View.GONE); 284 | } 285 | 286 | public int getScreenOrientation(Activity activity) { 287 | int rotation = activity.getWindowManager().getDefaultDisplay().getRotation(); 288 | DisplayMetrics dm = new DisplayMetrics(); 289 | activity.getWindowManager().getDefaultDisplay().getMetrics(dm); 290 | int width = dm.widthPixels; 291 | int height = dm.heightPixels; 292 | int orientation; 293 | // if the device's natural orientation is portrait: 294 | if ((rotation == Surface.ROTATION_0 295 | || rotation == Surface.ROTATION_180) && height > width || 296 | (rotation == Surface.ROTATION_90 297 | || rotation == Surface.ROTATION_270) && width > height) { 298 | switch (rotation) { 299 | case Surface.ROTATION_0: 300 | orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; 301 | break; 302 | case Surface.ROTATION_90: 303 | orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; 304 | break; 305 | case Surface.ROTATION_180: 306 | orientation = 307 | ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT; 308 | break; 309 | case Surface.ROTATION_270: 310 | orientation = 311 | ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE; 312 | break; 313 | default: 314 | orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; 315 | break; 316 | } 317 | } 318 | // if the device's natural orientation is landscape or if the device 319 | // is square: 320 | else { 321 | switch (rotation) { 322 | case Surface.ROTATION_0: 323 | orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; 324 | break; 325 | case Surface.ROTATION_90: 326 | orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; 327 | break; 328 | case Surface.ROTATION_180: 329 | orientation = 330 | ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE; 331 | break; 332 | case Surface.ROTATION_270: 333 | orientation = 334 | ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT; 335 | break; 336 | default: 337 | orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; 338 | break; 339 | } 340 | } 341 | 342 | return orientation; 343 | } 344 | 345 | @Override 346 | public void hide() { 347 | if (isShow) { 348 | handler.removeMessages(MESSAGE_SHOW_PROGRESS); 349 | isShow = false; 350 | handler.removeMessages(SET_VIEW_HIDE); 351 | itemView.setVisibility(View.GONE); 352 | } 353 | } 354 | 355 | @Override 356 | public boolean isShowing() { 357 | return isShow; 358 | } 359 | 360 | @Override 361 | public void setAnchorView(View view) { 362 | } 363 | 364 | @Override 365 | public void setEnabled(boolean enabled) { 366 | } 367 | 368 | @Override 369 | public void setMediaPlayer(MediaController.MediaPlayerControl player) { 370 | } 371 | 372 | @Override 373 | public void show(int timeout) { 374 | handler.sendEmptyMessageDelayed(SET_VIEW_HIDE, timeout); 375 | } 376 | 377 | @Override 378 | public void show() { 379 | if (!isShowContoller) 380 | return; 381 | isShow = true; 382 | progressBar.setVisibility(View.GONE); 383 | itemView.setVisibility(View.VISIBLE); 384 | handler.sendEmptyMessage(MESSAGE_SHOW_PROGRESS); 385 | show(TIME_OUT); 386 | } 387 | 388 | @Override 389 | public void showOnce(View view) { 390 | } 391 | 392 | private String generateTime(long time) { 393 | int totalSeconds = (int) (time / 1000); 394 | int seconds = totalSeconds % 60; 395 | int minutes = (totalSeconds / 60) % 60; 396 | int hours = totalSeconds / 3600; 397 | return hours > 0 ? String.format("%02d:%02d:%02d", hours, minutes, seconds) : String.format("%02d:%02d", minutes, seconds); 398 | } 399 | 400 | public void setVisiable() { 401 | show(); 402 | } 403 | 404 | private long setProgress() { 405 | if (isDragging) { 406 | return 0; 407 | } 408 | 409 | long position = videoView.getCurrentPosition(); 410 | long duration = videoView.getDuration(); 411 | this.duration = duration; 412 | if (!generateTime(duration).equals(allTime.getText().toString())) 413 | allTime.setText(generateTime(duration)); 414 | if (seekBar != null) { 415 | if (duration > 0) { 416 | long pos = 100L * position / duration; 417 | seekBar.setProgress((int) pos); 418 | } 419 | int percent = videoView.getBufferPercentage(); 420 | seekBar.setSecondaryProgress(percent); 421 | } 422 | String string = generateTime((long) (duration * seekBar.getProgress() * 1.0f / 100)); 423 | time.setText(string); 424 | return position; 425 | } 426 | 427 | private VedioIsPause vedioIsPause; 428 | 429 | public interface VedioIsPause { 430 | void pause(boolean pause); 431 | } 432 | 433 | public void setPauseImageHide() { 434 | pauseImage.setVisibility(View.GONE); 435 | } 436 | } 437 | -------------------------------------------------------------------------------- /VideoList/src/main/java/com/trs/videolist/VideoPlayView.java: -------------------------------------------------------------------------------- 1 | package com.trs.videolist; 2 | 3 | import android.app.Activity; 4 | import android.content.Context; 5 | import android.content.res.Configuration; 6 | import android.media.MediaPlayer; 7 | import android.net.Uri; 8 | import android.os.Handler; 9 | import android.util.Log; 10 | import android.view.LayoutInflater; 11 | import android.view.View; 12 | import android.view.ViewGroup; 13 | import android.view.WindowManager; 14 | import android.widget.RelativeLayout; 15 | 16 | import com.trs.videolist.media.IjkVideoView; 17 | 18 | import tv.danmaku.ijk.media.player.IMediaPlayer; 19 | 20 | /** 21 | * Description 播放view 22 | */ 23 | public class VideoPlayView extends RelativeLayout implements MediaPlayer.OnInfoListener, MediaPlayer.OnBufferingUpdateListener { 24 | private CustomMediaContoller mediaController; 25 | private View player_btn, view; 26 | private IjkVideoView mVideoView; 27 | private Handler handler = new Handler(); 28 | private boolean isPause; 29 | 30 | private View rView; 31 | private Context mContext; 32 | private boolean portrait; 33 | // private OrientationEventListener orientationEventListener; 34 | 35 | public VideoPlayView(Context context) { 36 | super(context); 37 | mContext = context; 38 | initData(); 39 | initViews(); 40 | initActions(); 41 | } 42 | 43 | private void initData() { 44 | 45 | } 46 | 47 | private void initViews() { 48 | 49 | rView = LayoutInflater.from(mContext).inflate(R.layout.view_video_item, this, true); 50 | view = findViewById(R.id.media_contoller); 51 | mVideoView = (IjkVideoView) findViewById(R.id.main_video); 52 | mediaController = new CustomMediaContoller(mContext, rView); 53 | mVideoView.setMediaController(mediaController); 54 | 55 | mVideoView.setOnCompletionListener(new IMediaPlayer.OnCompletionListener() { 56 | @Override 57 | public void onCompletion(IMediaPlayer mp) { 58 | view.setVisibility(View.GONE); 59 | /* if (mediaController.getScreenOrientation((Activity) mContext) 60 | == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) { 61 | //横屏播放完毕,重置 62 | ((Activity) mContext).setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 63 | ViewGroup.LayoutParams layoutParams = getLayoutParams(); 64 | layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT; 65 | layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT; 66 | setLayoutParams(layoutParams); 67 | }*/ 68 | 69 | if (completionListener != null) 70 | completionListener.completion(mp); 71 | } 72 | }); 73 | 74 | } 75 | 76 | public void setOnErrorListener(IMediaPlayer.OnErrorListener errorListener) { 77 | if (mVideoView != null) { 78 | mVideoView.setOnErrorListener(errorListener); 79 | } 80 | } 81 | 82 | 83 | public void setFullScreenChangeListener(CustomMediaContoller.FullScreenChangeListener listener) { 84 | if (mediaController != null) { 85 | mediaController.setFullScreenChangeListener(listener); 86 | } 87 | } 88 | 89 | private void initActions() { 90 | 91 | /*orientationEventListener = new OrientationEventListener(mContext) { 92 | @Override 93 | public void onOrientationChanged(int orientation) { 94 | Log.e("onOrientationChanged", "orientation"); 95 | if (orientation >= 0 && orientation <= 30 || orientation >= 330 || (orientation >= 150 && orientation <= 210)) { 96 | //竖屏 97 | if (portrait) { 98 | ((Activity) mContext).setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR); 99 | orientationEventListener.disable(); 100 | } 101 | } else if ((orientation >= 90 && orientation <= 120) || (orientation >= 240 && orientation <= 300)) { 102 | if (!portrait) { 103 | ((Activity) mContext).setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR); 104 | orientationEventListener.disable(); 105 | } 106 | } 107 | } 108 | };*/ 109 | } 110 | 111 | public boolean isPlay() { 112 | return mVideoView.isPlaying(); 113 | } 114 | 115 | public void pause() { 116 | if (mVideoView.isPlaying()) { 117 | mVideoView.pause(); 118 | } else { 119 | mVideoView.start(); 120 | } 121 | } 122 | 123 | public void start(String path) { 124 | Uri uri = Uri.parse(path); 125 | if (mediaController != null) 126 | mediaController.start(); 127 | if (!mVideoView.isPlaying()) { 128 | mVideoView.setVideoURI(uri); 129 | mVideoView.start(); 130 | } else { 131 | mVideoView.stopPlayback(); 132 | mVideoView.setVideoURI(uri); 133 | mVideoView.start(); 134 | 135 | } 136 | } 137 | 138 | public void start() { 139 | if (mVideoView.isPlaying()) { 140 | mVideoView.start(); 141 | 142 | } 143 | } 144 | 145 | 146 | 147 | 148 | public void setContorllerVisiable() { 149 | mediaController.setVisiable(); 150 | } 151 | 152 | public void seekTo(int msec) { 153 | mVideoView.seekTo(msec); 154 | } 155 | 156 | public void onChanged(Configuration configuration) { 157 | portrait = configuration.orientation == Configuration.ORIENTATION_PORTRAIT; 158 | doOnConfigurationChanged(portrait); 159 | } 160 | 161 | public void doOnConfigurationChanged(final boolean portrait) { 162 | if (mVideoView != null) { 163 | handler.post(new Runnable() { 164 | @Override 165 | public void run() { 166 | setFullScreen(!portrait); 167 | if (portrait) { 168 | ViewGroup.LayoutParams layoutParams = getLayoutParams(); 169 | layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT; 170 | layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT; 171 | Log.e("handler", "400"); 172 | setLayoutParams(layoutParams); 173 | requestLayout(); 174 | } else { 175 | int heightPixels = ((Activity) mContext).getResources().getDisplayMetrics().heightPixels; 176 | int widthPixels = ((Activity) mContext).getResources().getDisplayMetrics().widthPixels; 177 | ViewGroup.LayoutParams layoutParams = getLayoutParams(); 178 | layoutParams.height = heightPixels; 179 | layoutParams.width = widthPixels; 180 | Log.e("handler", "height==" + heightPixels + "\nwidth==" + widthPixels); 181 | setLayoutParams(layoutParams); 182 | } 183 | } 184 | }); 185 | // orientationEventListener.enable(); 186 | } 187 | } 188 | 189 | public void stop() { 190 | if (mVideoView.isPlaying()) { 191 | mVideoView.stopPlayback(); 192 | } 193 | } 194 | 195 | public void onDestroy() { 196 | handler.removeCallbacksAndMessages(null); 197 | // orientationEventListener.disable(); 198 | } 199 | 200 | private void setFullScreen(boolean fullScreen) { 201 | if (mContext != null && mContext instanceof Activity) { 202 | WindowManager.LayoutParams attrs = ((Activity) mContext).getWindow().getAttributes(); 203 | if (fullScreen) { 204 | attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN; 205 | ((Activity) mContext).getWindow().setAttributes(attrs); 206 | ((Activity) mContext).getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); 207 | } else { 208 | attrs.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN); 209 | ((Activity) mContext).getWindow().setAttributes(attrs); 210 | ((Activity) mContext).getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); 211 | } 212 | } 213 | 214 | } 215 | 216 | public void setShowContoller(boolean isShowContoller) { 217 | mediaController.setShowContoller(isShowContoller); 218 | } 219 | 220 | @Override 221 | public void onBufferingUpdate(MediaPlayer mp, int percent) { 222 | 223 | } 224 | 225 | @Override 226 | public boolean onInfo(MediaPlayer mp, int what, int extra) { 227 | return false; 228 | } 229 | 230 | public long getPalyPostion() { 231 | return mVideoView.getCurrentPosition(); 232 | } 233 | 234 | public void release() { 235 | mVideoView.release(true); 236 | } 237 | 238 | public int VideoStatus() { 239 | return mVideoView.getCurrentStatue(); 240 | } 241 | 242 | private CompletionListener completionListener; 243 | 244 | public void setCompletionListener(CompletionListener completionListener) { 245 | this.completionListener = completionListener; 246 | } 247 | 248 | public interface CompletionListener { 249 | void completion(IMediaPlayer mp); 250 | } 251 | } 252 | -------------------------------------------------------------------------------- /VideoList/src/main/java/com/trs/videolist/media/IMediaController.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Zhang Rui 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.trs.videolist.media; 18 | 19 | import android.view.View; 20 | import android.widget.MediaController; 21 | 22 | public interface IMediaController { 23 | void hide(); 24 | 25 | boolean isShowing(); 26 | 27 | void setAnchorView(View view); 28 | 29 | void setEnabled(boolean enabled); 30 | 31 | void setMediaPlayer(MediaController.MediaPlayerControl player); 32 | 33 | void show(int timeout); 34 | 35 | void show(); 36 | 37 | //---------- 38 | // Extends 39 | //---------- 40 | void showOnce(View view); 41 | } 42 | -------------------------------------------------------------------------------- /VideoList/src/main/java/com/trs/videolist/media/IRenderView.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Zhang Rui 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.trs.videolist.media; 18 | 19 | import android.graphics.SurfaceTexture; 20 | import android.support.annotation.NonNull; 21 | import android.support.annotation.Nullable; 22 | import android.view.Surface; 23 | import android.view.SurfaceHolder; 24 | import android.view.View; 25 | 26 | import tv.danmaku.ijk.media.player.IMediaPlayer; 27 | 28 | public interface IRenderView { 29 | int AR_ASPECT_FIT_PARENT = 0; // without clip 30 | int AR_ASPECT_FILL_PARENT = 1; // may clip 31 | int AR_ASPECT_WRAP_CONTENT = 2; 32 | int AR_MATCH_PARENT = 3; 33 | int AR_16_9_FIT_PARENT = 4; 34 | int AR_4_3_FIT_PARENT = 5; 35 | 36 | View getView(); 37 | 38 | boolean shouldWaitForResize(); 39 | 40 | void setVideoSize(int videoWidth, int videoHeight); 41 | 42 | void setVideoSampleAspectRatio(int videoSarNum, int videoSarDen); 43 | 44 | void setVideoRotation(int degree); 45 | 46 | void setAspectRatio(int aspectRatio); 47 | 48 | void addRenderCallback(@NonNull IRenderCallback callback); 49 | 50 | void removeRenderCallback(@NonNull IRenderCallback callback); 51 | 52 | interface ISurfaceHolder { 53 | void bindToMediaPlayer(IMediaPlayer mp); 54 | 55 | @NonNull 56 | IRenderView getRenderView(); 57 | 58 | @Nullable 59 | SurfaceHolder getSurfaceHolder(); 60 | 61 | @Nullable 62 | Surface openSurface(); 63 | 64 | @Nullable 65 | SurfaceTexture getSurfaceTexture(); 66 | } 67 | 68 | interface IRenderCallback { 69 | /** 70 | * @param holder 71 | * @param width could be 0 72 | * @param height could be 0 73 | */ 74 | void onSurfaceCreated(@NonNull ISurfaceHolder holder, int width, int height); 75 | 76 | /** 77 | * @param holder 78 | * @param format could be 0 79 | * @param width 80 | * @param height 81 | */ 82 | void onSurfaceChanged(@NonNull ISurfaceHolder holder, int format, int width, int height); 83 | 84 | void onSurfaceDestroyed(@NonNull ISurfaceHolder holder); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /VideoList/src/main/java/com/trs/videolist/media/InfoHudViewHolder.java: -------------------------------------------------------------------------------- 1 | package com.trs.videolist.media; 2 | 3 | import android.content.Context; 4 | import android.os.Handler; 5 | import android.os.Message; 6 | import android.util.SparseArray; 7 | import android.view.View; 8 | import android.widget.TableLayout; 9 | 10 | import com.trs.videolist.R; 11 | 12 | import java.util.Locale; 13 | 14 | import tv.danmaku.ijk.media.player.IMediaPlayer; 15 | import tv.danmaku.ijk.media.player.IjkMediaPlayer; 16 | import tv.danmaku.ijk.media.player.MediaPlayerProxy; 17 | 18 | public class InfoHudViewHolder { 19 | private TableLayoutBinder mTableLayoutBinder; 20 | private SparseArray mRowMap = new SparseArray(); 21 | private IMediaPlayer mMediaPlayer; 22 | 23 | public InfoHudViewHolder(Context context, TableLayout tableLayout) { 24 | mTableLayoutBinder = new TableLayoutBinder(context, tableLayout); 25 | } 26 | 27 | private void appendSection(int nameId) { 28 | mTableLayoutBinder.appendSection(nameId); 29 | } 30 | 31 | private void appendRow(int nameId) { 32 | View rowView = mTableLayoutBinder.appendRow2(nameId, null); 33 | mRowMap.put(nameId, rowView); 34 | } 35 | 36 | private void setRowValue(int id, String value) { 37 | View rowView = mRowMap.get(id); 38 | if (rowView == null) { 39 | rowView = mTableLayoutBinder.appendRow2(id, value); 40 | mRowMap.put(id, rowView); 41 | } else { 42 | mTableLayoutBinder.setValueText(rowView, value); 43 | } 44 | } 45 | 46 | public void setMediaPlayer(IMediaPlayer mp) { 47 | mMediaPlayer = mp; 48 | if (mMediaPlayer != null) { 49 | mHandler.sendEmptyMessageDelayed(MSG_UPDATE_HUD, 500); 50 | } else { 51 | mHandler.removeMessages(MSG_UPDATE_HUD); 52 | } 53 | } 54 | 55 | private static String formatedDurationMilli(long duration) { 56 | if (duration >= 1000) { 57 | return String.format(Locale.US, "%.2f sec", ((float)duration) / 1000); 58 | } else { 59 | return String.format(Locale.US, "%d msec", duration); 60 | } 61 | } 62 | 63 | private static String formatedSize(long bytes) { 64 | if (bytes >= 100 * 1000) { 65 | return String.format(Locale.US, "%.2f MB", ((float)bytes) / 1000 / 1000); 66 | } else if (bytes >= 100) { 67 | return String.format(Locale.US, "%.1f KB", ((float)bytes) / 1000); 68 | } else { 69 | return String.format(Locale.US, "%d B", bytes); 70 | } 71 | } 72 | 73 | private static final int MSG_UPDATE_HUD = 1; 74 | private Handler mHandler = new Handler() { 75 | @Override 76 | public void handleMessage(Message msg) { 77 | switch (msg.what) { 78 | case MSG_UPDATE_HUD: { 79 | InfoHudViewHolder holder = InfoHudViewHolder.this; 80 | IjkMediaPlayer mp = null; 81 | if (mMediaPlayer == null) 82 | break; 83 | if (mMediaPlayer instanceof IjkMediaPlayer) { 84 | mp = (IjkMediaPlayer) mMediaPlayer; 85 | } else if (mMediaPlayer instanceof MediaPlayerProxy) { 86 | MediaPlayerProxy proxy = (MediaPlayerProxy) mMediaPlayer; 87 | IMediaPlayer internal = proxy.getInternalMediaPlayer(); 88 | if (internal != null && internal instanceof IjkMediaPlayer) 89 | mp = (IjkMediaPlayer) internal; 90 | } 91 | if (mp == null) 92 | break; 93 | 94 | int vdec = mp.getVideoDecoder(); 95 | switch (vdec) { 96 | case IjkMediaPlayer.FFP_PROPV_DECODER_AVCODEC: 97 | setRowValue(R.string.vdec, "avcodec"); 98 | break; 99 | case IjkMediaPlayer.FFP_PROPV_DECODER_MEDIACODEC: 100 | setRowValue(R.string.vdec, "MediaCodec"); 101 | break; 102 | default: 103 | setRowValue(R.string.vdec, ""); 104 | break; 105 | } 106 | 107 | float fpsOutput = mp.getVideoOutputFramesPerSecond(); 108 | float fpsDecode = mp.getVideoDecodeFramesPerSecond(); 109 | setRowValue(R.string.fps, String.format(Locale.US, "%.2f / %.2f", fpsDecode, fpsOutput)); 110 | 111 | long videoCachedDuration = mp.getVideoCachedDuration(); 112 | long audioCachedDuration = mp.getAudioCachedDuration(); 113 | long videoCachedBytes = mp.getVideoCachedBytes(); 114 | long audioCachedBytes = mp.getAudioCachedBytes(); 115 | 116 | setRowValue(R.string.v_cache, String.format(Locale.US, "%s, %s", formatedDurationMilli(videoCachedDuration), formatedSize(videoCachedBytes))); 117 | setRowValue(R.string.a_cache, String.format(Locale.US, "%s, %s", formatedDurationMilli(audioCachedDuration), formatedSize(audioCachedBytes))); 118 | 119 | mHandler.removeMessages(MSG_UPDATE_HUD); 120 | mHandler.sendEmptyMessageDelayed(MSG_UPDATE_HUD, 500); 121 | } 122 | } 123 | } 124 | }; 125 | } 126 | -------------------------------------------------------------------------------- /VideoList/src/main/java/com/trs/videolist/media/MeasureHelper.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Zhang Rui 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.trs.videolist.media; 18 | 19 | import android.content.Context; 20 | import android.support.annotation.NonNull; 21 | import android.view.View; 22 | 23 | import com.trs.videolist.R; 24 | 25 | import java.lang.ref.WeakReference; 26 | 27 | public final class MeasureHelper { 28 | private WeakReference mWeakView; 29 | 30 | private int mVideoWidth; 31 | private int mVideoHeight; 32 | private int mVideoSarNum; 33 | private int mVideoSarDen; 34 | 35 | private int mVideoRotationDegree; 36 | 37 | private int mMeasuredWidth; 38 | private int mMeasuredHeight; 39 | 40 | private int mCurrentAspectRatio = IRenderView.AR_ASPECT_FIT_PARENT; 41 | 42 | public MeasureHelper(View view) { 43 | mWeakView = new WeakReference(view); 44 | } 45 | 46 | public View getView() { 47 | if (mWeakView == null) 48 | return null; 49 | return mWeakView.get(); 50 | } 51 | 52 | public void setVideoSize(int videoWidth, int videoHeight) { 53 | mVideoWidth = videoWidth; 54 | mVideoHeight = videoHeight; 55 | } 56 | 57 | public void setVideoSampleAspectRatio(int videoSarNum, int videoSarDen) { 58 | mVideoSarNum = videoSarNum; 59 | mVideoSarDen = videoSarDen; 60 | } 61 | 62 | public void setVideoRotation(int videoRotationDegree) { 63 | mVideoRotationDegree = videoRotationDegree; 64 | } 65 | 66 | /** 67 | * Must be called by View.onMeasure(int, int) 68 | * 69 | * @param widthMeasureSpec 70 | * @param heightMeasureSpec 71 | */ 72 | public void doMeasure(int widthMeasureSpec, int heightMeasureSpec) { 73 | //Log.i("@@@@", "onMeasure(" + MeasureSpec.toString(widthMeasureSpec) + ", " 74 | // + MeasureSpec.toString(heightMeasureSpec) + ")"); 75 | if (mVideoRotationDegree == 90 || mVideoRotationDegree == 270) { 76 | int tempSpec = widthMeasureSpec; 77 | widthMeasureSpec = heightMeasureSpec; 78 | heightMeasureSpec = tempSpec; 79 | } 80 | 81 | int width = View.getDefaultSize(mVideoWidth, widthMeasureSpec); 82 | int height = View.getDefaultSize(mVideoHeight, heightMeasureSpec); 83 | if (mCurrentAspectRatio == IRenderView.AR_MATCH_PARENT) { 84 | width = widthMeasureSpec; 85 | height = heightMeasureSpec; 86 | } else if (mVideoWidth > 0 && mVideoHeight > 0) { 87 | int widthSpecMode = View.MeasureSpec.getMode(widthMeasureSpec); 88 | int widthSpecSize = View.MeasureSpec.getSize(widthMeasureSpec); 89 | int heightSpecMode = View.MeasureSpec.getMode(heightMeasureSpec); 90 | int heightSpecSize = View.MeasureSpec.getSize(heightMeasureSpec); 91 | 92 | if (widthSpecMode == View.MeasureSpec.AT_MOST && heightSpecMode == View.MeasureSpec.AT_MOST) { 93 | float specAspectRatio = (float) widthSpecSize / (float) heightSpecSize; 94 | float displayAspectRatio; 95 | switch (mCurrentAspectRatio) { 96 | case IRenderView.AR_16_9_FIT_PARENT: 97 | displayAspectRatio = 16.0f / 9.0f; 98 | if (mVideoRotationDegree == 90 || mVideoRotationDegree == 270) 99 | displayAspectRatio = 1.0f / displayAspectRatio; 100 | break; 101 | case IRenderView.AR_4_3_FIT_PARENT: 102 | displayAspectRatio = 4.0f / 3.0f; 103 | if (mVideoRotationDegree == 90 || mVideoRotationDegree == 270) 104 | displayAspectRatio = 1.0f / displayAspectRatio; 105 | break; 106 | case IRenderView.AR_ASPECT_FIT_PARENT: 107 | case IRenderView.AR_ASPECT_FILL_PARENT: 108 | case IRenderView.AR_ASPECT_WRAP_CONTENT: 109 | default: 110 | displayAspectRatio = (float) mVideoWidth / (float) mVideoHeight; 111 | if (mVideoSarNum > 0 && mVideoSarDen > 0) 112 | displayAspectRatio = displayAspectRatio * mVideoSarNum / mVideoSarDen; 113 | break; 114 | } 115 | boolean shouldBeWider = displayAspectRatio > specAspectRatio; 116 | 117 | switch (mCurrentAspectRatio) { 118 | case IRenderView.AR_ASPECT_FIT_PARENT: 119 | case IRenderView.AR_16_9_FIT_PARENT: 120 | case IRenderView.AR_4_3_FIT_PARENT: 121 | if (shouldBeWider) { 122 | // too wide, fix width 123 | width = widthSpecSize; 124 | height = (int) (width / displayAspectRatio); 125 | } else { 126 | // too high, fix height 127 | height = heightSpecSize; 128 | width = (int) (height * displayAspectRatio); 129 | } 130 | break; 131 | case IRenderView.AR_ASPECT_FILL_PARENT: 132 | if (shouldBeWider) { 133 | // not high enough, fix height 134 | height = heightSpecSize; 135 | width = (int) (height * displayAspectRatio); 136 | } else { 137 | // not wide enough, fix width 138 | width = widthSpecSize; 139 | height = (int) (width / displayAspectRatio); 140 | } 141 | break; 142 | case IRenderView.AR_ASPECT_WRAP_CONTENT: 143 | default: 144 | if (shouldBeWider) { 145 | // too wide, fix width 146 | width = Math.min(mVideoWidth, widthSpecSize); 147 | height = (int) (width / displayAspectRatio); 148 | } else { 149 | // too high, fix height 150 | height = Math.min(mVideoHeight, heightSpecSize); 151 | width = (int) (height * displayAspectRatio); 152 | } 153 | break; 154 | } 155 | } else if (widthSpecMode == View.MeasureSpec.EXACTLY && heightSpecMode == View.MeasureSpec.EXACTLY) { 156 | // the size is fixed 157 | width = widthSpecSize; 158 | height = heightSpecSize; 159 | 160 | // for compatibility, we adjust size based on aspect ratio 161 | if (mVideoWidth * height < width * mVideoHeight) { 162 | //Log.i("@@@", "image too wide, correcting"); 163 | width = height * mVideoWidth / mVideoHeight; 164 | } else if (mVideoWidth * height > width * mVideoHeight) { 165 | //Log.i("@@@", "image too tall, correcting"); 166 | height = width * mVideoHeight / mVideoWidth; 167 | } 168 | } else if (widthSpecMode == View.MeasureSpec.EXACTLY) { 169 | // only the width is fixed, adjust the height to match aspect ratio if possible 170 | width = widthSpecSize; 171 | height = width * mVideoHeight / mVideoWidth; 172 | if (heightSpecMode == View.MeasureSpec.AT_MOST && height > heightSpecSize) { 173 | // couldn't match aspect ratio within the constraints 174 | height = heightSpecSize; 175 | } 176 | } else if (heightSpecMode == View.MeasureSpec.EXACTLY) { 177 | // only the height is fixed, adjust the width to match aspect ratio if possible 178 | height = heightSpecSize; 179 | width = height * mVideoWidth / mVideoHeight; 180 | if (widthSpecMode == View.MeasureSpec.AT_MOST && width > widthSpecSize) { 181 | // couldn't match aspect ratio within the constraints 182 | width = widthSpecSize; 183 | } 184 | } else { 185 | // neither the width nor the height are fixed, try to use actual video size 186 | width = mVideoWidth; 187 | height = mVideoHeight; 188 | if (heightSpecMode == View.MeasureSpec.AT_MOST && height > heightSpecSize) { 189 | // too tall, decrease both width and height 190 | height = heightSpecSize; 191 | width = height * mVideoWidth / mVideoHeight; 192 | } 193 | if (widthSpecMode == View.MeasureSpec.AT_MOST && width > widthSpecSize) { 194 | // too wide, decrease both width and height 195 | width = widthSpecSize; 196 | height = width * mVideoHeight / mVideoWidth; 197 | } 198 | } 199 | } else { 200 | // no size yet, just adopt the given spec sizes 201 | } 202 | 203 | mMeasuredWidth = width; 204 | mMeasuredHeight = height; 205 | } 206 | 207 | public int getMeasuredWidth() { 208 | return mMeasuredWidth; 209 | } 210 | 211 | public int getMeasuredHeight() { 212 | return mMeasuredHeight; 213 | } 214 | 215 | public void setAspectRatio(int aspectRatio) { 216 | mCurrentAspectRatio = aspectRatio; 217 | } 218 | 219 | @NonNull 220 | public static String getAspectRatioText(Context context, int aspectRatio) { 221 | String text; 222 | switch (aspectRatio) { 223 | case IRenderView.AR_ASPECT_FIT_PARENT: 224 | text = context.getString(R.string.VideoView_ar_aspect_fit_parent); 225 | break; 226 | case IRenderView.AR_ASPECT_FILL_PARENT: 227 | text = context.getString(R.string.VideoView_ar_aspect_fill_parent); 228 | break; 229 | case IRenderView.AR_ASPECT_WRAP_CONTENT: 230 | text = context.getString(R.string.VideoView_ar_aspect_wrap_content); 231 | break; 232 | case IRenderView.AR_MATCH_PARENT: 233 | text = context.getString(R.string.VideoView_ar_match_parent); 234 | break; 235 | case IRenderView.AR_16_9_FIT_PARENT: 236 | text = context.getString(R.string.VideoView_ar_16_9_fit_parent); 237 | break; 238 | case IRenderView.AR_4_3_FIT_PARENT: 239 | text = context.getString(R.string.VideoView_ar_4_3_fit_parent); 240 | break; 241 | default: 242 | text = context.getString(R.string.N_A); 243 | break; 244 | } 245 | return text; 246 | } 247 | } 248 | -------------------------------------------------------------------------------- /VideoList/src/main/java/com/trs/videolist/media/MediaPlayerCompat.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Zhang Rui 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.trs.videolist.media; 18 | 19 | import tv.danmaku.ijk.media.player.IMediaPlayer; 20 | import tv.danmaku.ijk.media.player.IjkMediaPlayer; 21 | import tv.danmaku.ijk.media.player.MediaPlayerProxy; 22 | import tv.danmaku.ijk.media.player.TextureMediaPlayer; 23 | 24 | public class MediaPlayerCompat { 25 | public static String getName(IMediaPlayer mp) { 26 | if (mp == null) { 27 | return "null"; 28 | } else if (mp instanceof TextureMediaPlayer) { 29 | StringBuilder sb = new StringBuilder("TextureMediaPlayer <"); 30 | IMediaPlayer internalMediaPlayer = ((TextureMediaPlayer) mp).getInternalMediaPlayer(); 31 | if (internalMediaPlayer == null) { 32 | sb.append("null>"); 33 | } else { 34 | sb.append(internalMediaPlayer.getClass().getSimpleName()); 35 | sb.append(">"); 36 | } 37 | return sb.toString(); 38 | } else { 39 | return mp.getClass().getSimpleName(); 40 | } 41 | } 42 | 43 | public static IjkMediaPlayer getIjkMediaPlayer(IMediaPlayer mp) { 44 | IjkMediaPlayer ijkMediaPlayer = null; 45 | if (mp == null) { 46 | return null; 47 | } if (mp instanceof IjkMediaPlayer) { 48 | ijkMediaPlayer = (IjkMediaPlayer) mp; 49 | } else if (mp instanceof MediaPlayerProxy && ((MediaPlayerProxy) mp).getInternalMediaPlayer() instanceof IjkMediaPlayer) { 50 | ijkMediaPlayer = (IjkMediaPlayer) ((MediaPlayerProxy) mp).getInternalMediaPlayer(); 51 | } 52 | return ijkMediaPlayer; 53 | } 54 | 55 | public static void selectTrack(IMediaPlayer mp, int stream) { 56 | IjkMediaPlayer ijkMediaPlayer = getIjkMediaPlayer(mp); 57 | if (ijkMediaPlayer == null) 58 | return; 59 | ijkMediaPlayer.selectTrack(stream); 60 | } 61 | 62 | public static void deselectTrack(IMediaPlayer mp, int stream) { 63 | IjkMediaPlayer ijkMediaPlayer = getIjkMediaPlayer(mp); 64 | if (ijkMediaPlayer == null) 65 | return; 66 | ijkMediaPlayer.deselectTrack(stream); 67 | } 68 | 69 | public static int getSelectedTrack(IMediaPlayer mp, int trackType) { 70 | IjkMediaPlayer ijkMediaPlayer = getIjkMediaPlayer(mp); 71 | if (ijkMediaPlayer == null) 72 | return -1; 73 | return ijkMediaPlayer.getSelectedTrack(trackType); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /VideoList/src/main/java/com/trs/videolist/media/MediaPlayerService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Zhang Rui 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.trs.videolist.media; 18 | 19 | import android.app.Service; 20 | import android.content.Context; 21 | import android.content.Intent; 22 | import android.os.IBinder; 23 | import android.support.annotation.Nullable; 24 | 25 | import tv.danmaku.ijk.media.player.IMediaPlayer; 26 | 27 | public class MediaPlayerService extends Service { 28 | private static IMediaPlayer sMediaPlayer; 29 | 30 | public static Intent newIntent(Context context) { 31 | Intent intent = new Intent(context, MediaPlayerService.class); 32 | return intent; 33 | } 34 | 35 | public static void intentToStart(Context context) { 36 | context.startService(newIntent(context)); 37 | } 38 | 39 | public static void intentToStop(Context context) { 40 | context.stopService(newIntent(context)); 41 | } 42 | 43 | @Nullable 44 | @Override 45 | public IBinder onBind(Intent intent) { 46 | return null; 47 | } 48 | 49 | public static void setMediaPlayer(IMediaPlayer mp) { 50 | if (sMediaPlayer != null && sMediaPlayer != mp) { 51 | if (sMediaPlayer.isPlaying()) 52 | sMediaPlayer.stop(); 53 | sMediaPlayer.release(); 54 | sMediaPlayer = null; 55 | } 56 | sMediaPlayer = mp; 57 | } 58 | 59 | public static IMediaPlayer getMediaPlayer() { 60 | return sMediaPlayer; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /VideoList/src/main/java/com/trs/videolist/media/Settings.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Zhang Rui 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.trs.videolist.media; 18 | 19 | import android.content.Context; 20 | import android.content.SharedPreferences; 21 | import android.preference.PreferenceManager; 22 | 23 | import com.trs.videolist.R; 24 | 25 | public class Settings { 26 | private Context mAppContext; 27 | private SharedPreferences mSharedPreferences; 28 | 29 | public static final int PV_PLAYER__Auto = 0; 30 | public static final int PV_PLAYER__AndroidMediaPlayer = 1; 31 | public static final int PV_PLAYER__IjkMediaPlayer = 2; 32 | public static final int PV_PLAYER__IjkExoMediaPlayer = 3; 33 | 34 | public Settings(Context context) { 35 | mAppContext = context.getApplicationContext(); 36 | mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(mAppContext); 37 | } 38 | 39 | public boolean getEnableBackgroundPlay() { 40 | String key = mAppContext.getString(R.string.pref_key_enable_background_play); 41 | return mSharedPreferences.getBoolean(key, false); 42 | } 43 | 44 | public int getPlayer() { 45 | String key = mAppContext.getString(R.string.pref_key_player); 46 | String value = mSharedPreferences.getString(key, ""); 47 | try { 48 | return Integer.valueOf(value).intValue(); 49 | } catch (NumberFormatException e) { 50 | return 0; 51 | } 52 | } 53 | 54 | public boolean getUsingMediaCodec() { 55 | String key = mAppContext.getString(R.string.pref_key_using_media_codec); 56 | return mSharedPreferences.getBoolean(key, false); 57 | } 58 | 59 | public boolean getUsingMediaCodecAutoRotate() { 60 | String key = mAppContext.getString(R.string.pref_key_using_media_codec_auto_rotate); 61 | return mSharedPreferences.getBoolean(key, false); 62 | } 63 | 64 | public boolean getUsingOpenSLES() { 65 | String key = mAppContext.getString(R.string.pref_key_using_opensl_es); 66 | return mSharedPreferences.getBoolean(key, false); 67 | } 68 | 69 | public String getPixelFormat() { 70 | String key = mAppContext.getString(R.string.pref_key_pixel_format); 71 | return mSharedPreferences.getString(key, ""); 72 | } 73 | 74 | public boolean getEnableNoView() { 75 | String key = mAppContext.getString(R.string.pref_key_enable_no_view); 76 | return mSharedPreferences.getBoolean(key, false); 77 | } 78 | 79 | public boolean getEnableSurfaceView() { 80 | String key = mAppContext.getString(R.string.pref_key_enable_surface_view); 81 | return mSharedPreferences.getBoolean(key, false); 82 | } 83 | 84 | public boolean getEnableTextureView() { 85 | String key = mAppContext.getString(R.string.pref_key_enable_texture_view); 86 | return mSharedPreferences.getBoolean(key, false); 87 | } 88 | 89 | public boolean getEnableDetachedSurfaceTextureView() { 90 | String key = mAppContext.getString(R.string.pref_key_enable_detached_surface_texture); 91 | return mSharedPreferences.getBoolean(key, false); 92 | } 93 | 94 | public String getLastDirectory() { 95 | String key = mAppContext.getString(R.string.pref_key_last_directory); 96 | return mSharedPreferences.getString(key, "/"); 97 | } 98 | 99 | public void setLastDirectory(String path) { 100 | String key = mAppContext.getString(R.string.pref_key_last_directory); 101 | mSharedPreferences.edit().putString(key, path).apply(); 102 | } 103 | 104 | public void setEnableTextureView(boolean enable){ 105 | String key = mAppContext.getString(R.string.pref_key_enable_texture_view); 106 | SharedPreferences.Editor editor=mSharedPreferences.edit(); 107 | editor.putBoolean(key,enable); 108 | editor.commit(); 109 | } 110 | 111 | public void setEnableSurfaceView(boolean enable){ 112 | String key = mAppContext.getString(R.string.pref_key_enable_surface_view); 113 | SharedPreferences.Editor editor=mSharedPreferences.edit(); 114 | editor.putBoolean(key,enable); 115 | editor.commit(); 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /VideoList/src/main/java/com/trs/videolist/media/SurfaceRenderView.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Zhang Rui 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.trs.videolist.media; 18 | 19 | import android.annotation.TargetApi; 20 | import android.content.Context; 21 | import android.graphics.SurfaceTexture; 22 | import android.os.Build; 23 | import android.support.annotation.NonNull; 24 | import android.support.annotation.Nullable; 25 | import android.util.AttributeSet; 26 | import android.util.Log; 27 | import android.view.Surface; 28 | import android.view.SurfaceHolder; 29 | import android.view.SurfaceView; 30 | import android.view.View; 31 | import android.view.accessibility.AccessibilityEvent; 32 | import android.view.accessibility.AccessibilityNodeInfo; 33 | 34 | import java.lang.ref.WeakReference; 35 | import java.util.Map; 36 | import java.util.concurrent.ConcurrentHashMap; 37 | 38 | import tv.danmaku.ijk.media.player.IMediaPlayer; 39 | import tv.danmaku.ijk.media.player.ISurfaceTextureHolder; 40 | 41 | public class SurfaceRenderView extends SurfaceView implements IRenderView { 42 | private MeasureHelper mMeasureHelper; 43 | 44 | public SurfaceRenderView(Context context) { 45 | super(context); 46 | initView(context); 47 | } 48 | 49 | public SurfaceRenderView(Context context, AttributeSet attrs) { 50 | super(context, attrs); 51 | initView(context); 52 | } 53 | 54 | public SurfaceRenderView(Context context, AttributeSet attrs, int defStyleAttr) { 55 | super(context, attrs, defStyleAttr); 56 | initView(context); 57 | } 58 | 59 | @TargetApi(Build.VERSION_CODES.LOLLIPOP) 60 | public SurfaceRenderView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 61 | super(context, attrs, defStyleAttr, defStyleRes); 62 | initView(context); 63 | } 64 | 65 | private void initView(Context context) { 66 | mMeasureHelper = new MeasureHelper(this); 67 | mSurfaceCallback = new SurfaceCallback(this); 68 | getHolder().addCallback(mSurfaceCallback); 69 | //noinspection deprecation 70 | getHolder().setType(SurfaceHolder.SURFACE_TYPE_NORMAL); 71 | } 72 | 73 | @Override 74 | public View getView() { 75 | return this; 76 | } 77 | 78 | @Override 79 | public boolean shouldWaitForResize() { 80 | return true; 81 | } 82 | 83 | //-------------------- 84 | // Layout & Measure 85 | //-------------------- 86 | @Override 87 | public void setVideoSize(int videoWidth, int videoHeight) { 88 | if (videoWidth > 0 && videoHeight > 0) { 89 | mMeasureHelper.setVideoSize(videoWidth, videoHeight); 90 | getHolder().setFixedSize(videoWidth, videoHeight); 91 | requestLayout(); 92 | } 93 | } 94 | 95 | @Override 96 | public void setVideoSampleAspectRatio(int videoSarNum, int videoSarDen) { 97 | if (videoSarNum > 0 && videoSarDen > 0) { 98 | mMeasureHelper.setVideoSampleAspectRatio(videoSarNum, videoSarDen); 99 | requestLayout(); 100 | } 101 | } 102 | 103 | @Override 104 | public void setVideoRotation(int degree) { 105 | Log.e("", "SurfaceView doesn't support rotation (" + degree + ")!\n"); 106 | } 107 | 108 | @Override 109 | public void setAspectRatio(int aspectRatio) { 110 | mMeasureHelper.setAspectRatio(aspectRatio); 111 | requestLayout(); 112 | } 113 | 114 | @Override 115 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 116 | mMeasureHelper.doMeasure(widthMeasureSpec, heightMeasureSpec); 117 | setMeasuredDimension(mMeasureHelper.getMeasuredWidth(), mMeasureHelper.getMeasuredHeight()); 118 | } 119 | 120 | //-------------------- 121 | // SurfaceViewHolder 122 | //-------------------- 123 | 124 | private static final class InternalSurfaceHolder implements IRenderView.ISurfaceHolder { 125 | private SurfaceRenderView mSurfaceView; 126 | private SurfaceHolder mSurfaceHolder; 127 | 128 | public InternalSurfaceHolder(@NonNull SurfaceRenderView surfaceView, 129 | @Nullable SurfaceHolder surfaceHolder) { 130 | mSurfaceView = surfaceView; 131 | mSurfaceHolder = surfaceHolder; 132 | } 133 | 134 | public void bindToMediaPlayer(IMediaPlayer mp) { 135 | if (mp != null) { 136 | if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) && 137 | (mp instanceof ISurfaceTextureHolder)) { 138 | ISurfaceTextureHolder textureHolder = (ISurfaceTextureHolder) mp; 139 | textureHolder.setSurfaceTexture(null); 140 | } 141 | mp.setDisplay(mSurfaceHolder); 142 | } 143 | } 144 | 145 | @NonNull 146 | @Override 147 | public IRenderView getRenderView() { 148 | return mSurfaceView; 149 | } 150 | 151 | @Nullable 152 | @Override 153 | public SurfaceHolder getSurfaceHolder() { 154 | return mSurfaceHolder; 155 | } 156 | 157 | @Nullable 158 | @Override 159 | public SurfaceTexture getSurfaceTexture() { 160 | return null; 161 | } 162 | 163 | @Nullable 164 | @Override 165 | public Surface openSurface() { 166 | if (mSurfaceHolder == null) 167 | return null; 168 | return mSurfaceHolder.getSurface(); 169 | } 170 | } 171 | 172 | //------------------------- 173 | // SurfaceHolder.Callback 174 | //------------------------- 175 | 176 | @Override 177 | public void addRenderCallback(IRenderCallback callback) { 178 | mSurfaceCallback.addRenderCallback(callback); 179 | } 180 | 181 | @Override 182 | public void removeRenderCallback(IRenderCallback callback) { 183 | mSurfaceCallback.removeRenderCallback(callback); 184 | } 185 | 186 | private SurfaceCallback mSurfaceCallback; 187 | 188 | private static final class SurfaceCallback implements SurfaceHolder.Callback { 189 | private SurfaceHolder mSurfaceHolder; 190 | private boolean mIsFormatChanged; 191 | private int mFormat; 192 | private int mWidth; 193 | private int mHeight; 194 | 195 | private WeakReference mWeakSurfaceView; 196 | private Map mRenderCallbackMap = new ConcurrentHashMap(); 197 | 198 | public SurfaceCallback(@NonNull SurfaceRenderView surfaceView) { 199 | mWeakSurfaceView = new WeakReference(surfaceView); 200 | } 201 | 202 | public void addRenderCallback(@NonNull IRenderCallback callback) { 203 | mRenderCallbackMap.put(callback, callback); 204 | 205 | ISurfaceHolder surfaceHolder = null; 206 | if (mSurfaceHolder != null) { 207 | if (surfaceHolder == null) 208 | surfaceHolder = new InternalSurfaceHolder(mWeakSurfaceView.get(), mSurfaceHolder); 209 | callback.onSurfaceCreated(surfaceHolder, mWidth, mHeight); 210 | } 211 | 212 | if (mIsFormatChanged) { 213 | if (surfaceHolder == null) 214 | surfaceHolder = new InternalSurfaceHolder(mWeakSurfaceView.get(), mSurfaceHolder); 215 | callback.onSurfaceChanged(surfaceHolder, mFormat, mWidth, mHeight); 216 | } 217 | } 218 | 219 | public void removeRenderCallback(@NonNull IRenderCallback callback) { 220 | mRenderCallbackMap.remove(callback); 221 | } 222 | 223 | @Override 224 | public void surfaceCreated(SurfaceHolder holder) { 225 | mSurfaceHolder = holder; 226 | mIsFormatChanged = false; 227 | mFormat = 0; 228 | mWidth = 0; 229 | mHeight = 0; 230 | 231 | ISurfaceHolder surfaceHolder = new InternalSurfaceHolder(mWeakSurfaceView.get(), mSurfaceHolder); 232 | for (IRenderCallback renderCallback : mRenderCallbackMap.keySet()) { 233 | renderCallback.onSurfaceCreated(surfaceHolder, 0, 0); 234 | } 235 | } 236 | 237 | @Override 238 | public void surfaceDestroyed(SurfaceHolder holder) { 239 | mSurfaceHolder = null; 240 | mIsFormatChanged = false; 241 | mFormat = 0; 242 | mWidth = 0; 243 | mHeight = 0; 244 | 245 | ISurfaceHolder surfaceHolder = new InternalSurfaceHolder(mWeakSurfaceView.get(), mSurfaceHolder); 246 | for (IRenderCallback renderCallback : mRenderCallbackMap.keySet()) { 247 | renderCallback.onSurfaceDestroyed(surfaceHolder); 248 | } 249 | } 250 | 251 | @Override 252 | public void surfaceChanged(SurfaceHolder holder, int format, 253 | int width, int height) { 254 | mSurfaceHolder = holder; 255 | mIsFormatChanged = true; 256 | mFormat = format; 257 | mWidth = width; 258 | mHeight = height; 259 | 260 | // mMeasureHelper.setVideoSize(width, height); 261 | 262 | ISurfaceHolder surfaceHolder = new InternalSurfaceHolder(mWeakSurfaceView.get(), mSurfaceHolder); 263 | for (IRenderCallback renderCallback : mRenderCallbackMap.keySet()) { 264 | renderCallback.onSurfaceChanged(surfaceHolder, format, width, height); 265 | } 266 | } 267 | } 268 | 269 | //-------------------- 270 | // Accessibility 271 | //-------------------- 272 | 273 | @Override 274 | public void onInitializeAccessibilityEvent(AccessibilityEvent event) { 275 | super.onInitializeAccessibilityEvent(event); 276 | event.setClassName(SurfaceRenderView.class.getName()); 277 | } 278 | 279 | @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) 280 | @Override 281 | public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { 282 | super.onInitializeAccessibilityNodeInfo(info); 283 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { 284 | info.setClassName(SurfaceRenderView.class.getName()); 285 | } 286 | } 287 | } 288 | -------------------------------------------------------------------------------- /VideoList/src/main/java/com/trs/videolist/media/TableLayoutBinder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Zhang Rui 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.trs.videolist.media; 18 | 19 | import android.content.Context; 20 | import android.support.v7.app.AlertDialog; 21 | import android.view.LayoutInflater; 22 | import android.view.View; 23 | import android.view.ViewGroup; 24 | import android.widget.TableLayout; 25 | import android.widget.TextView; 26 | 27 | import com.trs.videolist.R; 28 | 29 | public class TableLayoutBinder { 30 | private Context mContext; 31 | public ViewGroup mTableView; 32 | public TableLayout mTableLayout; 33 | 34 | public TableLayoutBinder(Context context) { 35 | this(context, R.layout.table_media_info); 36 | } 37 | 38 | public TableLayoutBinder(Context context, int layoutResourceId) { 39 | mContext = context; 40 | mTableView = (ViewGroup) LayoutInflater.from(mContext).inflate(layoutResourceId, null); 41 | mTableLayout = (TableLayout) mTableView.findViewById(R.id.table); 42 | } 43 | 44 | public TableLayoutBinder(Context context, TableLayout tableLayout) { 45 | mContext = context; 46 | mTableView = tableLayout; 47 | mTableLayout = tableLayout; 48 | } 49 | 50 | public View appendRow1(String name, String value) { 51 | return appendRow(R.layout.table_media_info_row1, name, value); 52 | } 53 | 54 | public View appendRow1(int nameId, String value) { 55 | return appendRow1(mContext.getString(nameId), value); 56 | } 57 | 58 | public View appendRow2(String name, String value) { 59 | return appendRow(R.layout.table_media_info_row2, name, value); 60 | } 61 | 62 | public View appendRow2(int nameId, String value) { 63 | return appendRow2(mContext.getString(nameId), value); 64 | } 65 | 66 | public View appendSection(String name) { 67 | return appendRow(R.layout.table_media_info_section, name, null); 68 | } 69 | 70 | public View appendSection(int nameId) { 71 | return appendSection(mContext.getString(nameId)); 72 | } 73 | 74 | public View appendRow(int layoutId, String name, String value) { 75 | ViewGroup rowView = (ViewGroup) LayoutInflater.from(mContext).inflate(layoutId, mTableLayout, false); 76 | setNameValueText(rowView, name, value); 77 | 78 | mTableLayout.addView(rowView); 79 | return rowView; 80 | } 81 | 82 | public ViewHolder obtainViewHolder(View rowView) { 83 | ViewHolder viewHolder = (ViewHolder) rowView.getTag(); 84 | if (viewHolder == null) { 85 | viewHolder = new ViewHolder(); 86 | viewHolder.mNameTextView = (TextView) rowView.findViewById(R.id.name); 87 | viewHolder.mValueTextView = (TextView) rowView.findViewById(R.id.value); 88 | rowView.setTag(viewHolder); 89 | } 90 | return viewHolder; 91 | } 92 | 93 | public void setNameValueText(View rowView, String name, String value) { 94 | ViewHolder viewHolder = obtainViewHolder(rowView); 95 | viewHolder.setName(name); 96 | viewHolder.setValue(value); 97 | } 98 | 99 | public void setValueText(View rowView, String value) { 100 | ViewHolder viewHolder = obtainViewHolder(rowView); 101 | viewHolder.setValue(value); 102 | } 103 | 104 | public ViewGroup buildLayout() { 105 | return mTableView; 106 | } 107 | 108 | public AlertDialog.Builder buildAlertDialogBuilder() { 109 | AlertDialog.Builder dlgBuilder = new AlertDialog.Builder(mContext); 110 | dlgBuilder.setView(buildLayout()); 111 | return dlgBuilder; 112 | } 113 | 114 | private static class ViewHolder { 115 | public TextView mNameTextView; 116 | public TextView mValueTextView; 117 | 118 | public void setName(String name) { 119 | if (mNameTextView != null) { 120 | mNameTextView.setText(name); 121 | } 122 | } 123 | 124 | public void setValue(String value) { 125 | if (mValueTextView != null) { 126 | mValueTextView.setText(value); 127 | } 128 | } 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /VideoList/src/main/java/com/trs/videolist/media/TextureRenderView.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Zhang Rui 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.trs.videolist.media; 18 | 19 | import android.annotation.TargetApi; 20 | import android.content.Context; 21 | import android.graphics.SurfaceTexture; 22 | import android.os.Build; 23 | import android.support.annotation.NonNull; 24 | import android.support.annotation.Nullable; 25 | import android.util.AttributeSet; 26 | import android.util.Log; 27 | import android.view.Surface; 28 | import android.view.SurfaceHolder; 29 | import android.view.TextureView; 30 | import android.view.View; 31 | import android.view.accessibility.AccessibilityEvent; 32 | import android.view.accessibility.AccessibilityNodeInfo; 33 | 34 | import java.lang.ref.WeakReference; 35 | import java.util.Map; 36 | import java.util.concurrent.ConcurrentHashMap; 37 | 38 | import tv.danmaku.ijk.media.player.IMediaPlayer; 39 | import tv.danmaku.ijk.media.player.ISurfaceTextureHolder; 40 | import tv.danmaku.ijk.media.player.ISurfaceTextureHost; 41 | 42 | @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) 43 | public class TextureRenderView extends TextureView implements IRenderView { 44 | private static final String TAG = "TextureRenderView"; 45 | private MeasureHelper mMeasureHelper; 46 | 47 | public TextureRenderView(Context context) { 48 | super(context); 49 | initView(context); 50 | } 51 | 52 | public TextureRenderView(Context context, AttributeSet attrs) { 53 | super(context, attrs); 54 | initView(context); 55 | } 56 | 57 | public TextureRenderView(Context context, AttributeSet attrs, int defStyleAttr) { 58 | super(context, attrs, defStyleAttr); 59 | initView(context); 60 | } 61 | 62 | @TargetApi(Build.VERSION_CODES.LOLLIPOP) 63 | public TextureRenderView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 64 | super(context, attrs, defStyleAttr, defStyleRes); 65 | initView(context); 66 | } 67 | 68 | private void initView(Context context) { 69 | mMeasureHelper = new MeasureHelper(this); 70 | mSurfaceCallback = new SurfaceCallback(this); 71 | setSurfaceTextureListener(mSurfaceCallback); 72 | } 73 | 74 | @Override 75 | public View getView() { 76 | return this; 77 | } 78 | 79 | @Override 80 | public boolean shouldWaitForResize() { 81 | return false; 82 | } 83 | 84 | @Override 85 | protected void onDetachedFromWindow() { 86 | mSurfaceCallback.willDetachFromWindow(); 87 | super.onDetachedFromWindow(); 88 | mSurfaceCallback.didDetachFromWindow(); 89 | } 90 | 91 | //-------------------- 92 | // Layout & Measure 93 | //-------------------- 94 | @Override 95 | public void setVideoSize(int videoWidth, int videoHeight) { 96 | if (videoWidth > 0 && videoHeight > 0) { 97 | mMeasureHelper.setVideoSize(videoWidth, videoHeight); 98 | requestLayout(); 99 | } 100 | } 101 | 102 | @Override 103 | public void setVideoSampleAspectRatio(int videoSarNum, int videoSarDen) { 104 | if (videoSarNum > 0 && videoSarDen > 0) { 105 | mMeasureHelper.setVideoSampleAspectRatio(videoSarNum, videoSarDen); 106 | requestLayout(); 107 | } 108 | } 109 | 110 | @Override 111 | public void setVideoRotation(int degree) { 112 | mMeasureHelper.setVideoRotation(degree); 113 | setRotation(degree); 114 | } 115 | 116 | @Override 117 | public void setAspectRatio(int aspectRatio) { 118 | mMeasureHelper.setAspectRatio(aspectRatio); 119 | requestLayout(); 120 | } 121 | 122 | @Override 123 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 124 | mMeasureHelper.doMeasure(widthMeasureSpec, heightMeasureSpec); 125 | setMeasuredDimension(mMeasureHelper.getMeasuredWidth(), mMeasureHelper.getMeasuredHeight()); 126 | } 127 | 128 | //-------------------- 129 | // TextureViewHolder 130 | //-------------------- 131 | 132 | public IRenderView.ISurfaceHolder getSurfaceHolder() { 133 | return new InternalSurfaceHolder(this, mSurfaceCallback.mSurfaceTexture, mSurfaceCallback); 134 | } 135 | 136 | private static final class InternalSurfaceHolder implements IRenderView.ISurfaceHolder { 137 | private TextureRenderView mTextureView; 138 | private SurfaceTexture mSurfaceTexture; 139 | private ISurfaceTextureHost mSurfaceTextureHost; 140 | 141 | public InternalSurfaceHolder(@NonNull TextureRenderView textureView, 142 | @Nullable SurfaceTexture surfaceTexture, 143 | @NonNull ISurfaceTextureHost surfaceTextureHost) { 144 | mTextureView = textureView; 145 | mSurfaceTexture = surfaceTexture; 146 | mSurfaceTextureHost = surfaceTextureHost; 147 | } 148 | 149 | @TargetApi(Build.VERSION_CODES.JELLY_BEAN) 150 | public void bindToMediaPlayer(IMediaPlayer mp) { 151 | if (mp == null) 152 | return; 153 | 154 | if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) && 155 | (mp instanceof ISurfaceTextureHolder)) { 156 | ISurfaceTextureHolder textureHolder = (ISurfaceTextureHolder) mp; 157 | mTextureView.mSurfaceCallback.setOwnSurfaceTexture(false); 158 | 159 | SurfaceTexture surfaceTexture = textureHolder.getSurfaceTexture(); 160 | if (surfaceTexture != null) { 161 | mTextureView.setSurfaceTexture(surfaceTexture); 162 | } else { 163 | textureHolder.setSurfaceTexture(mSurfaceTexture); 164 | textureHolder.setSurfaceTextureHost(mTextureView.mSurfaceCallback); 165 | } 166 | } else { 167 | mp.setSurface(openSurface()); 168 | } 169 | } 170 | 171 | @NonNull 172 | @Override 173 | public IRenderView getRenderView() { 174 | return mTextureView; 175 | } 176 | 177 | @Nullable 178 | @Override 179 | public SurfaceHolder getSurfaceHolder() { 180 | return null; 181 | } 182 | 183 | @Nullable 184 | @Override 185 | public SurfaceTexture getSurfaceTexture() { 186 | return mSurfaceTexture; 187 | } 188 | 189 | @Nullable 190 | @Override 191 | public Surface openSurface() { 192 | if (mSurfaceTexture == null) 193 | return null; 194 | return new Surface(mSurfaceTexture); 195 | } 196 | } 197 | 198 | //------------------------- 199 | // SurfaceHolder.Callback 200 | //------------------------- 201 | 202 | @Override 203 | public void addRenderCallback(IRenderCallback callback) { 204 | mSurfaceCallback.addRenderCallback(callback); 205 | } 206 | 207 | @Override 208 | public void removeRenderCallback(IRenderCallback callback) { 209 | mSurfaceCallback.removeRenderCallback(callback); 210 | } 211 | 212 | private SurfaceCallback mSurfaceCallback; 213 | 214 | private static final class SurfaceCallback implements SurfaceTextureListener, ISurfaceTextureHost { 215 | private SurfaceTexture mSurfaceTexture; 216 | private boolean mIsFormatChanged; 217 | private int mWidth; 218 | private int mHeight; 219 | 220 | private boolean mOwnSurfaceTexture = true; 221 | private boolean mWillDetachFromWindow = false; 222 | private boolean mDidDetachFromWindow = false; 223 | 224 | private WeakReference mWeakRenderView; 225 | private Map mRenderCallbackMap = new ConcurrentHashMap(); 226 | 227 | public SurfaceCallback(@NonNull TextureRenderView renderView) { 228 | mWeakRenderView = new WeakReference(renderView); 229 | } 230 | 231 | public void setOwnSurfaceTexture(boolean ownSurfaceTexture) { 232 | mOwnSurfaceTexture = ownSurfaceTexture; 233 | } 234 | 235 | public void addRenderCallback(@NonNull IRenderCallback callback) { 236 | mRenderCallbackMap.put(callback, callback); 237 | 238 | ISurfaceHolder surfaceHolder = null; 239 | if (mSurfaceTexture != null) { 240 | if (surfaceHolder == null) 241 | surfaceHolder = new InternalSurfaceHolder(mWeakRenderView.get(), mSurfaceTexture, this); 242 | callback.onSurfaceCreated(surfaceHolder, mWidth, mHeight); 243 | } 244 | 245 | if (mIsFormatChanged) { 246 | if (surfaceHolder == null) 247 | surfaceHolder = new InternalSurfaceHolder(mWeakRenderView.get(), mSurfaceTexture, this); 248 | callback.onSurfaceChanged(surfaceHolder, 0, mWidth, mHeight); 249 | } 250 | } 251 | 252 | public void removeRenderCallback(@NonNull IRenderCallback callback) { 253 | mRenderCallbackMap.remove(callback); 254 | } 255 | 256 | @Override 257 | public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { 258 | mSurfaceTexture = surface; 259 | mIsFormatChanged = false; 260 | mWidth = 0; 261 | mHeight = 0; 262 | 263 | ISurfaceHolder surfaceHolder = new InternalSurfaceHolder(mWeakRenderView.get(), surface, this); 264 | for (IRenderCallback renderCallback : mRenderCallbackMap.keySet()) { 265 | renderCallback.onSurfaceCreated(surfaceHolder, 0, 0); 266 | } 267 | } 268 | 269 | @Override 270 | public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) { 271 | mSurfaceTexture = surface; 272 | mIsFormatChanged = true; 273 | mWidth = width; 274 | mHeight = height; 275 | 276 | ISurfaceHolder surfaceHolder = new InternalSurfaceHolder(mWeakRenderView.get(), surface, this); 277 | for (IRenderCallback renderCallback : mRenderCallbackMap.keySet()) { 278 | renderCallback.onSurfaceChanged(surfaceHolder, 0, width, height); 279 | } 280 | } 281 | 282 | @Override 283 | public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) { 284 | mSurfaceTexture = surface; 285 | mIsFormatChanged = false; 286 | mWidth = 0; 287 | mHeight = 0; 288 | 289 | ISurfaceHolder surfaceHolder = new InternalSurfaceHolder(mWeakRenderView.get(), surface, this); 290 | for (IRenderCallback renderCallback : mRenderCallbackMap.keySet()) { 291 | renderCallback.onSurfaceDestroyed(surfaceHolder); 292 | } 293 | 294 | Log.d(TAG, "onSurfaceTextureDestroyed: destroy: " + mOwnSurfaceTexture); 295 | return mOwnSurfaceTexture; 296 | } 297 | 298 | @Override 299 | public void onSurfaceTextureUpdated(SurfaceTexture surface) { 300 | } 301 | 302 | //------------------------- 303 | // ISurfaceTextureHost 304 | //------------------------- 305 | 306 | @Override 307 | public void releaseSurfaceTexture(SurfaceTexture surfaceTexture) { 308 | if (surfaceTexture == null) { 309 | Log.d(TAG, "releaseSurfaceTexture: null"); 310 | } else if (mDidDetachFromWindow) { 311 | if (surfaceTexture != mSurfaceTexture) { 312 | Log.d(TAG, "releaseSurfaceTexture: didDetachFromWindow(): release different SurfaceTexture"); 313 | surfaceTexture.release(); 314 | } else if (!mOwnSurfaceTexture) { 315 | Log.d(TAG, "releaseSurfaceTexture: didDetachFromWindow(): release detached SurfaceTexture"); 316 | surfaceTexture.release(); 317 | } else { 318 | Log.d(TAG, "releaseSurfaceTexture: didDetachFromWindow(): already released by TextureView"); 319 | } 320 | } else if (mWillDetachFromWindow) { 321 | if (surfaceTexture != mSurfaceTexture) { 322 | Log.d(TAG, "releaseSurfaceTexture: willDetachFromWindow(): release different SurfaceTexture"); 323 | surfaceTexture.release(); 324 | } else if (!mOwnSurfaceTexture) { 325 | Log.d(TAG, "releaseSurfaceTexture: willDetachFromWindow(): re-attach SurfaceTexture to TextureView"); 326 | setOwnSurfaceTexture(true); 327 | } else { 328 | Log.d(TAG, "releaseSurfaceTexture: willDetachFromWindow(): will released by TextureView"); 329 | } 330 | } else { 331 | if (surfaceTexture != mSurfaceTexture) { 332 | Log.d(TAG, "releaseSurfaceTexture: alive: release different SurfaceTexture"); 333 | surfaceTexture.release(); 334 | } else if (!mOwnSurfaceTexture) { 335 | Log.d(TAG, "releaseSurfaceTexture: alive: re-attach SurfaceTexture to TextureView"); 336 | setOwnSurfaceTexture(true); 337 | } else { 338 | Log.d(TAG, "releaseSurfaceTexture: alive: will released by TextureView"); 339 | } 340 | } 341 | } 342 | 343 | public void willDetachFromWindow() { 344 | Log.d(TAG, "willDetachFromWindow()"); 345 | mWillDetachFromWindow = true; 346 | } 347 | 348 | public void didDetachFromWindow() { 349 | Log.d(TAG, "didDetachFromWindow()"); 350 | mDidDetachFromWindow = true; 351 | } 352 | } 353 | 354 | //-------------------- 355 | // Accessibility 356 | //-------------------- 357 | 358 | @Override 359 | public void onInitializeAccessibilityEvent(AccessibilityEvent event) { 360 | super.onInitializeAccessibilityEvent(event); 361 | event.setClassName(TextureRenderView.class.getName()); 362 | } 363 | 364 | @Override 365 | public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { 366 | super.onInitializeAccessibilityNodeInfo(info); 367 | info.setClassName(TextureRenderView.class.getName()); 368 | } 369 | } 370 | -------------------------------------------------------------------------------- /VideoList/src/main/res/drawable/app_video_center_bg.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /VideoList/src/main/res/drawable/bg_seek.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /VideoList/src/main/res/drawable/bg_thumb.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /VideoList/src/main/res/layout/media_contoller.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 14 | 15 | 21 | 22 | 32 | 33 | 39 | 40 | 48 | 49 | 61 | 62 | 69 | 70 | 77 | 78 | 79 | 80 | 81 | -------------------------------------------------------------------------------- /VideoList/src/main/res/layout/table_media_info.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /VideoList/src/main/res/layout/table_media_info_row1.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 12 | 13 | 20 | 21 | -------------------------------------------------------------------------------- /VideoList/src/main/res/layout/table_media_info_row2.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 15 | 16 | 23 | 24 | -------------------------------------------------------------------------------- /VideoList/src/main/res/layout/table_media_info_section.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 14 | 15 | -------------------------------------------------------------------------------- /VideoList/src/main/res/layout/view_video_item.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 12 | 13 | 19 | 20 | 25 | 26 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /VideoList/src/main/res/mipmap-xhdpi/full_screen_icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuguohui/VideoDemo/53e7c8864be8a13e08b2410066eab78732086fe4/VideoList/src/main/res/mipmap-xhdpi/full_screen_icon.png -------------------------------------------------------------------------------- /VideoList/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuguohui/VideoDemo/53e7c8864be8a13e08b2410066eab78732086fe4/VideoList/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /VideoList/src/main/res/mipmap-xhdpi/sound_mult_icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuguohui/VideoDemo/53e7c8864be8a13e08b2410066eab78732086fe4/VideoList/src/main/res/mipmap-xhdpi/sound_mult_icon.png -------------------------------------------------------------------------------- /VideoList/src/main/res/mipmap-xhdpi/sound_open_icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuguohui/VideoDemo/53e7c8864be8a13e08b2410066eab78732086fe4/VideoList/src/main/res/mipmap-xhdpi/sound_open_icon.png -------------------------------------------------------------------------------- /VideoList/src/main/res/mipmap-xhdpi/video_play_btn.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuguohui/VideoDemo/53e7c8864be8a13e08b2410066eab78732086fe4/VideoList/src/main/res/mipmap-xhdpi/video_play_btn.png -------------------------------------------------------------------------------- /VideoList/src/main/res/mipmap-xhdpi/video_stop_btn.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuguohui/VideoDemo/53e7c8864be8a13e08b2410066eab78732086fe4/VideoList/src/main/res/mipmap-xhdpi/video_stop_btn.png -------------------------------------------------------------------------------- /VideoList/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /VideoList/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /VideoList/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 10dp 4 | 10dp 5 | 6 | -------------------------------------------------------------------------------- /VideoList/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | VideoListDemo 3 | 4 | 本地视频 5 | 在线视频 6 | 7 | 8 | loading... 9 | Click to load more 10 | All the data has been loaded 11 | It is empty. :( 12 | Load error, click to retry 13 | N/A 14 | Close 15 | Exit 16 | Sample 17 | Recent 18 | Settings 19 | Tracks 20 | Player 21 | Render 22 | Scale 23 | Info 24 | vdec 25 | fps 26 | v-cache 27 | a-cache 28 | 29 | Media Information 30 | Player 31 | Media 32 | Profile level 33 | Pixel format 34 | Resolution 35 | Length 36 | Stream #%d 37 | Type 38 | Language 39 | Codec 40 | Frame rate 41 | Bit rate 42 | Sample rate 43 | Channels 44 | * 45 | * 46 | 47 | Video 48 | Audio 49 | Subtitle 50 | Timed text 51 | Meta data 52 | Unknown 53 | 54 | Invalid progressive playback 55 | Unknown 56 | OK 57 | 58 | Aspect / Fit parent 59 | Aspect / Fill parent 60 | Aspect / Wrap content 61 | Free / Fill parent 62 | 16:9 / Fit parent 63 | 4:3 / Fit parent 64 | 65 | Render: None 66 | Render: SurfaceView 67 | Render: TextureView 68 | 69 | Player: None 70 | Player: AndroidMediaPlayer 71 | Player: IjkMediaPlayer 72 | Player: IjkExoMediaPlayer 73 | 74 | 75 | Please specify the address of the video playback 76 | 77 | Player encountered a small problem 78 | 79 | can not play this video 80 | 81 | player not support this device 82 | 83 | -------------------------------------------------------------------------------- /VideoList/src/main/res/values/strings_pref.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | General 5 | 6 | pref.enable_background_play 7 | Enable background play 8 | need Android 4.0+ 9 | 10 | pref.using_android_player 11 | Using system player 12 | 13 | 14 | pref.player 15 | Choose Player 16 | 17 | Auto Select 18 | AndroidMediaPlayer 19 | IjkMediaPlayer 20 | IjkExoMediaPlayer 21 | 22 | 23 | 0 24 | 1 25 | 2 26 | 3 27 | 28 | 29 | Auto Select 30 | AndroidMediaPlayer 31 | IjkMediaPlayer 32 | IjkExoMediaPlayer 33 | 34 | 35 | 36 | Video: ijkplayer 37 | 38 | pref.using_media_codec 39 | Using MediaCodec 40 | 41 | 42 | pref.using_media_codec_auto_rotate 43 | Using MediaCodec auto rotate 44 | 45 | 46 | pref.pixel_format 47 | Pixel Format 48 | 49 | Auto Select 50 | RGB 565 51 | RGB 888 52 | RGBX 8888 53 | YV12 54 | OpenGL ES2 55 | 56 | 57 | 58 | fcc-rv16 59 | fcc-rv24 60 | fcc-rv32 61 | fcc-yv12 62 | fcc-_es2 63 | 64 | 65 | Auto Select 66 | RGB 565 67 | RGB 888 68 | RGBX 8888 69 | YV12 70 | OpenGL ES2 71 | 72 | 73 | 74 | Audio: ijkplayer 75 | 76 | pref.using_opensl_es 77 | Using OpenSL ES 78 | 79 | 80 | 81 | RenderView 82 | 83 | pref.enable_no_view 84 | Enable NoView 85 | 86 | 87 | pref.enable_surface_view 88 | Enable SurfaceView 89 | 90 | 91 | pref.enable_texture_view 92 | Enable TextureView 93 | 94 | 95 | pref.enable_detached_surface_texture 96 | Enable detached SurfaceTexture 97 | 98 | 99 | 100 | 101 | 102 | 103 | -------------------------------------------------------------------------------- /VideoList/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /VideoList/src/test/java/com/trs/videolist/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.trs.videolist; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * To work on unit tests, switch the Test Artifact in the Build Variants view. 9 | */ 10 | public class ExampleUnitTest { 11 | @Test 12 | public void addition_isCorrect() throws Exception { 13 | assertEquals(4, 2 + 2); 14 | } 15 | } -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'me.tatarka.retrolambda' 3 | 4 | android { 5 | compileSdkVersion rootProject.ext.android.compileSdkVersion 6 | buildToolsVersion rootProject.ext.android.buildToolsVersion 7 | 8 | defaultConfig { 9 | minSdkVersion rootProject.ext.android.minSdkVersion 10 | targetSdkVersion rootProject.ext.android.targetSdkVersion 11 | versionCode rootProject.ext.android.versionCode 12 | versionName rootProject.ext.android.versionName 13 | applicationId "com.zhuguohui.videodemo" 14 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 15 | } 16 | 17 | buildTypes { 18 | release { 19 | minifyEnabled false 20 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 21 | } 22 | } 23 | compileOptions { 24 | targetCompatibility 1.8 25 | sourceCompatibility 1.8 26 | } 27 | } 28 | 29 | dependencies { 30 | compile fileTree(include: ['*.jar'], dir: 'libs') 31 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 32 | exclude group: 'com.android.support', module: 'support-annotations' 33 | }) 34 | compile 'com.android.support:appcompat-v7:25.1.0' 35 | testCompile 'junit:junit:4.12' 36 | compile project(':VideoList') 37 | compile 'com.android.support:design:25.1.0' 38 | //RxJava and Retrofit 39 | compile 'io.reactivex:rxjava:1.1.4' 40 | compile 'io.reactivex:rxandroid:1.1.0' 41 | compile 'com.google.code.gson:gson:2.8.0' 42 | compile 'com.github.bumptech.glide:glide:3.7.0' 43 | } 44 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in E:\Android_SDK/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/zhuguohui/videodemo/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.zhuguohui.videodemo; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.zhuguohui.videodemo", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 35 | 36 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /app/src/main/java/com/zhuguohui/videodemo/MyApp.java: -------------------------------------------------------------------------------- 1 | package com.zhuguohui.videodemo; 2 | 3 | import android.app.Application; 4 | import android.content.Intent; 5 | 6 | import com.zhuguohui.videodemo.service.NetworkStateService; 7 | import com.zhuguohui.videodemo.util.ToastUtil; 8 | import com.zhuguohui.videodemo.video.VideoPlayManager; 9 | 10 | /** 11 | * Created by zhuguohui on 2017/2/3. 12 | */ 13 | 14 | public class MyApp extends Application { 15 | @Override 16 | public void onCreate() { 17 | super.onCreate(); 18 | 19 | ToastUtil.initialize(this); 20 | //启动网络状态监听服务 21 | startService(new Intent(this, NetworkStateService.class)); 22 | //初始化视频播放管理器 23 | VideoPlayManager.init(this); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /app/src/main/java/com/zhuguohui/videodemo/activity/FullscreenActivity.java: -------------------------------------------------------------------------------- 1 | package com.zhuguohui.videodemo.activity; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.view.Window; 7 | import android.view.WindowManager; 8 | import android.widget.FrameLayout; 9 | 10 | import com.zhuguohui.videodemo.R; 11 | import com.zhuguohui.videodemo.rx.RxBus; 12 | import com.zhuguohui.videodemo.video.VideoPlayManager; 13 | 14 | 15 | /** 16 | * An example full-screen activity that shows and hides the system UI (i.e. 17 | * status bar and navigation/system bar) with user interaction. 18 | */ 19 | public class FullscreenActivity extends AppCompatActivity { 20 | 21 | private FrameLayout frameLayout; 22 | 23 | @Override 24 | protected void onCreate(@Nullable Bundle savedInstanceState) { 25 | super.onCreate(savedInstanceState); 26 | this.requestWindowFeature(Window.FEATURE_NO_TITLE); 27 | this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 28 | WindowManager.LayoutParams.FLAG_FULLSCREEN); 29 | setContentView(R.layout.activity_fullscreen); 30 | frameLayout = (FrameLayout) findViewById(R.id.full_holder); 31 | } 32 | 33 | @Override 34 | protected void onResume() { 35 | super.onResume(); 36 | RxBus.getDefault().post(new VideoPlayManager.PlayInViewEvent(frameLayout, VideoPlayManager.getPlayingItem())); 37 | } 38 | 39 | @Override 40 | protected void onPause() { 41 | super.onPause(); 42 | RxBus.getDefault().post(new VideoPlayManager.PlayVideoBackEvent()); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /app/src/main/java/com/zhuguohui/videodemo/activity/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.zhuguohui.videodemo.activity; 2 | 3 | import android.support.design.widget.TabLayout; 4 | import android.support.v4.view.ViewPager; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.os.Bundle; 7 | 8 | import com.zhuguohui.videodemo.R; 9 | import com.zhuguohui.videodemo.adapter.NewsTabAdapter; 10 | 11 | import java.util.ArrayList; 12 | import java.util.List; 13 | 14 | public class MainActivity extends AppCompatActivity { 15 | private ViewPager viewPager; 16 | private TabLayout tabLayout; 17 | private List titleList = new ArrayList<>(); 18 | 19 | { 20 | titleList.add("头条"); 21 | titleList.add("视频"); 22 | titleList.add("体育"); 23 | titleList.add("军事"); 24 | titleList.add("经济"); 25 | titleList.add("娱乐"); 26 | titleList.add("科技"); 27 | titleList.add("汽车"); 28 | } 29 | 30 | @Override 31 | protected void onCreate(Bundle savedInstanceState) { 32 | super.onCreate(savedInstanceState); 33 | setContentView(R.layout.activity_main); 34 | viewPager = (ViewPager) findViewById(R.id.viewPager); 35 | tabLayout = (TabLayout) findViewById(R.id.tabLayout); 36 | viewPager.setAdapter(new NewsTabAdapter(getSupportFragmentManager(), titleList)); 37 | tabLayout.setupWithViewPager(viewPager); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /app/src/main/java/com/zhuguohui/videodemo/activity/NewsDetailActivity.java: -------------------------------------------------------------------------------- 1 | package com.zhuguohui.videodemo.activity; 2 | 3 | import android.support.v7.app.AppCompatActivity; 4 | import android.os.Bundle; 5 | import android.widget.TextView; 6 | 7 | import com.zhuguohui.videodemo.R; 8 | 9 | public class NewsDetailActivity extends AppCompatActivity { 10 | public static final String KEY_TITLE = "key_title"; 11 | 12 | @Override 13 | protected void onCreate(Bundle savedInstanceState) { 14 | super.onCreate(savedInstanceState); 15 | setContentView(R.layout.activity_news_detail); 16 | if (getIntent() != null && getIntent().hasExtra(KEY_TITLE)) { 17 | String title = getIntent().getStringExtra(KEY_TITLE); 18 | TextView tv_title = (TextView) findViewById(R.id.tv_title); 19 | tv_title.setText(title); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /app/src/main/java/com/zhuguohui/videodemo/activity/VideoDetailActivity.java: -------------------------------------------------------------------------------- 1 | package com.zhuguohui.videodemo.activity; 2 | 3 | import android.support.v7.app.AppCompatActivity; 4 | import android.os.Bundle; 5 | import android.webkit.WebChromeClient; 6 | import android.webkit.WebResourceRequest; 7 | import android.webkit.WebResourceResponse; 8 | import android.webkit.WebView; 9 | import android.webkit.WebViewClient; 10 | import android.widget.FrameLayout; 11 | import android.widget.ImageView; 12 | 13 | import com.bumptech.glide.Glide; 14 | import com.zhuguohui.videodemo.R; 15 | import com.zhuguohui.videodemo.bean.VideoItem; 16 | import com.zhuguohui.videodemo.rx.RxBus; 17 | import com.zhuguohui.videodemo.video.VideoPlayManager; 18 | 19 | import java.io.Serializable; 20 | 21 | public class VideoDetailActivity extends AppCompatActivity { 22 | FrameLayout video_holder; 23 | ImageView iv_video; 24 | private VideoItem videoItem; 25 | public static String KEY_PLAY_ITEM = "key_play_item"; 26 | private boolean autoPlayVideo = false; 27 | private WebView webView; 28 | @Override 29 | protected void onCreate(Bundle savedInstanceState) { 30 | super.onCreate(savedInstanceState); 31 | setContentView(R.layout.activity_video_detail); 32 | iv_video = (ImageView) findViewById(R.id.iv_video); 33 | video_holder = (FrameLayout) findViewById(R.id.video_holder); 34 | Serializable obj = getIntent().getSerializableExtra(KEY_PLAY_ITEM); 35 | if (obj != null) { 36 | videoItem = (VideoItem) obj; 37 | autoPlayVideo = true; 38 | } else { 39 | //保存正在播放的视频信息,因为在这个界面重播的时候,需要这些信息 40 | videoItem = VideoPlayManager.getPlayingItem(); 41 | } 42 | //设置缩略图 43 | Glide.with(this).load(videoItem.getImgUrl()).into(iv_video); 44 | findViewById(R.id.iv_play).setOnClickListener(v -> { 45 | RxBus.getDefault().post(new VideoPlayManager.PlayInViewEvent(video_holder, videoItem)); 46 | }); 47 | //设置内容 48 | webView= (WebView) findViewById(R.id.webView); 49 | webView.loadUrl(videoItem.getDocUrl()); 50 | webView.setWebViewClient(new WebViewClient(){ 51 | @Override 52 | public boolean shouldOverrideUrlLoading(WebView view, String url) { 53 | view.loadUrl(url); 54 | return true; 55 | } 56 | }); 57 | 58 | } 59 | 60 | 61 | @Override 62 | protected void onResume() { 63 | super.onResume(); 64 | //当从全屏返回这个界面的时候,判断是否还有播放的item,防止再次自动播放 65 | //只有从列表或小窗进入这个界面的时候才自动播放 66 | if (VideoPlayManager.getPlayingItem() != null || autoPlayVideo) { 67 | autoPlayVideo = false; 68 | RxBus.getDefault().post(new VideoPlayManager.PlayInViewEvent(video_holder, videoItem)); 69 | } 70 | } 71 | 72 | @Override 73 | protected void onPause() { 74 | super.onPause(); 75 | RxBus.getDefault().post(new VideoPlayManager.PlayVideoBackEvent()); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /app/src/main/java/com/zhuguohui/videodemo/adapter/NewsListAdapter.java: -------------------------------------------------------------------------------- 1 | package com.zhuguohui.videodemo.adapter; 2 | 3 | import android.content.Intent; 4 | import android.support.v7.widget.RecyclerView; 5 | import android.view.LayoutInflater; 6 | import android.view.View; 7 | import android.view.ViewGroup; 8 | import android.widget.TextView; 9 | 10 | import com.zhuguohui.videodemo.R; 11 | import com.zhuguohui.videodemo.activity.NewsDetailActivity; 12 | 13 | import java.util.ArrayList; 14 | import java.util.List; 15 | 16 | /** 17 | * Created by zhuguohui on 2017/2/3. 18 | */ 19 | 20 | public class NewsListAdapter extends RecyclerView.Adapter { 21 | 22 | public NewsListAdapter(String title) { 23 | this.title = title; 24 | createdData(); 25 | } 26 | 27 | private void createdData() { 28 | 29 | for (int i = 1; i <= 20; i++) { 30 | data.add(title + "新闻" + i); 31 | } 32 | } 33 | 34 | private List data = new ArrayList<>(); 35 | 36 | 37 | private String title; 38 | 39 | @Override 40 | public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 41 | View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_news, parent, false); 42 | return new MyViewHolder(view); 43 | } 44 | 45 | @Override 46 | public void onBindViewHolder(MyViewHolder holder, int position) { 47 | holder.tv_title.setText(data.get(position)); 48 | holder.itemView.setOnClickListener(v -> { 49 | Intent intent = new Intent(holder.itemView.getContext(), NewsDetailActivity.class); 50 | intent.putExtra(NewsDetailActivity.KEY_TITLE, data.get(position)); 51 | holder.itemView.getContext().startActivity(intent); 52 | }); 53 | } 54 | 55 | @Override 56 | public int getItemCount() { 57 | return data.size(); 58 | } 59 | 60 | class MyViewHolder extends RecyclerView.ViewHolder { 61 | public TextView tv_title; 62 | 63 | public MyViewHolder(View itemView) { 64 | super(itemView); 65 | tv_title = (TextView) itemView.findViewById(R.id.tv_title); 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /app/src/main/java/com/zhuguohui/videodemo/adapter/NewsTabAdapter.java: -------------------------------------------------------------------------------- 1 | package com.zhuguohui.videodemo.adapter; 2 | 3 | import android.os.Bundle; 4 | import android.support.v4.app.Fragment; 5 | import android.support.v4.app.FragmentManager; 6 | import android.support.v4.app.FragmentPagerAdapter; 7 | 8 | import com.zhuguohui.videodemo.fragment.NewsListFragment; 9 | import com.zhuguohui.videodemo.fragment.VideoListFragment; 10 | 11 | import java.util.ArrayList; 12 | import java.util.List; 13 | 14 | /** 15 | * Created by zhuguohui on 2017/2/3. 16 | */ 17 | 18 | public class NewsTabAdapter extends FragmentPagerAdapter { 19 | 20 | private List titleList = new ArrayList<>(); 21 | 22 | public NewsTabAdapter(FragmentManager fm, List titleList) { 23 | super(fm); 24 | this.titleList = titleList; 25 | } 26 | 27 | @Override 28 | public Fragment getItem(int position) { 29 | String title = titleList.get(position); 30 | if ("视频".equals(title)) { 31 | return new VideoListFragment(); 32 | } 33 | Fragment fragment = new NewsListFragment(); 34 | Bundle bundle = new Bundle(); 35 | bundle.putString(NewsListFragment.KEY_TITLE, titleList.get(position)); 36 | fragment.setArguments(bundle); 37 | return fragment; 38 | } 39 | 40 | @Override 41 | public int getCount() { 42 | return titleList.size(); 43 | } 44 | 45 | @Override 46 | public CharSequence getPageTitle(int position) { 47 | return titleList.get(position); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /app/src/main/java/com/zhuguohui/videodemo/adapter/VideoAdapter.java: -------------------------------------------------------------------------------- 1 | package com.zhuguohui.videodemo.adapter; 2 | 3 | import android.content.Context; 4 | import android.content.Intent; 5 | import android.support.v7.widget.RecyclerView; 6 | import android.view.LayoutInflater; 7 | import android.view.View; 8 | import android.view.ViewGroup; 9 | import android.widget.FrameLayout; 10 | import android.widget.ImageView; 11 | import android.widget.TextView; 12 | 13 | import com.bumptech.glide.Glide; 14 | import com.zhuguohui.videodemo.R; 15 | import com.zhuguohui.videodemo.activity.VideoDetailActivity; 16 | import com.zhuguohui.videodemo.bean.VideoItem; 17 | import com.zhuguohui.videodemo.bean.VideoPage; 18 | import com.zhuguohui.videodemo.rx.RxBus; 19 | import com.zhuguohui.videodemo.video.VideoPlayManager; 20 | 21 | /** 22 | * Created by zhuguohui on 2017/2/3. 23 | */ 24 | 25 | public class VideoAdapter extends RecyclerView.Adapter { 26 | VideoPage videoPage; 27 | 28 | public VideoAdapter(VideoPage videoPage) { 29 | this.videoPage = videoPage; 30 | } 31 | 32 | 33 | @Override 34 | public VideoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 35 | View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_video, parent, false); 36 | return new VideoViewHolder(view); 37 | } 38 | 39 | @Override 40 | public void onBindViewHolder(VideoViewHolder holder, int position) { 41 | final VideoItem videoItem = videoPage.getData().get(position); 42 | holder.tv_title.setText(videoItem.getTitle()); 43 | Glide.with(holder.itemView.getContext()).load(videoItem.getImgUrl()).into(holder.iv_video); 44 | holder.layout_holder.setTag(videoItem); 45 | holder.layout_holder.setOnClickListener(v -> { 46 | //如果当前视频没有播放,则播放 47 | RxBus.getDefault().post(new VideoPlayManager.PlayInViewEvent(holder.layout_holder, videoItem, true)); 48 | }); 49 | holder.itemView.setOnClickListener(v -> videoClickListener.onVideoClick(holder.itemView.getContext(), videoItem)); 50 | } 51 | 52 | VideoClickListener videoClickListener = new VideoClickListener(); 53 | 54 | public static class VideoClickListener { 55 | public void onVideoClick(Context context, VideoItem item) { 56 | Intent intent = new Intent(context, VideoDetailActivity.class); 57 | intent.putExtra(VideoDetailActivity.KEY_PLAY_ITEM, item); 58 | context.startActivity(intent); 59 | } 60 | } 61 | 62 | @Override 63 | public int getItemCount() { 64 | return videoPage.getData().size(); 65 | } 66 | 67 | public class VideoViewHolder extends RecyclerView.ViewHolder { 68 | 69 | TextView tv_title; 70 | ImageView iv_video; 71 | FrameLayout layout_holder; 72 | 73 | public VideoViewHolder(View itemView) { 74 | super(itemView); 75 | tv_title = (TextView) itemView.findViewById(R.id.tv_news_title); 76 | iv_video = (ImageView) itemView.findViewById(R.id.iv_video); 77 | layout_holder = (FrameLayout) itemView.findViewById(R.id.layout_video_holder); 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /app/src/main/java/com/zhuguohui/videodemo/bean/VideoItem.java: -------------------------------------------------------------------------------- 1 | package com.zhuguohui.videodemo.bean; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | * Created by zhuguohui on 2017/2/3. 7 | */ 8 | 9 | public class VideoItem implements Serializable{ 10 | /** 11 | * title : 西藏民俗年味浓 古老歌舞首秀新年舞台 12 | * imgUrl : http://www.xzcmvideo.cn//masvod/public/2017/01/28/11299.images/v11299_b1485583569795_app.jpg 13 | * videoUrl : http://www.xzcmvideo.cn//masvod/public/2017/01/28/20170128_159e3ae0361_r1_300k.mp4 14 | * docUrl : http://news.hexun.com/2017-01-27/187953027.html 15 | */ 16 | 17 | private String title; 18 | private String imgUrl; 19 | private String videoUrl; 20 | private String docUrl; 21 | 22 | public String getTitle() { 23 | return title; 24 | } 25 | 26 | public void setTitle(String title) { 27 | this.title = title; 28 | } 29 | 30 | public String getImgUrl() { 31 | return imgUrl; 32 | } 33 | 34 | public void setImgUrl(String imgUrl) { 35 | this.imgUrl = imgUrl; 36 | } 37 | 38 | public String getVideoUrl() { 39 | return videoUrl; 40 | } 41 | 42 | public void setVideoUrl(String videoUrl) { 43 | this.videoUrl = videoUrl; 44 | } 45 | 46 | public String getDocUrl() { 47 | return docUrl; 48 | } 49 | 50 | public void setDocUrl(String docUrl) { 51 | this.docUrl = docUrl; 52 | } 53 | 54 | @Override 55 | public boolean equals(Object obj) { 56 | if(obj==null){ 57 | return false; 58 | } 59 | 60 | if(!(obj instanceof VideoItem)){ 61 | return false; 62 | } 63 | 64 | VideoItem other= (VideoItem) obj; 65 | return other.getTitle().equals(getTitle())&&other.getDocUrl().equals(getDocUrl())&&other.getVideoUrl().equals(getVideoUrl()); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /app/src/main/java/com/zhuguohui/videodemo/bean/VideoPage.java: -------------------------------------------------------------------------------- 1 | package com.zhuguohui.videodemo.bean; 2 | 3 | import java.util.List; 4 | 5 | /** 6 | * Created by zhuguohui on 2017/2/3. 7 | */ 8 | 9 | public class VideoPage { 10 | 11 | private List data; 12 | 13 | public List getData() { 14 | return data; 15 | } 16 | 17 | public void setData(List data) { 18 | this.data = data; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /app/src/main/java/com/zhuguohui/videodemo/fragment/NewsListFragment.java: -------------------------------------------------------------------------------- 1 | package com.zhuguohui.videodemo.fragment; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.support.v7.widget.RecyclerView; 6 | 7 | import com.zhuguohui.videodemo.adapter.NewsListAdapter; 8 | import com.zhuguohui.videodemo.fragment.base.BaseListFragment; 9 | 10 | /** 11 | * Created by zhuguohui on 2017/2/3. 12 | */ 13 | 14 | public class NewsListFragment extends BaseListFragment { 15 | private String title = ""; 16 | public static final String KEY_TITLE = "key_title"; 17 | private NewsListAdapter adapter; 18 | 19 | @Override 20 | public void onCreate(@Nullable Bundle savedInstanceState) { 21 | super.onCreate(savedInstanceState); 22 | if (getArguments() != null && getArguments().containsKey(KEY_TITLE)) { 23 | title = getArguments().getString(KEY_TITLE); 24 | } 25 | 26 | } 27 | 28 | 29 | @Override 30 | protected RecyclerView.Adapter getAdapter() { 31 | return adapter == null ? adapter = new NewsListAdapter(title) : adapter; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /app/src/main/java/com/zhuguohui/videodemo/fragment/VideoListFragment.java: -------------------------------------------------------------------------------- 1 | package com.zhuguohui.videodemo.fragment; 2 | 3 | import android.content.res.Resources; 4 | import android.support.v7.widget.RecyclerView; 5 | 6 | import com.google.gson.Gson; 7 | import com.zhuguohui.videodemo.R; 8 | import com.zhuguohui.videodemo.adapter.VideoAdapter; 9 | import com.zhuguohui.videodemo.bean.VideoPage; 10 | import com.zhuguohui.videodemo.fragment.base.BaseListFragment; 11 | 12 | import java.io.IOException; 13 | import java.io.InputStream; 14 | 15 | /** 16 | * Created by zhuguohui on 2017/2/3. 17 | */ 18 | 19 | public class VideoListFragment extends BaseListFragment { 20 | 21 | VideoAdapter adapter; 22 | 23 | 24 | @Override 25 | protected RecyclerView.Adapter getAdapter() { 26 | if (adapter == null) { 27 | String jsonStr = readRawFile(); 28 | Gson gson = new Gson(); 29 | VideoPage videoPage = gson.fromJson(jsonStr, VideoPage.class); 30 | adapter = new VideoAdapter(videoPage); 31 | } 32 | return adapter; 33 | } 34 | 35 | String readRawFile() { 36 | String content = ""; 37 | Resources resources = getContext().getResources(); 38 | InputStream is = null; 39 | try { 40 | is = resources.openRawResource(R.raw.video_list); 41 | byte buffer[] = new byte[is.available()]; 42 | is.read(buffer); 43 | content = new String(buffer); 44 | 45 | } catch (IOException e) { 46 | e.printStackTrace(); 47 | } finally { 48 | if (is != null) { 49 | try { 50 | is.close(); 51 | } catch (IOException e) { 52 | e.printStackTrace(); 53 | } 54 | } 55 | } 56 | return content; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /app/src/main/java/com/zhuguohui/videodemo/fragment/base/BaseListFragment.java: -------------------------------------------------------------------------------- 1 | package com.zhuguohui.videodemo.fragment.base; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.support.v4.app.Fragment; 6 | import android.support.v7.widget.LinearLayoutManager; 7 | import android.support.v7.widget.RecyclerView; 8 | import android.view.LayoutInflater; 9 | import android.view.View; 10 | import android.view.ViewGroup; 11 | 12 | import com.zhuguohui.videodemo.R; 13 | import com.zhuguohui.videodemo.view.DividerItemDecoration; 14 | 15 | /** 16 | * Created by zhuguohui on 2017/2/3. 17 | */ 18 | 19 | public abstract class BaseListFragment extends Fragment { 20 | private RecyclerView recyclerView; 21 | 22 | 23 | @Nullable 24 | @Override 25 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 26 | return inflater.inflate(R.layout.fragment_simple_news_list, container, false); 27 | } 28 | 29 | @Override 30 | public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 31 | recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView); 32 | recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); 33 | recyclerView.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL_LIST)); 34 | recyclerView.setAdapter(getAdapter()); 35 | } 36 | 37 | protected abstract RecyclerView.Adapter getAdapter(); 38 | } 39 | -------------------------------------------------------------------------------- /app/src/main/java/com/zhuguohui/videodemo/rx/RxBus.java: -------------------------------------------------------------------------------- 1 | package com.zhuguohui.videodemo.rx; 2 | 3 | import rx.Observable; 4 | import rx.subjects.PublishSubject; 5 | import rx.subjects.SerializedSubject; 6 | import rx.subjects.Subject; 7 | 8 | /** 9 | * Created by Vincent Woo 10 | * Date: 2016/5/4 11 | * Time: 16:51 12 | */ 13 | public class RxBus { 14 | private static volatile RxBus defaultInstance; 15 | // 主题 16 | private final Subject bus; 17 | 18 | // PublishSubject只会把在订阅发生的时间点之后来自原始Observable的数据发射给观察者 19 | public RxBus() { 20 | bus = new SerializedSubject<>(PublishSubject.create()); 21 | } 22 | 23 | // 单例RxBus 24 | public static RxBus getDefault() { 25 | RxBus rxBus = defaultInstance; 26 | if (defaultInstance == null) { 27 | synchronized (RxBus.class) { 28 | rxBus = defaultInstance; 29 | if (defaultInstance == null) { 30 | rxBus = new RxBus(); 31 | defaultInstance = rxBus; 32 | } 33 | } 34 | } 35 | return rxBus; 36 | } 37 | // 提供了一个新的事件 38 | public void post (Object o) { 39 | bus.onNext(o); 40 | } 41 | 42 | // 根据传递的 eventType 类型返回特定类型(eventType)的 被观察者 43 | public Observable toObserverable (Class eventType) { 44 | return bus.ofType(eventType); 45 | // ofType = filter + cast 46 | // return bus.filter(new Func1() { 47 | // @Override 48 | // public Boolean call(Object o) { 49 | // return eventType.isInstance(o); 50 | // } 51 | // }) .cast(eventType); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /app/src/main/java/com/zhuguohui/videodemo/service/NetworkStateService.java: -------------------------------------------------------------------------------- 1 | package com.zhuguohui.videodemo.service; 2 | 3 | import android.app.Service; 4 | import android.content.BroadcastReceiver; 5 | import android.content.Context; 6 | import android.content.Intent; 7 | import android.content.IntentFilter; 8 | import android.net.ConnectivityManager; 9 | import android.net.NetworkInfo; 10 | import android.os.IBinder; 11 | 12 | import com.zhuguohui.videodemo.rx.RxBus; 13 | import com.zhuguohui.videodemo.util.ToastUtil; 14 | 15 | 16 | /** 17 | * 监听网络状态改变的服务 18 | * Created by zhuguohui on 2017/1/18. 19 | */ 20 | 21 | public class NetworkStateService extends Service { 22 | 23 | // Class that answers queries about the state of network connectivity. 24 | // 系统网络连接相关的操作管理类. 25 | 26 | private ConnectivityManager connectivityManager; 27 | // Describes the status of a network interface. 28 | // 网络状态信息的实例 29 | private NetworkInfo info; 30 | 31 | /** 32 | * 当前处于的网络 33 | * 0 :null 34 | * 1 :2G/3G 35 | * 2 :wifi 36 | */ 37 | public static int networkStatus; 38 | 39 | 40 | /** 41 | * 广播实例 42 | */ 43 | private BroadcastReceiver mReceiver = new BroadcastReceiver() { 44 | @Override 45 | public void onReceive(Context context, Intent intent) { 46 | 47 | String action = intent.getAction(); //当前接受到的广播的标识(行动/意图) 48 | 49 | // 当当前接受到的广播的标识(意图)为网络状态的标识时做相应判断 50 | if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) { 51 | // 获取网络连接管理器 52 | connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 53 | 54 | // 获取当前网络状态信息 55 | info = connectivityManager.getActiveNetworkInfo(); 56 | 57 | if (info != null && info.isAvailable()) { 58 | 59 | //当NetworkInfo不为空且是可用的情况下,获取当前网络的Type状态 60 | //根据NetworkInfo.getTypeName()判断当前网络 61 | String name = info.getTypeName(); 62 | 63 | //更改NetworkStateService的静态变量,之后只要在Activity中进行判断就好了 64 | if (name.equals("WIFI")) { 65 | networkStatus = 2; 66 | RxBus.getDefault().post(new NetStateChangeEvent(NetStateChangeEvent.NetState.NET_WIFI)); 67 | } else { 68 | networkStatus = 1; 69 | RxBus.getDefault().post(new NetStateChangeEvent(NetStateChangeEvent.NetState.NET_4G)); 70 | } 71 | 72 | } else { 73 | 74 | // NetworkInfo为空或者是不可用的情况下 75 | networkStatus = 0; 76 | 77 | RxBus.getDefault().post(new NetStateChangeEvent(NetStateChangeEvent.NetState.NET_NO)); 78 | // Toast.makeText(context, "没有可用网络!\n请连接网络后刷新本界面", Toast.LENGTH_SHORT).show(); 79 | ToastUtil.getInstance().showToast("网络不可用"); 80 | 81 | } 82 | } 83 | } 84 | }; 85 | 86 | public static class NetStateChangeEvent { 87 | public enum NetState { 88 | NET_NO, NET_WIFI, NET_4G 89 | } 90 | 91 | NetState state; 92 | 93 | public NetStateChangeEvent(NetState state) { 94 | this.state = state; 95 | } 96 | 97 | public NetState getState() { 98 | return state; 99 | } 100 | } 101 | 102 | @Override 103 | public IBinder onBind(Intent intent) { 104 | return null; 105 | } 106 | 107 | @Override 108 | public void onCreate() { 109 | super.onCreate(); 110 | //注册网络状态的广播,绑定到mReceiver 111 | IntentFilter mFilter = new IntentFilter(); 112 | mFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); 113 | registerReceiver(mReceiver, mFilter); 114 | } 115 | 116 | @Override 117 | public void onDestroy() { 118 | super.onDestroy(); 119 | //注销接收 120 | unregisterReceiver(mReceiver); 121 | } 122 | 123 | @Override 124 | public int onStartCommand(Intent intent, int flags, int startId) { 125 | return super.onStartCommand(intent, flags, startId); 126 | } 127 | 128 | /** 129 | * 判断网络是否可用 130 | */ 131 | public static boolean isNetworkAvailable(Context context) { 132 | // 获取网络连接管理器 133 | ConnectivityManager mgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 134 | 135 | // 获取当前网络状态信息 136 | NetworkInfo[] info = mgr.getAllNetworkInfo(); 137 | if (info != null) { 138 | for (int i = 0; i < info.length; i++) { 139 | if (info[i].getState() == NetworkInfo.State.CONNECTED) { 140 | return true; 141 | } 142 | } 143 | } 144 | 145 | return false; 146 | } 147 | } 148 | -------------------------------------------------------------------------------- /app/src/main/java/com/zhuguohui/videodemo/util/AppUtil.java: -------------------------------------------------------------------------------- 1 | package com.zhuguohui.videodemo.util; 2 | 3 | import android.app.Activity; 4 | import android.app.AlertDialog; 5 | import android.content.ComponentName; 6 | import android.content.Context; 7 | import android.content.DialogInterface; 8 | import android.content.Intent; 9 | import android.content.pm.ApplicationInfo; 10 | import android.content.pm.PackageInfo; 11 | import android.content.pm.PackageManager; 12 | import android.content.pm.PackageManager.NameNotFoundException; 13 | import android.net.ConnectivityManager; 14 | import android.net.NetworkInfo; 15 | import android.os.Bundle; 16 | import android.os.Environment; 17 | import android.telephony.TelephonyManager; 18 | import android.text.TextUtils; 19 | import android.util.DisplayMetrics; 20 | import android.util.Log; 21 | import android.view.inputmethod.InputMethodManager; 22 | 23 | public class AppUtil { 24 | private Context mContext; 25 | private static AppUtil mInstance; 26 | 27 | public static AppUtil getInstance() { 28 | return mInstance; 29 | } 30 | 31 | public static void initialize(Context ctx) { 32 | mInstance = new AppUtil(ctx); 33 | } 34 | 35 | private AppUtil(Context ctx) { 36 | mContext = ctx; 37 | } 38 | 39 | public static int getWidth(Activity context) { 40 | DisplayMetrics dm = new DisplayMetrics(); 41 | context.getWindowManager().getDefaultDisplay().getMetrics(dm); 42 | return dm.widthPixels; 43 | } 44 | 45 | public static int getHeight(Context context) { 46 | return context.getResources().getDisplayMetrics().heightPixels; 47 | 48 | } 49 | 50 | /** 51 | * 根据手机的分辨率从 dp 的单位 转成为 px(像素) 52 | */ 53 | public static int dip2px(Context context, float dpValue) { 54 | final float scale = context.getResources().getDisplayMetrics().density; 55 | return (int) (dpValue * scale + 0.5f); 56 | } 57 | 58 | /** 59 | * 根据手机的分辨率从 px(像素) 的单位 转成为 dp 60 | */ 61 | public static int px2dip(Context context, float pxValue) { 62 | final float scale = context.getResources().getDisplayMetrics().density; 63 | return (int) (pxValue / scale + 0.5f); 64 | } 65 | 66 | /** 67 | * 根据手机的分辨率从 px(像素) 的单位 转成为 sp 68 | */ 69 | public static int px2sp(Context context, float pxValue) { 70 | final float scale = context.getResources().getDisplayMetrics().density; 71 | return (int) (pxValue / scale + 0.5f); 72 | } 73 | 74 | /** 75 | * 根据手机的分辨率从 sp(像素) 的单位 转成为 px 76 | */ 77 | public static int sp2px(Context context, float spValue) { 78 | final float scale = context.getResources().getDisplayMetrics().density; 79 | return (int) (spValue * scale + 0.5f); 80 | } 81 | 82 | /** 83 | * 获取App安装包信息 84 | * 85 | * @return 86 | */ 87 | public static PackageInfo getPackageInfo(Context context) { 88 | PackageInfo info = null; 89 | try { 90 | info = context.getPackageManager().getPackageInfo( 91 | context.getPackageName(), 0); 92 | } catch (NameNotFoundException e) { 93 | e.printStackTrace(System.err); 94 | } 95 | if (info == null) 96 | info = new PackageInfo(); 97 | return info; 98 | } 99 | 100 | public static boolean isStorageExists() { 101 | if (Environment.getExternalStorageState().equals( 102 | Environment.MEDIA_MOUNTED)) { 103 | return true; 104 | } 105 | return false; 106 | } 107 | 108 | public static void hideKkeyboard(Activity activity) { 109 | try { 110 | ((InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE)) 111 | .hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); 112 | } catch (Exception ex) { 113 | ex.printStackTrace(); 114 | } 115 | } 116 | 117 | /** 118 | * 判断网络是否正常连接 119 | * 120 | * @return boolean true:网络连接正常 false:网络连接不正常 121 | */ 122 | public boolean isNetworkConnected() { 123 | if (null == mContext) { 124 | return false; 125 | } 126 | // 获取手机所有连接管理对象(包括wifi,net等连接的管理) 127 | ConnectivityManager connectivityManager = (ConnectivityManager) mContext 128 | .getSystemService(Context.CONNECTIVITY_SERVICE); 129 | if (connectivityManager != null) { 130 | // 获取网络连接管理的对象 131 | NetworkInfo info = connectivityManager.getActiveNetworkInfo(); 132 | if (info != null && info.isConnected()) { 133 | // 判断当前网络是否已经连接 134 | if (info.getState() == NetworkInfo.State.CONNECTED) { 135 | return true; 136 | } 137 | } 138 | } 139 | return false; 140 | } 141 | 142 | /** 143 | * 没有网络 144 | */ 145 | public static final int NETWORKTYPE_INVALID = 0; 146 | /** 147 | * wap网络 148 | */ 149 | public static final int NETWORKTYPE_WAP = 1; 150 | /** 151 | * 2G网络 152 | */ 153 | public static final int NETWORKTYPE_2G = 2; 154 | /** 155 | * 3G和3G以上网络,或统称为快速网络 156 | */ 157 | public static final int NETWORKTYPE_3G = 3; 158 | /** 159 | * wifi网络 160 | */ 161 | public static final int NETWORKTYPE_WIFI = 4; 162 | 163 | /** 164 | * 获取网络状态,wifi,wap,2g,3g. 165 | * 166 | * @param context 上下文 167 | * @return int 网络状态 {@link #NETWORKTYPE_2G},{@link #NETWORKTYPE_3G}, 168 | * {@link #NETWORKTYPE_INVALID},{@link #NETWORKTYPE_WAP}* {@link #NETWORKTYPE_WIFI} 169 | */ 170 | public static int getNetWorkType(Context context) { 171 | int mNetWorkType = NETWORKTYPE_INVALID; 172 | ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 173 | NetworkInfo networkInfo = manager.getActiveNetworkInfo(); 174 | if (networkInfo != null && networkInfo.isConnected()) { 175 | String type = networkInfo.getTypeName(); 176 | if (type.equalsIgnoreCase("WIFI")) { 177 | mNetWorkType = NETWORKTYPE_WIFI; 178 | } else if (type.equalsIgnoreCase("MOBILE")) { 179 | String proxyHost = android.net.Proxy.getDefaultHost(); 180 | mNetWorkType = TextUtils.isEmpty(proxyHost) 181 | ? (isFastMobileNetwork(context) ? NETWORKTYPE_3G : NETWORKTYPE_2G) 182 | : NETWORKTYPE_WAP; 183 | } 184 | } else { 185 | mNetWorkType = NETWORKTYPE_INVALID; 186 | } 187 | return mNetWorkType; 188 | } 189 | 190 | private static boolean isFastMobileNetwork(Context context) { 191 | TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 192 | switch (telephonyManager.getNetworkType()) { 193 | case TelephonyManager.NETWORK_TYPE_1xRTT: 194 | return false; // ~ 50-100 kbps 195 | case TelephonyManager.NETWORK_TYPE_CDMA: 196 | return false; // ~ 14-64 kbps 197 | case TelephonyManager.NETWORK_TYPE_EDGE: 198 | return false; // ~ 50-100 kbps 199 | case TelephonyManager.NETWORK_TYPE_EVDO_0: 200 | return true; // ~ 400-1000 kbps 201 | case TelephonyManager.NETWORK_TYPE_EVDO_A: 202 | return true; // ~ 600-1400 kbps 203 | case TelephonyManager.NETWORK_TYPE_GPRS: 204 | return false; // ~ 100 kbps 205 | case TelephonyManager.NETWORK_TYPE_HSDPA: 206 | return true; // ~ 2-14 Mbps 207 | case TelephonyManager.NETWORK_TYPE_HSPA: 208 | return true; // ~ 700-1700 kbps 209 | case TelephonyManager.NETWORK_TYPE_HSUPA: 210 | return true; // ~ 1-23 Mbps 211 | case TelephonyManager.NETWORK_TYPE_UMTS: 212 | return true; // ~ 400-7000 kbps 213 | case TelephonyManager.NETWORK_TYPE_EHRPD: 214 | return true; // ~ 1-2 Mbps 215 | case TelephonyManager.NETWORK_TYPE_EVDO_B: 216 | return true; // ~ 5 Mbps 217 | case TelephonyManager.NETWORK_TYPE_HSPAP: 218 | return true; // ~ 10-20 Mbps 219 | case TelephonyManager.NETWORK_TYPE_IDEN: 220 | return false; // ~25 kbps 221 | case TelephonyManager.NETWORK_TYPE_LTE: 222 | return true; // ~ 10+ Mbps 223 | case TelephonyManager.NETWORK_TYPE_UNKNOWN: 224 | return false; 225 | default: 226 | return false; 227 | } 228 | } 229 | 230 | /* 231 | * 打开设置网络界面 232 | */ 233 | public static void setNetworkMethod(final Context context) { 234 | // 提示对话框 235 | AlertDialog.Builder builder = new AlertDialog.Builder(context); 236 | builder.setCancelable(false); 237 | try { 238 | builder.setTitle("网络设置提示") 239 | .setMessage("网络连接不可用,是否进行设置?") 240 | .setPositiveButton("设置", 241 | new DialogInterface.OnClickListener() { 242 | 243 | @Override 244 | public void onClick(DialogInterface dialog, 245 | int which) { 246 | Intent intent = null; 247 | // 判断手机系统的版本 即API大于10 就是3.0或以上版本 248 | if (android.os.Build.VERSION.SDK_INT > 10) { 249 | intent = new Intent( 250 | android.provider.Settings.ACTION_WIRELESS_SETTINGS); 251 | } else { 252 | intent = new Intent(); 253 | ComponentName component = new ComponentName( 254 | "com.android.settings", 255 | "com.android.settings.WirelessSettings"); 256 | intent.setComponent(component); 257 | intent.setAction("android.intent.action.VIEW"); 258 | } 259 | context.startActivity(intent); 260 | } 261 | }) 262 | .setNegativeButton("取消", 263 | new DialogInterface.OnClickListener() { 264 | 265 | @Override 266 | public void onClick(DialogInterface dialog, 267 | int which) { 268 | dialog.dismiss(); 269 | } 270 | }).show(); 271 | } catch (Exception e) { 272 | e.printStackTrace(); 273 | } 274 | } 275 | 276 | public String getMetaData(String name, String def) { 277 | try { 278 | ApplicationInfo ai = mContext.getPackageManager() 279 | .getApplicationInfo(mContext.getPackageName(), PackageManager.GET_META_DATA); 280 | Bundle bundle = ai.metaData; 281 | return bundle.getString(name); 282 | } catch (NameNotFoundException e) { 283 | Log.e("AppUtil", "Failed to load meta-data, NameNotFound: " + e.getMessage()); 284 | return def; 285 | } catch (NullPointerException e) { 286 | Log.e("AppUtil", "Failed to load meta-data, NullPointer: " + e.getMessage()); 287 | return def; 288 | } 289 | } 290 | 291 | public static String getUrlFileName(String url) { 292 | return url.substring(url.lastIndexOf("/") + 1); 293 | } 294 | 295 | public static String getUrlPath(String url) { 296 | return url.substring(0, url.lastIndexOf("/") + 1); 297 | } 298 | 299 | 300 | } 301 | -------------------------------------------------------------------------------- /app/src/main/java/com/zhuguohui/videodemo/util/ToastUtil.java: -------------------------------------------------------------------------------- 1 | package com.zhuguohui.videodemo.util; 2 | 3 | import android.content.Context; 4 | import android.graphics.drawable.Drawable; 5 | import android.view.Gravity; 6 | import android.view.View; 7 | import android.widget.TextView; 8 | import android.widget.Toast; 9 | 10 | import com.zhuguohui.videodemo.R; 11 | 12 | 13 | /** 14 | * Created by Vincent Woo 15 | * Date: 2016/1/22 16 | * Time: 17:27 17 | */ 18 | public class ToastUtil { 19 | private Context mContext; 20 | private static ToastUtil mInstance; 21 | private Toast mToast; 22 | private TYPE type = TYPE.TYPE_WARING; 23 | private TextView mTextView; 24 | 25 | private enum TYPE { 26 | TYPE_OK, TYPE_WARING, TYPE_COLLECT, TYPE_CANCEL_COLLECT; 27 | } 28 | 29 | public static ToastUtil getInstance() { 30 | return mInstance; 31 | } 32 | 33 | public static void initialize(Context ctx) { 34 | mInstance = new ToastUtil(ctx); 35 | } 36 | 37 | private ToastUtil(Context ctx) { 38 | mContext = ctx; 39 | } 40 | 41 | public void showToast(String text) { 42 | if (mToast == null) { 43 | mToast = Toast.makeText(mContext, text, Toast.LENGTH_SHORT); 44 | View view = View.inflate(mContext, R.layout.view_toast, null); 45 | mTextView = (TextView) view.findViewById(android.R.id.message); 46 | mToast.setView(view); 47 | 48 | } 49 | mToast.setText(text); 50 | int yOffset = 0; 51 | int xOffset = 0; 52 | View view = mToast.getView(); 53 | int width = 0; 54 | int height = 0; 55 | view.measure(0, 0); 56 | width = view.getMeasuredWidth(); 57 | height = view.getMeasuredHeight(); 58 | int imageId = R.drawable.ic_waring; 59 | //根据type设置图片 60 | switch (type) { 61 | case TYPE_WARING: 62 | imageId = R.drawable.ic_waring; 63 | break; 64 | } 65 | 66 | Drawable drawable = mContext.getResources().getDrawable(imageId); 67 | drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); 68 | mTextView.setCompoundDrawables(null, drawable, null, null); 69 | xOffset = (mContext.getResources().getDisplayMetrics().widthPixels - width) / 2; 70 | yOffset = (mContext.getResources().getDisplayMetrics().heightPixels - height) / 2; 71 | mToast.setGravity(Gravity.TOP | Gravity.LEFT, xOffset, yOffset); 72 | mToast.show(); 73 | //还原为默认type 74 | type = TYPE.TYPE_WARING; 75 | } 76 | 77 | public ToastUtil ok() { 78 | type = TYPE.TYPE_OK; 79 | return this; 80 | } 81 | 82 | public ToastUtil collect() { 83 | type = TYPE.TYPE_COLLECT; 84 | return this; 85 | } 86 | 87 | public ToastUtil cancelCollect() { 88 | type = TYPE.TYPE_CANCEL_COLLECT; 89 | return this; 90 | } 91 | 92 | public void cancelToast() { 93 | if (mToast != null) { 94 | mToast.cancel(); 95 | } 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /app/src/main/java/com/zhuguohui/videodemo/video/ViedoPlayChecker.java: -------------------------------------------------------------------------------- 1 | package com.zhuguohui.videodemo.video; 2 | 3 | import android.annotation.SuppressLint; 4 | import android.app.DialogFragment; 5 | import android.content.Context; 6 | import android.graphics.Color; 7 | import android.graphics.drawable.ColorDrawable; 8 | import android.net.ConnectivityManager; 9 | import android.net.NetworkInfo; 10 | import android.os.Bundle; 11 | import android.support.annotation.Nullable; 12 | import android.support.v4.app.FragmentActivity; 13 | import android.view.LayoutInflater; 14 | import android.view.View; 15 | import android.view.ViewGroup; 16 | import android.view.Window; 17 | 18 | import com.zhuguohui.videodemo.R; 19 | 20 | 21 | /** 22 | * Created by zhuguohui on 2016/12/23 0023. 23 | */ 24 | 25 | public class ViedoPlayChecker { 26 | //允许在非wifi环境下播放视频 27 | private static final boolean ALLOW_PALY_IN_NOT_WIFI_NET = false; 28 | private static final boolean HAVE_SET = false; 29 | 30 | public interface ActionListener { 31 | void doAction(); 32 | } 33 | 34 | public static void checkPlayNet(Context context, ActionListener allowListener, ActionListener cancleListener) { 35 | //如果在wifi网络直接return true 36 | if (isWifi(context)) { 37 | if (allowListener != null) { 38 | allowListener.doAction(); 39 | } 40 | return; 41 | } 42 | 43 | FragmentActivity fragmentActivity = null; 44 | if (!(context instanceof FragmentActivity)) { 45 | throw new IllegalArgumentException("must use FragmentActivity"); 46 | } 47 | fragmentActivity = (FragmentActivity) context; 48 | 49 | // Fragment instantiate = NetWarringFragment.instantiate(context, ""); 50 | NetWarringFragment netWarringFragment = new NetWarringFragment(allowListener, cancleListener); 51 | netWarringFragment.show(fragmentActivity.getFragmentManager(), ""); 52 | } 53 | 54 | 55 | public static class NetWarringFragment extends DialogFragment { 56 | public boolean allow = false; 57 | public ActionListener actionListener, cancleListener; 58 | 59 | public NetWarringFragment() { 60 | this(null, null); 61 | } 62 | 63 | @SuppressLint("ValidFragment") 64 | public NetWarringFragment(ActionListener allowListener, ActionListener cancleListener) { 65 | super(); 66 | this.actionListener = allowListener; 67 | this.cancleListener = cancleListener; 68 | } 69 | 70 | @Nullable 71 | @Override 72 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 73 | getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE); 74 | getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 75 | View view = inflater.inflate(R.layout.fragment_play_net_allow, container, false); 76 | view.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() { 77 | @Override 78 | public void onClick(View v) { 79 | getDialog().dismiss(); 80 | if (cancleListener != null) { 81 | cancleListener.doAction(); 82 | } 83 | } 84 | }); 85 | view.findViewById(R.id.btn_allow).setOnClickListener(new View.OnClickListener() { 86 | @Override 87 | public void onClick(View v) { 88 | getDialog().dismiss(); 89 | if (actionListener != null) { 90 | actionListener.doAction(); 91 | } 92 | } 93 | }); 94 | return view; 95 | } 96 | } 97 | 98 | /** 99 | * make true current connect service is wifi 100 | * 101 | * @param mContext 102 | * @return 103 | */ 104 | public static boolean isWifi(Context mContext) { 105 | ConnectivityManager connectivityManager = (ConnectivityManager) mContext 106 | .getSystemService(Context.CONNECTIVITY_SERVICE); 107 | NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo(); 108 | if (activeNetInfo != null 109 | && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) { 110 | return true; 111 | } 112 | return false; 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /app/src/main/java/com/zhuguohui/videodemo/view/DividerItemDecoration.java: -------------------------------------------------------------------------------- 1 | package com.zhuguohui.videodemo.view; 2 | /* 3 | * Copyright (C) 2014 The Android Open Source Project 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * limitations under the License. 7 | */ 8 | 9 | import android.content.Context; 10 | import android.content.res.TypedArray; 11 | import android.graphics.Canvas; 12 | import android.graphics.Rect; 13 | import android.graphics.drawable.Drawable; 14 | import android.support.v7.widget.LinearLayoutManager; 15 | import android.support.v7.widget.RecyclerView; 16 | import android.view.View; 17 | 18 | 19 | /** 20 | * This class is from the v7 samples of the Android SDK. It's not by me! 21 | * 22 | * See the license above for details. 23 | */ 24 | public class DividerItemDecoration extends RecyclerView.ItemDecoration { 25 | 26 | private static final int[] ATTRS = new int[]{ 27 | android.R.attr.listDivider 28 | }; 29 | 30 | public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL; 31 | 32 | public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL; 33 | 34 | private Drawable mDivider; 35 | 36 | private int mOrientation; 37 | 38 | public DividerItemDecoration(Context context, int orientation) { 39 | final TypedArray a = context.obtainStyledAttributes(ATTRS); 40 | mDivider = a.getDrawable(0); 41 | a.recycle(); 42 | setOrientation(orientation); 43 | } 44 | 45 | public void setOrientation(int orientation) { 46 | if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) { 47 | throw new IllegalArgumentException("invalid orientation"); 48 | } 49 | mOrientation = orientation; 50 | } 51 | 52 | @Override 53 | public void onDraw(Canvas c, RecyclerView parent) { 54 | // Log.v("recyclerview - itemdecoration", "onDraw()"); 55 | 56 | if (mOrientation == VERTICAL_LIST) { 57 | drawVertical(c, parent); 58 | } else { 59 | drawHorizontal(c, parent); 60 | } 61 | 62 | } 63 | 64 | 65 | public void drawVertical(Canvas c, RecyclerView parent) { 66 | final int left = parent.getPaddingLeft(); 67 | final int right = parent.getWidth() - parent.getPaddingRight(); 68 | 69 | final int childCount = parent.getChildCount(); 70 | for (int i = 0; i < childCount; i++) { 71 | final View child = parent.getChildAt(i); 72 | android.support.v7.widget.RecyclerView v = new android.support.v7.widget.RecyclerView(parent.getContext()); 73 | final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child 74 | .getLayoutParams(); 75 | final int top = child.getBottom() + params.bottomMargin; 76 | final int bottom = top + mDivider.getIntrinsicHeight(); 77 | mDivider.setBounds(left, top, right, bottom); 78 | mDivider.draw(c); 79 | } 80 | } 81 | 82 | public void drawHorizontal(Canvas c, RecyclerView parent) { 83 | final int top = parent.getPaddingTop(); 84 | final int bottom = parent.getHeight() - parent.getPaddingBottom(); 85 | 86 | final int childCount = parent.getChildCount(); 87 | for (int i = 0; i < childCount; i++) { 88 | final View child = parent.getChildAt(i); 89 | final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child 90 | .getLayoutParams(); 91 | final int left = child.getRight() + params.rightMargin; 92 | final int right = left + mDivider.getIntrinsicHeight(); 93 | mDivider.setBounds(left, top, right, bottom); 94 | mDivider.draw(c); 95 | } 96 | } 97 | 98 | @Override 99 | public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) { 100 | if (mOrientation == VERTICAL_LIST) { 101 | outRect.set(0, 0, 0, mDivider.getIntrinsicHeight()); 102 | } else { 103 | outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0); 104 | } 105 | } 106 | } -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/bg_dialog.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/bg_dialog_left_bottom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 7 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/bg_dialog_right_bottom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 7 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/ic_waring.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuguohui/VideoDemo/53e7c8864be8a13e08b2410066eab78732086fe4/app/src/main/res/drawable-xxhdpi/ic_waring.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/shape_dialog_left_bottom_normal.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/shape_dialog_left_bottom_press.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/shape_dialog_right_bottom_normal.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/shape_dialog_right_bottom_press.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/bg_item_common.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 7 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/bg_toast.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_fullscreen.xml: -------------------------------------------------------------------------------- 1 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 16 | 17 | 21 | 22 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_news_detail.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 19 | 20 | 27 | 28 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_video_detail.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 13 | 14 | 18 | 19 | 24 | 25 | 31 | 32 | 33 | 37 | 38 | 39 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /app/src/main/res/layout/fragment_play_net_allow.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 13 | 14 | 20 | 21 | 30 | 31 | 43 | 44 | 45 | 51 | 52 | 62 | 63 | 67 | 68 | 78 | 79 | 80 | -------------------------------------------------------------------------------- /app/src/main/res/layout/fragment_simple_news_list.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 11 | -------------------------------------------------------------------------------- /app/src/main/res/layout/item_news.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 16 | -------------------------------------------------------------------------------- /app/src/main/res/layout/list_item_video.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 18 | 19 | 24 | 25 | 26 | 30 | 31 | 36 | 37 | 43 | 44 | 45 | 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /app/src/main/res/layout/view_small_holder.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 13 | 14 | 15 | 16 | 21 | -------------------------------------------------------------------------------- /app/src/main/res/layout/view_toast.xml: -------------------------------------------------------------------------------- 1 | 19 | 20 | 26 | 27 | 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuguohui/VideoDemo/53e7c8864be8a13e08b2410066eab78732086fe4/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuguohui/VideoDemo/53e7c8864be8a13e08b2410066eab78732086fe4/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuguohui/VideoDemo/53e7c8864be8a13e08b2410066eab78732086fe4/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuguohui/VideoDemo/53e7c8864be8a13e08b2410066eab78732086fe4/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_video_close.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuguohui/VideoDemo/53e7c8864be8a13e08b2410066eab78732086fe4/app/src/main/res/mipmap-xxhdpi/ic_video_close.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuguohui/VideoDemo/53e7c8864be8a13e08b2410066eab78732086fe4/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/raw/video_list.json: -------------------------------------------------------------------------------- 1 | { 2 | "data": [ 3 | { 4 | "title": "西藏民俗年味浓 古老歌舞首秀新年舞台", 5 | "imgUrl": "http://www.xzcmvideo.cn//masvod/public/2017/01/28/11299.images/v11299_b1485583569795_app.jpg", 6 | "videoUrl": "http://www.xzcmvideo.cn//masvod/public/2017/01/28/20170128_159e3ae0361_r1_300k.mp4", 7 | "docUrl": "http://news.hexun.com/2017-01-27/187953027.html" 8 | }, 9 | { 10 | "title": "降央卓玛《那一天》", 11 | "imgUrl": "http://www.xzcmmat.cn/20170104/W020170104363076698303.jpg", 12 | "videoUrl": "http://www.xzcmvideo.cn//masvod/public/2017/01/04/20170104_1596735b47b_r1.mp4", 13 | "docUrl": "http://www.baidu.com" 14 | }, 15 | { 16 | "title": "访西藏曲艺家协会主席平措扎西", 17 | "imgUrl": "http://www.xzcmmat.cn/20170104/W020170104361595929442.jpg", 18 | "videoUrl": "http://www.xzcmvideo.cn//masvod/public/2017/01/04/20170104_1596735a01e_r1_1200k.mp4", 19 | "docUrl": "http://www.baidu.com" 20 | }, 21 | { 22 | "title": "大美昌都,灵动江达", 23 | "imgUrl": "http://www.xzcmmat.cn/20170103/W020170103344229493283.jpg", 24 | "videoUrl": "http://www.xzcmvideo.cn//masvod/public/2017/01/03/20170103_15961f53bae_r1_1200k.mp4", 25 | "docUrl": "http://www.baidu.com" 26 | }, 27 | { 28 | "title": "阿佳卓玛啦", 29 | "imgUrl": "http://www.xzcmmat.cn/20170103/W020170103342791779385.jpg", 30 | "videoUrl": "http://www.xzcmvideo.cn//masvod/public/2017/01/03/20170103_15961f35f4c_r1_1200k.mp4", 31 | "docUrl": "http://www.baidu.com" 32 | }, 33 | { 34 | "title": "《藏地骑遇》预告片", 35 | "imgUrl": "http://www.xzcmmat.cn/20161230/W020161230377834854389.jpg", 36 | "videoUrl": "http://www.xzcmvideo.cn//masvod/public/2016/12/30/20161230_1594d8eb40b_r1_1200k.mp4", 37 | "docUrl": "http://www.baidu.com" 38 | } 39 | ] 40 | } -------------------------------------------------------------------------------- /app/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #f44b50 4 | #f44b50 5 | #AE3A3D 6 | 7 | 8 | #f44b50 9 | #EEEEEE 10 | #FFFFFF 11 | #363636 12 | #FFFFFF 13 | #AAAAAA 14 | 15 | #EB413D 16 | #AAAAAA 17 | 18 | 19 | #262727 20 | 21 | #656565 22 | 23 | #b3b3b3 24 | 25 | #FFFFFF 26 | 27 | #EEEEEE 28 | 29 | #D5D5D5 30 | 31 | #f5f5f5 32 | 33 | #ffffff 34 | #f9f9f9 35 | 36 | #ededed 37 | 38 | #d5d5d5 39 | 40 | #ffffff 41 | 42 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | VideoDemo 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/test/java/com/zhuguohui/videodemo/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.zhuguohui.videodemo; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() throws Exception { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | apply from: "version.gradle" 3 | buildscript { 4 | repositories { 5 | jcenter() 6 | } 7 | dependencies { 8 | classpath 'com.android.tools.build:gradle:2.2.3' 9 | classpath 'me.tatarka:gradle-retrolambda:3.2.5' 10 | classpath 'com.jakewharton:butterknife-gradle-plugin:8.2.1' 11 | // NOTE: Do not place your application dependencies here; they belong 12 | // in the individual module build.gradle files 13 | } 14 | } 15 | 16 | allprojects { 17 | repositories { 18 | jcenter() 19 | } 20 | } 21 | 22 | task clean(type: Delete) { 23 | delete rootProject.buildDir 24 | } 25 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | org.gradle.jvmargs=-Xmx1536m 13 | 14 | # When configured, Gradle will run in incubating parallel mode. 15 | # This option should only be used with decoupled projects. More details, visit 16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 17 | # org.gradle.parallel=true 18 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhuguohui/VideoDemo/53e7c8864be8a13e08b2410066eab78732086fe4/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Dec 28 10:00:20 PST 2015 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':VideoList' 2 | -------------------------------------------------------------------------------- /version.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | Shared gradle properties 3 | */ 4 | ext { 5 | 6 | android = [ 7 | compileSdkVersion: 24, 8 | buildToolsVersion: '24.0.1', 9 | minSdkVersion : 15, 10 | targetSdkVersion : 22, 11 | javaVersion : JavaVersion.VERSION_1_8, 12 | // versionCode : 1, 13 | // versionName : '1.0', 14 | // appApplicationId : 'xxx' 15 | ] 16 | 17 | 18 | depsVersion = [ 19 | junitVersion : '4.12', 20 | supportVersion : '24.1.1', 21 | okhttpVersion : '3.4.1', 22 | retrofitVersion : '2.1.0', 23 | butterknifeVersion : '8.2.1', 24 | timberVersion : '4.1.2', 25 | rxJavaVersion : '1.1.8', 26 | rxAndroidVersion : '1.2.1', 27 | rxpermissionsVersion : '0.7.0@aar', 28 | rxLifecycleVersion : '0.6.1', 29 | supportUiVersion : '0.1.9', 30 | recyclerviewFlexibleDividerVersion: '1.2.9', 31 | frescoVersion : '0.11.0', 32 | leakcanaryAndroidVersion : '1.3.1', 33 | ottoVersion : '1.3.8', 34 | paginateVersion : '0.5.1', 35 | materialDialogsVersion : '0.8.5.9', 36 | kprogresshudVersion : '1.0.5', 37 | easyrecycleradaptersVersion : '1.0.10', 38 | prefsVersion : '1.1.0', 39 | smoothprogressbarVersion : '1.1.0', 40 | recyclerviewAnimatorsbarVersion : '1.3.0', 41 | materialishProgressVersion : '1.7', 42 | glideVersion : '3.7.0', 43 | multidexVersion : '1.0.1', 44 | daggerCompilerVersion : '2.0.2', 45 | daggerVersion : '2.0.2', 46 | javaxAnnotationVersion : '10.0-b28', 47 | swipeToolVersion : '1.0.1', 48 | photoViewVersion : '1.4.0', 49 | chrisbanesphotoViewVersion : '1.3.1', 50 | pagerSlidingTabstripVersion : '1.3', 51 | avosCloudSDKVersion : 'v3.+', 52 | avosCloudPushVersion : 'v3.+@aar', 53 | loggerVersion : '1.13', 54 | icePickVersion : '3.2.0', 55 | icePickProcessorVersion : '3.2.0', 56 | materialiconsVersion : '1.0.2', 57 | ] 58 | 59 | deps = [ 60 | jUnit : 'junit:junit:' + depsVersion.junitVersion, 61 | supportV4 : 'com.android.support:support-v4:' + depsVersion.supportVersion, 62 | supportAnnotations : 'com.android.support:support-annotations:' + depsVersion.supportVersion, 63 | appcompatV7 : 'com.android.support:appcompat-v7:' + depsVersion.supportVersion, 64 | design : 'com.android.support:design:' + depsVersion.supportVersion, 65 | gridlayoutV7 : 'com.android.support:gridlayout-v7:' + depsVersion.supportVersion, 66 | recyclerviewV7 : 'com.android.support:recyclerview-v7:' + depsVersion.supportVersion, 67 | cardviewV7 : 'com.android.support:cardview-v7:' + depsVersion.supportVersion, 68 | okhttp : 'com.squareup.okhttp3:okhttp:' + depsVersion.okhttpVersion, 69 | okhttpUrlconnection : 'com.squareup.okhttp3:okhttp-urlconnection:' + depsVersion.okhttpVersion, 70 | loggingInterceptor : 'com.squareup.okhttp3:logging-interceptor:' + depsVersion.okhttpVersion, 71 | rxjava : 'io.reactivex:rxjava:' + depsVersion.rxJavaVersion, 72 | rxandroid : 'io.reactivex:rxandroid:' + depsVersion.rxAndroidVersion, 73 | rxPermission : 'com.tbruyelle.rxpermissions:rxpermissions:' + depsVersion.rxpermissionsVersion, 74 | retrofit : 'com.squareup.retrofit2:retrofit:' + depsVersion.retrofitVersion, 75 | converterGson : 'com.squareup.retrofit2:converter-gson:' + depsVersion.retrofitVersion, 76 | converterJackson : 'com.squareup.retrofit2:converter-jackson:' + depsVersion.retrofitVersion, 77 | adapterRxjava : 'com.squareup.retrofit2:adapter-rxjava:' + depsVersion.retrofitVersion, 78 | butterknife : 'com.jakewharton:butterknife:' + depsVersion.butterknifeVersion, 79 | butterknifeCompiler : 'com.jakewharton:butterknife-compiler:' + depsVersion.butterknifeVersion, 80 | timber : 'com.jakewharton.timber:timber:' + depsVersion.timberVersion, 81 | easyrecycleradapters : 'com.smartydroid:easyrecycleradapters:' + depsVersion.easyrecycleradaptersVersion, 82 | prefs : 'me.alexrs:prefs:' + depsVersion.prefsVersion, 83 | smoothprogressbar : 'com.github.castorflex.smoothprogressbar:library-circular:' + depsVersion.smoothprogressbarVersion, 84 | recyclerviewAnimators : 'jp.wasabeef:recyclerview-animators:' + depsVersion.recyclerviewAnimatorsbarVersion, 85 | kprogresshud : 'com.kaopiz:kprogresshud:' + depsVersion.kprogresshudVersion, 86 | materialDialogs : 'com.afollestad.material-dialogs:core:' + depsVersion.materialDialogsVersion, 87 | paginate : 'com.github.markomilos:paginate:' + depsVersion.paginateVersion, 88 | supportUi : 'com.smartydroid:support-ui:' + depsVersion.supportUiVersion, 89 | recyclerviewFlexibleDivider: 'com.yqritc:recyclerview-flexibledivider:' + depsVersion.recyclerviewFlexibleDividerVersion, 90 | fresco : 'com.facebook.fresco:fresco:' + depsVersion.frescoVersion, 91 | leakcanaryAndroid : 'com.squareup.leakcanary:leakcanary-android:' + depsVersion.leakcanaryAndroidVersion, 92 | leakcanaryAndroidNoOp : 'com.squareup.leakcanary:leakcanary-android-no-op:' + depsVersion.leakcanaryAndroidVersion, 93 | otto : 'com.squareup:otto:' + depsVersion.ottoVersion, 94 | materialishProgress : 'com.pnikosis:materialish-progress:' + depsVersion.materialishProgressVersion, 95 | glide : 'com.github.bumptech.glide:glide:' + depsVersion.glideVersion, 96 | multiDex : 'com.android.support:multidex:' + depsVersion.multidexVersion, 97 | dagger : 'com.google.dagger:dagger:' + depsVersion.daggerVersion, 98 | daggerCompiler : 'com.google.dagger:dagger-compiler:' + depsVersion.daggerCompilerVersion, 99 | javaxAnnotation : 'org.glassfish:javax.annotation:' + depsVersion.javaxAnnotationVersion, 100 | ultimateSwipeTool : 'com.anthony.ultimateswipetool:library:' + depsVersion.swipeToolVersion, 101 | photoView : 'com.bm.photoview:library:' + depsVersion.photoViewVersion, 102 | pagerSlidingTabStrip : 'com.gxz.pagerslidingtabstrip:library:' + depsVersion.pagerSlidingTabstripVersion, 103 | avosCloudSDK : 'cn.leancloud.android:avoscloud-sdk:' + depsVersion.avosCloudSDKVersion, 104 | avosCloudPush : 'cn.leancloud.android:avoscloud-push:' + depsVersion.avosCloudPushVersion, 105 | rxLifecycle : 'com.trello:rxlifecycle:' + depsVersion.rxLifecycleVersion, 106 | logger : 'com.orhanobut:logger:' + depsVersion.loggerVersion, 107 | icePick : 'frankiesardo:icepick:' + depsVersion.icePickVersion, 108 | icePickProcessor : 'frankiesardo:icepick-processor:' + depsVersion.icePickProcessorVersion, 109 | chrisbanesPhotoView : 'com.github.chrisbanes:PhotoView:' + depsVersion.chrisbanesphotoViewVersion, 110 | materialicons : 'com.malinskiy:materialicons:' + depsVersion.materialiconsVersion 111 | ] 112 | } --------------------------------------------------------------------------------
{@link #NETWORKTYPE_WIFI} 169 | */ 170 | public static int getNetWorkType(Context context) { 171 | int mNetWorkType = NETWORKTYPE_INVALID; 172 | ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 173 | NetworkInfo networkInfo = manager.getActiveNetworkInfo(); 174 | if (networkInfo != null && networkInfo.isConnected()) { 175 | String type = networkInfo.getTypeName(); 176 | if (type.equalsIgnoreCase("WIFI")) { 177 | mNetWorkType = NETWORKTYPE_WIFI; 178 | } else if (type.equalsIgnoreCase("MOBILE")) { 179 | String proxyHost = android.net.Proxy.getDefaultHost(); 180 | mNetWorkType = TextUtils.isEmpty(proxyHost) 181 | ? (isFastMobileNetwork(context) ? NETWORKTYPE_3G : NETWORKTYPE_2G) 182 | : NETWORKTYPE_WAP; 183 | } 184 | } else { 185 | mNetWorkType = NETWORKTYPE_INVALID; 186 | } 187 | return mNetWorkType; 188 | } 189 | 190 | private static boolean isFastMobileNetwork(Context context) { 191 | TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 192 | switch (telephonyManager.getNetworkType()) { 193 | case TelephonyManager.NETWORK_TYPE_1xRTT: 194 | return false; // ~ 50-100 kbps 195 | case TelephonyManager.NETWORK_TYPE_CDMA: 196 | return false; // ~ 14-64 kbps 197 | case TelephonyManager.NETWORK_TYPE_EDGE: 198 | return false; // ~ 50-100 kbps 199 | case TelephonyManager.NETWORK_TYPE_EVDO_0: 200 | return true; // ~ 400-1000 kbps 201 | case TelephonyManager.NETWORK_TYPE_EVDO_A: 202 | return true; // ~ 600-1400 kbps 203 | case TelephonyManager.NETWORK_TYPE_GPRS: 204 | return false; // ~ 100 kbps 205 | case TelephonyManager.NETWORK_TYPE_HSDPA: 206 | return true; // ~ 2-14 Mbps 207 | case TelephonyManager.NETWORK_TYPE_HSPA: 208 | return true; // ~ 700-1700 kbps 209 | case TelephonyManager.NETWORK_TYPE_HSUPA: 210 | return true; // ~ 1-23 Mbps 211 | case TelephonyManager.NETWORK_TYPE_UMTS: 212 | return true; // ~ 400-7000 kbps 213 | case TelephonyManager.NETWORK_TYPE_EHRPD: 214 | return true; // ~ 1-2 Mbps 215 | case TelephonyManager.NETWORK_TYPE_EVDO_B: 216 | return true; // ~ 5 Mbps 217 | case TelephonyManager.NETWORK_TYPE_HSPAP: 218 | return true; // ~ 10-20 Mbps 219 | case TelephonyManager.NETWORK_TYPE_IDEN: 220 | return false; // ~25 kbps 221 | case TelephonyManager.NETWORK_TYPE_LTE: 222 | return true; // ~ 10+ Mbps 223 | case TelephonyManager.NETWORK_TYPE_UNKNOWN: 224 | return false; 225 | default: 226 | return false; 227 | } 228 | } 229 | 230 | /* 231 | * 打开设置网络界面 232 | */ 233 | public static void setNetworkMethod(final Context context) { 234 | // 提示对话框 235 | AlertDialog.Builder builder = new AlertDialog.Builder(context); 236 | builder.setCancelable(false); 237 | try { 238 | builder.setTitle("网络设置提示") 239 | .setMessage("网络连接不可用,是否进行设置?") 240 | .setPositiveButton("设置", 241 | new DialogInterface.OnClickListener() { 242 | 243 | @Override 244 | public void onClick(DialogInterface dialog, 245 | int which) { 246 | Intent intent = null; 247 | // 判断手机系统的版本 即API大于10 就是3.0或以上版本 248 | if (android.os.Build.VERSION.SDK_INT > 10) { 249 | intent = new Intent( 250 | android.provider.Settings.ACTION_WIRELESS_SETTINGS); 251 | } else { 252 | intent = new Intent(); 253 | ComponentName component = new ComponentName( 254 | "com.android.settings", 255 | "com.android.settings.WirelessSettings"); 256 | intent.setComponent(component); 257 | intent.setAction("android.intent.action.VIEW"); 258 | } 259 | context.startActivity(intent); 260 | } 261 | }) 262 | .setNegativeButton("取消", 263 | new DialogInterface.OnClickListener() { 264 | 265 | @Override 266 | public void onClick(DialogInterface dialog, 267 | int which) { 268 | dialog.dismiss(); 269 | } 270 | }).show(); 271 | } catch (Exception e) { 272 | e.printStackTrace(); 273 | } 274 | } 275 | 276 | public String getMetaData(String name, String def) { 277 | try { 278 | ApplicationInfo ai = mContext.getPackageManager() 279 | .getApplicationInfo(mContext.getPackageName(), PackageManager.GET_META_DATA); 280 | Bundle bundle = ai.metaData; 281 | return bundle.getString(name); 282 | } catch (NameNotFoundException e) { 283 | Log.e("AppUtil", "Failed to load meta-data, NameNotFound: " + e.getMessage()); 284 | return def; 285 | } catch (NullPointerException e) { 286 | Log.e("AppUtil", "Failed to load meta-data, NullPointer: " + e.getMessage()); 287 | return def; 288 | } 289 | } 290 | 291 | public static String getUrlFileName(String url) { 292 | return url.substring(url.lastIndexOf("/") + 1); 293 | } 294 | 295 | public static String getUrlPath(String url) { 296 | return url.substring(0, url.lastIndexOf("/") + 1); 297 | } 298 | 299 | 300 | } 301 | -------------------------------------------------------------------------------- /app/src/main/java/com/zhuguohui/videodemo/util/ToastUtil.java: -------------------------------------------------------------------------------- 1 | package com.zhuguohui.videodemo.util; 2 | 3 | import android.content.Context; 4 | import android.graphics.drawable.Drawable; 5 | import android.view.Gravity; 6 | import android.view.View; 7 | import android.widget.TextView; 8 | import android.widget.Toast; 9 | 10 | import com.zhuguohui.videodemo.R; 11 | 12 | 13 | /** 14 | * Created by Vincent Woo 15 | * Date: 2016/1/22 16 | * Time: 17:27 17 | */ 18 | public class ToastUtil { 19 | private Context mContext; 20 | private static ToastUtil mInstance; 21 | private Toast mToast; 22 | private TYPE type = TYPE.TYPE_WARING; 23 | private TextView mTextView; 24 | 25 | private enum TYPE { 26 | TYPE_OK, TYPE_WARING, TYPE_COLLECT, TYPE_CANCEL_COLLECT; 27 | } 28 | 29 | public static ToastUtil getInstance() { 30 | return mInstance; 31 | } 32 | 33 | public static void initialize(Context ctx) { 34 | mInstance = new ToastUtil(ctx); 35 | } 36 | 37 | private ToastUtil(Context ctx) { 38 | mContext = ctx; 39 | } 40 | 41 | public void showToast(String text) { 42 | if (mToast == null) { 43 | mToast = Toast.makeText(mContext, text, Toast.LENGTH_SHORT); 44 | View view = View.inflate(mContext, R.layout.view_toast, null); 45 | mTextView = (TextView) view.findViewById(android.R.id.message); 46 | mToast.setView(view); 47 | 48 | } 49 | mToast.setText(text); 50 | int yOffset = 0; 51 | int xOffset = 0; 52 | View view = mToast.getView(); 53 | int width = 0; 54 | int height = 0; 55 | view.measure(0, 0); 56 | width = view.getMeasuredWidth(); 57 | height = view.getMeasuredHeight(); 58 | int imageId = R.drawable.ic_waring; 59 | //根据type设置图片 60 | switch (type) { 61 | case TYPE_WARING: 62 | imageId = R.drawable.ic_waring; 63 | break; 64 | } 65 | 66 | Drawable drawable = mContext.getResources().getDrawable(imageId); 67 | drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); 68 | mTextView.setCompoundDrawables(null, drawable, null, null); 69 | xOffset = (mContext.getResources().getDisplayMetrics().widthPixels - width) / 2; 70 | yOffset = (mContext.getResources().getDisplayMetrics().heightPixels - height) / 2; 71 | mToast.setGravity(Gravity.TOP | Gravity.LEFT, xOffset, yOffset); 72 | mToast.show(); 73 | //还原为默认type 74 | type = TYPE.TYPE_WARING; 75 | } 76 | 77 | public ToastUtil ok() { 78 | type = TYPE.TYPE_OK; 79 | return this; 80 | } 81 | 82 | public ToastUtil collect() { 83 | type = TYPE.TYPE_COLLECT; 84 | return this; 85 | } 86 | 87 | public ToastUtil cancelCollect() { 88 | type = TYPE.TYPE_CANCEL_COLLECT; 89 | return this; 90 | } 91 | 92 | public void cancelToast() { 93 | if (mToast != null) { 94 | mToast.cancel(); 95 | } 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /app/src/main/java/com/zhuguohui/videodemo/video/ViedoPlayChecker.java: -------------------------------------------------------------------------------- 1 | package com.zhuguohui.videodemo.video; 2 | 3 | import android.annotation.SuppressLint; 4 | import android.app.DialogFragment; 5 | import android.content.Context; 6 | import android.graphics.Color; 7 | import android.graphics.drawable.ColorDrawable; 8 | import android.net.ConnectivityManager; 9 | import android.net.NetworkInfo; 10 | import android.os.Bundle; 11 | import android.support.annotation.Nullable; 12 | import android.support.v4.app.FragmentActivity; 13 | import android.view.LayoutInflater; 14 | import android.view.View; 15 | import android.view.ViewGroup; 16 | import android.view.Window; 17 | 18 | import com.zhuguohui.videodemo.R; 19 | 20 | 21 | /** 22 | * Created by zhuguohui on 2016/12/23 0023. 23 | */ 24 | 25 | public class ViedoPlayChecker { 26 | //允许在非wifi环境下播放视频 27 | private static final boolean ALLOW_PALY_IN_NOT_WIFI_NET = false; 28 | private static final boolean HAVE_SET = false; 29 | 30 | public interface ActionListener { 31 | void doAction(); 32 | } 33 | 34 | public static void checkPlayNet(Context context, ActionListener allowListener, ActionListener cancleListener) { 35 | //如果在wifi网络直接return true 36 | if (isWifi(context)) { 37 | if (allowListener != null) { 38 | allowListener.doAction(); 39 | } 40 | return; 41 | } 42 | 43 | FragmentActivity fragmentActivity = null; 44 | if (!(context instanceof FragmentActivity)) { 45 | throw new IllegalArgumentException("must use FragmentActivity"); 46 | } 47 | fragmentActivity = (FragmentActivity) context; 48 | 49 | // Fragment instantiate = NetWarringFragment.instantiate(context, ""); 50 | NetWarringFragment netWarringFragment = new NetWarringFragment(allowListener, cancleListener); 51 | netWarringFragment.show(fragmentActivity.getFragmentManager(), ""); 52 | } 53 | 54 | 55 | public static class NetWarringFragment extends DialogFragment { 56 | public boolean allow = false; 57 | public ActionListener actionListener, cancleListener; 58 | 59 | public NetWarringFragment() { 60 | this(null, null); 61 | } 62 | 63 | @SuppressLint("ValidFragment") 64 | public NetWarringFragment(ActionListener allowListener, ActionListener cancleListener) { 65 | super(); 66 | this.actionListener = allowListener; 67 | this.cancleListener = cancleListener; 68 | } 69 | 70 | @Nullable 71 | @Override 72 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 73 | getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE); 74 | getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 75 | View view = inflater.inflate(R.layout.fragment_play_net_allow, container, false); 76 | view.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() { 77 | @Override 78 | public void onClick(View v) { 79 | getDialog().dismiss(); 80 | if (cancleListener != null) { 81 | cancleListener.doAction(); 82 | } 83 | } 84 | }); 85 | view.findViewById(R.id.btn_allow).setOnClickListener(new View.OnClickListener() { 86 | @Override 87 | public void onClick(View v) { 88 | getDialog().dismiss(); 89 | if (actionListener != null) { 90 | actionListener.doAction(); 91 | } 92 | } 93 | }); 94 | return view; 95 | } 96 | } 97 | 98 | /** 99 | * make true current connect service is wifi 100 | * 101 | * @param mContext 102 | * @return 103 | */ 104 | public static boolean isWifi(Context mContext) { 105 | ConnectivityManager connectivityManager = (ConnectivityManager) mContext 106 | .getSystemService(Context.CONNECTIVITY_SERVICE); 107 | NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo(); 108 | if (activeNetInfo != null 109 | && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) { 110 | return true; 111 | } 112 | return false; 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /app/src/main/java/com/zhuguohui/videodemo/view/DividerItemDecoration.java: -------------------------------------------------------------------------------- 1 | package com.zhuguohui.videodemo.view; 2 | /* 3 | * Copyright (C) 2014 The Android Open Source Project 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * limitations under the License. 7 | */ 8 | 9 | import android.content.Context; 10 | import android.content.res.TypedArray; 11 | import android.graphics.Canvas; 12 | import android.graphics.Rect; 13 | import android.graphics.drawable.Drawable; 14 | import android.support.v7.widget.LinearLayoutManager; 15 | import android.support.v7.widget.RecyclerView; 16 | import android.view.View; 17 | 18 | 19 | /** 20 | * This class is from the v7 samples of the Android SDK. It's not by me! 21 | *