亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android ijkplayer的使用方法解析

發布時間:2020-09-13 16:51:36 來源:腳本之家 閱讀:655 作者:huaxun66 欄目:移動開發

ijkplayer是Bilibili基于ffmpeg開發并開源的輕量級視頻播放器,支持播放本地網絡視頻,也支持流媒體播放。支持Android&iOS。

ijkplayer的編譯這里不多闡述,我也是直接獲取別人編譯完成的so庫文件,直接使用的。如果你對ijkplayer的編譯感興趣,可以百度一下,有很多文章。

使用ijkplayer

導包

ijkplayer源碼官方下載地址:https://github.com/Bilibili/ijkplayer

上面是官方提供的ijkplayer的源碼地址,但是它是沒有編譯過的。下面我給大家分享一份編譯好的ijkplayer源碼,由于比較大,分了三個包才上傳完成,需要三個包都下載后才能一起解壓: ijkplayer_jb51.rar

我們下載完成,進入android/ijkplayer目錄:

Android ijkplayer的使用方法解析

  • ijkplayer-java:ijkplayer的一些操作封裝及定義。這里面是通用的API接口,里面最主要的是IMediaPlayer,它是用來渲染顯示多媒體的。
  • ijkplayer-exo:google開源的一個新的播放器ExoPlayer,在Demo中和ijkplayer對比用的。通過安裝ijkplayer可以發現setting里面可以選擇不同player來渲染多媒體顯示,該模塊下面就是一個MediaPlayer。
  • ijkplayer-example:測試程序
  • ijkplayer-{arch}:編譯出來的各個版本的.so文件。

官方提供的Demo的代碼還是挺多的,甚至還用了otto,需要對官方的demo進行精簡,去除一些用不到的代碼。

首先需要的是ijkplayer-{arch}、ijkplayer-java兩個庫。exo是Google提供的新的播放器,這里不需要,直接砍掉。其次是ijkplayer-example里的,我們需要的只有tv.danmaku.ijk.media.example.widget.media包下的部分類。

注:

鏈接庫ijkplayer-arm64,ijkplayer-armv5,ijkplayer-armv7a,ijkplayer-x86,ijkplayer-x86_64是不同體系架構的動態鏈接庫,在當前工程結構里面作為一個模塊,如果不想做兼容多平臺問題,可以刪除其他目錄結構,單獨保留自己需要的平臺目錄。

新建一個工程:

(1)把ijkplayer-armv7a/src/main/libs下的文件拷貝到新工程app目錄的libs下。
(2)把ijkplayer-java/build/outputs/aar/ijkplayer-java-release.aar復制到新工程app目錄的libs下。
(3)修改APP下的build.gradle, 主要設置.so及.aar的位置:

apply plugin: 'com.android.application'

android {
  compileSdkVersion 24
  buildToolsVersion "25.0.0"
  defaultConfig {
    applicationId "com.hx.ijkplayer_demo"
    minSdkVersion 14
    targetSdkVersion 24
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
  }
  buildTypes {
    release {
      minifyEnabled false
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
  }
  sourceSets {
    main {
      jniLibs.srcDirs = ['libs'] /**在libs文件夾下找so文件*/
    }
  }
}

repositories {
  mavenCentral()
  flatDir {
    dirs 'libs' /**在libs文件夾下找aar文件*/
  }
}

dependencies {
  compile fileTree(dir: 'libs', include: ['*.jar'])
  androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
  })
  compile 'com.android.support:appcompat-v7:24.2.1'
  testCompile 'junit:junit:4.12'
  compile(name: 'ijkplayer-java-release', ext: 'aar') /**編譯ijkplayer-java-release.aar文件*/
}
 

(4)復制ijkplayer-example下面的tv.danmaku.ijk.media.example.widget.media到新的工程,刪掉一些不需要類。

Android ijkplayer的使用方法解析

(5)IjkVideoView里面還是有很多如exo等沒用的東西,刪!具體可以參見我后面的Demo。

(6)Manifest

...
<activity android:name=".MainActivity"
     android:screenOrientation="sensorLandscape"
     android:configChanges="orientation|keyboardHidden">
...
</activity>
...
<uses-permission android:name="android.permission.INTERNET"/>

播放實現

Xml代碼

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".MainActivity">

  <com.hx.ijkplayer_demo.widget.media.IjkVideoView
    android:id="@+id/video_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
</RelativeLayout>

Java代碼

public class MainActivity extends AppCompatActivity {

  private IjkVideoView videoView;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    videoView = (IjkVideoView) findViewById(R.id.video_view);
    videoView.setAspectRatio(IRenderView.AR_ASPECT_FIT_PARENT);
    videoView.setVideoURI(Uri.parse("http://zv.3gv.ifeng.com/live/zhongwen800k.m3u8"));
    videoView.start();
  }
}

常用函數

/**
 * 參數aspectRatio(縮放參數)參見IRenderView的常量:IRenderView.AR_ASPECT_FIT_PARENT,
IRenderView.AR_ASPECT_FILL_PARENT,
IRenderView.AR_ASPECT_WRAP_CONTENT,
IRenderView.AR_MATCH_PARENT,
IRenderView.AR_16_9_FIT_PARENT,
IRenderView.AR_4_3_FIT_PARENT
 */
public void setAspectRatio(int aspectRatio);
//改變視頻縮放狀態。
public int toggleAspectRatio();
//設置視頻路徑。
public void setVideoPath(String path);
//設置視頻URI。(可以是網絡視頻地址)
public void setVideoURI(Uri uri);
//停止視頻播放,并釋放資源。
public void stopPlayback();
/**
 * 設置媒體控制器。
 * 參數controller:媒體控制器,注意是com.hx.ijkplayer_demo.widget.media.IMediaController。
 */
public void setMediaController(IMediaController controller);
//改變媒體控制器顯隱
private void toggleMediaControlsVisiblity();
//注冊一個回調函數,在視頻預處理完成后調用。在視頻預處理完成后被調用。此時視頻的寬度、高度、寬高比信息已經獲取到,此時可調用seekTo讓視頻從指定位置開始播放。
public void setOnPreparedListener(OnPreparedListener l);
//播放完成回調
public void setOnCompletionListener(IMediaPlayer.OnCompletionListener l);
//播放錯誤回調
public void setOnErrorListener(IMediaPlayer.OnErrorListener l);
//事件發生回調
public void setOnInfoListener(IMediaPlayer.OnInfoListener l);
//獲取總長度
public int getDuration();
//獲取當前播放位置。
public long getCurrentPosition();
//設置播放位置。單位毫秒
public void seekTo(long msec);
//是否正在播放。
public boolean isPlaying();
//獲取緩沖百分比。
public int getBufferPercentage();

封裝ijkplayer

上面只是ijkplayer的一個基本用法。我們可以對ijkplayer進行一次封裝,讓ijkplayer使用起來更加簡單。

功能:

  • 使用Vitamio的VideoView進行視頻播放
  • 視頻左側界面(左1/2以內)上下滑動調節亮度
  • 視頻右側界面(右1/2以外)上下滑動調節聲音
  • 雙擊切換視頻窗口布局
  • 非直播狀態,可以左右滑動調節當前播放進度
public class PlayerManager {
  /**
   * 可能會剪裁,保持原視頻的大小,顯示在中心,當原視頻的大小超過view的大小超過部分裁剪處理
   */
  public static final String SCALETYPE_FITPARENT="fitParent";
  /**
   * 可能會剪裁,等比例放大視頻,直到填滿View為止,超過View的部分作裁剪處理
   */
  public static final String SCALETYPE_FILLPARENT="fillParent";
  /**
   * 將視頻的內容完整居中顯示,如果視頻大于view,則按比例縮視頻直到完全顯示在view中
   */
  public static final String SCALETYPE_WRAPCONTENT="wrapContent";
  /**
   * 不剪裁,非等比例拉伸畫面填滿整個View
   */
  public static final String SCALETYPE_FITXY="fitXY";
  /**
   * 不剪裁,非等比例拉伸畫面到16:9,并完全顯示在View中
   */
  public static final String SCALETYPE_16_9="16:9";
  /**
   * 不剪裁,非等比例拉伸畫面到4:3,并完全顯示在View中
   */
  public static final String SCALETYPE_4_3="4:3";

  /**
   * 狀態常量
   */
  private final int STATUS_ERROR=-1;
  private final int STATUS_IDLE=0;
  private final int STATUS_LOADING=1;
  private final int STATUS_PLAYING=2;
  private final int STATUS_PAUSE=3;
  private final int STATUS_COMPLETED=4;

  private final Activity activity;
  private final IjkVideoView videoView;
  private final AudioManager audioManager;
  public GestureDetector gestureDetector;

  private boolean playerSupport;
  private boolean isLive = false;//是否為直播
  private boolean fullScreenOnly;
  private boolean portrait;

  private final int mMaxVolume;
  private int screenWidthPixels;
  private int currentPosition;
  private int status=STATUS_IDLE;
  private long pauseTime;
  private String url;

  private float brightness=-1;
  private int volume=-1;
  private long newPosition = -1;
  private long defaultRetryTime=5000;

  private OrientationEventListener orientationEventListener;
  private PlayerStateListener playerStateListener;

  public void setPlayerStateListener(PlayerStateListener playerStateListener) {
    this.playerStateListener = playerStateListener;
  }

  private OnErrorListener onErrorListener=new OnErrorListener() {
    @Override
    public void onError(int what, int extra) {
    }
  };

  private OnCompleteListener onCompleteListener=new OnCompleteListener() {
    @Override
    public void onComplete() {
    }
  };

  private OnInfoListener onInfoListener=new OnInfoListener(){
    @Override
    public void onInfo(int what, int extra) {

    }
  };
  private OnControlPanelVisibilityChangeListener onControlPanelVisibilityChangeListener=new OnControlPanelVisibilityChangeListener() {
    @Override
    public void change(boolean isShowing) {
    }
  };

  /**
   * try to play when error(only for live video)
   * @param defaultRetryTime millisecond,0 will stop retry,default is 5000 millisecond
   */
  public void setDefaultRetryTime(long defaultRetryTime) {
    this.defaultRetryTime = defaultRetryTime;
  }

  public PlayerManager(final Activity activity) {
    try {
      IjkMediaPlayer.loadLibrariesOnce(null);
      IjkMediaPlayer.native_profileBegin("libijkplayer.so");
      playerSupport=true;
    } catch (Throwable e) {
      Log.e("GiraffePlayer", "loadLibraries error", e);
    }
    this.activity=activity;
    screenWidthPixels = activity.getResources().getDisplayMetrics().widthPixels;

    videoView = (IjkVideoView) activity.findViewById(R.id.video_view);
    videoView.setOnCompletionListener(new IMediaPlayer.OnCompletionListener() {
      @Override
      public void onCompletion(IMediaPlayer mp) {
        statusChange(STATUS_COMPLETED);
        onCompleteListener.onComplete();
      }
    });
    videoView.setOnErrorListener(new IMediaPlayer.OnErrorListener() {
      @Override
      public boolean onError(IMediaPlayer mp, int what, int extra) {
        statusChange(STATUS_ERROR);
        onErrorListener.onError(what,extra);
        return true;
      }
    });
    videoView.setOnInfoListener(new IMediaPlayer.OnInfoListener() {
      @Override
      public boolean onInfo(IMediaPlayer mp, int what, int extra) {
        switch (what) {
          case IMediaPlayer.MEDIA_INFO_BUFFERING_START:
            statusChange(STATUS_LOADING);
            break;
          case IMediaPlayer.MEDIA_INFO_BUFFERING_END:
            statusChange(STATUS_PLAYING);
            break;
          case IMediaPlayer.MEDIA_INFO_NETWORK_BANDWIDTH:
            //顯示下載速度
//           Toast.show("download rate:" + extra);
            break;
          case IMediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START:
            statusChange(STATUS_PLAYING);
            break;
        }
        onInfoListener.onInfo(what,extra);
        return false;
      }
    });

    audioManager = (AudioManager) activity.getSystemService(Context.AUDIO_SERVICE);
    mMaxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    gestureDetector = new GestureDetector(activity, new PlayerGestureListener());

    if (fullScreenOnly) {
      activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }
    portrait=getScreenOrientation()== ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;

    if (!playerSupport) {
      DebugLog.e("播放器不支持此設備");
    }
  }

  private void statusChange(int newStatus) {
    status = newStatus;
    if (!isLive && newStatus==STATUS_COMPLETED) {
      DebugLog.d("statusChange STATUS_COMPLETED...");
      if (playerStateListener != null){
        playerStateListener.onComplete();
      }
    }else if (newStatus == STATUS_ERROR) {
      DebugLog.d("statusChange STATUS_ERROR...");
      if (playerStateListener != null){
        playerStateListener.onError();
      }
    } else if(newStatus==STATUS_LOADING){
//      $.id(R.id.app_video_loading).visible();
      if (playerStateListener != null){
        playerStateListener.onLoading();
      }
      DebugLog.d("statusChange STATUS_LOADING...");
    } else if (newStatus == STATUS_PLAYING) {
      DebugLog.d("statusChange STATUS_PLAYING...");
      if (playerStateListener != null){
        playerStateListener.onPlay();
      }
    }
  }

  public void onPause() {
    pauseTime= System.currentTimeMillis();
    if (status==STATUS_PLAYING) {
      videoView.pause();
      if (!isLive) {
        currentPosition = videoView.getCurrentPosition();
      }
    }
  }

  public void onResume() {
    pauseTime=0;
    if (status==STATUS_PLAYING) {
      if (isLive) {
        videoView.seekTo(0);
      } else {
        if (currentPosition>0) {
          videoView.seekTo(currentPosition);
        }
      }
      videoView.start();
    }
  }

  public void onDestroy() {
    orientationEventListener.disable();
    videoView.stopPlayback();
  }

  public void play(String url) {
    this.url = url;
    if (playerSupport) {
      videoView.setVideoPath(url);
      videoView.start();
    }
  }

  private String generateTime(long time) {
    int totalSeconds = (int) (time / 1000);
    int seconds = totalSeconds % 60;
    int minutes = (totalSeconds / 60) % 60;
    int hours = totalSeconds / 3600;
    return hours > 0 ? String.format("%02d:%02d:%02d", hours, minutes, seconds) : String.format("%02d:%02d", minutes, seconds);
  }

  private int getScreenOrientation() {
    int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
    DisplayMetrics dm = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
    int width = dm.widthPixels;
    int height = dm.heightPixels;
    int orientation;
    // if the device's natural orientation is portrait:
    if ((rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) && height > width ||
        (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) && width > height) {
      switch (rotation) {
        case Surface.ROTATION_0:
          orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
          break;
        case Surface.ROTATION_90:
          orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
          break;
        case Surface.ROTATION_180:
          orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
          break;
        case Surface.ROTATION_270:
          orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
          break;
        default:
          orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
          break;
      }
    }
    // if the device's natural orientation is landscape or if the device
    // is square:
    else {
      switch (rotation) {
        case Surface.ROTATION_0:
          orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
          break;
        case Surface.ROTATION_90:
          orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
          break;
        case Surface.ROTATION_180:
          orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
          break;
        case Surface.ROTATION_270:
          orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
          break;
        default:
          orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
          break;
      }
    }
    return orientation;
  }

  /**
   * 滑動改變聲音大小
   *
   * @param percent
   */
  private void onVolumeSlide(float percent) {
    if (volume == -1) {
      volume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
      if (volume < 0)
        volume = 0;
    }
    int index = (int) (percent * mMaxVolume) + volume;
    if (index > mMaxVolume) {
      index = mMaxVolume;
    } else if (index < 0){
      index = 0;
    }
    // 變更聲音
    audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, index, 0);
    // 變更進度條
    int i = (int) (index * 1.0 / mMaxVolume * 100);
    String s = i + "%";
    if (i == 0) {
      s = "off";
    }
    DebugLog.d("onVolumeSlide:"+s);
  }

  private void onProgressSlide(float percent) {
    long position = videoView.getCurrentPosition();
    long duration = videoView.getDuration();
    long deltaMax = Math.min(100 * 1000, duration - position);
    long delta = (long) (deltaMax * percent);

    newPosition = delta + position;
    if (newPosition > duration) {
      newPosition = duration;
    } else if (newPosition <= 0) {
      newPosition=0;
      delta=-position;
    }
    int showDelta = (int) delta / 1000;
    if (showDelta != 0) {
      String text = showDelta > 0 ? ("+" + showDelta) : "" + showDelta;
      DebugLog.d("onProgressSlide:" + text);
    }
  }

  /**
   * 滑動改變亮度
   *
   * @param percent
   */
  private void onBrightnessSlide(float percent) {
    if (brightness < 0) {
      brightness = activity.getWindow().getAttributes().screenBrightness;
      if (brightness <= 0.00f){
        brightness = 0.50f;
      }else if (brightness < 0.01f){
        brightness = 0.01f;
      }
    }
    DebugLog.d("brightness:"+brightness+",percent:"+ percent);
    WindowManager.LayoutParams lpa = activity.getWindow().getAttributes();
    lpa.screenBrightness = brightness + percent;
    if (lpa.screenBrightness > 1.0f){
      lpa.screenBrightness = 1.0f;
    }else if (lpa.screenBrightness < 0.01f){
      lpa.screenBrightness = 0.01f;
    }
    activity.getWindow().setAttributes(lpa);
  }

  public void setFullScreenOnly(boolean fullScreenOnly) {
    this.fullScreenOnly = fullScreenOnly;
    tryFullScreen(fullScreenOnly);
    if (fullScreenOnly) {
      activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    } else {
      activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
    }
  }

  private void tryFullScreen(boolean fullScreen) {
    if (activity instanceof AppCompatActivity) {
      ActionBar supportActionBar = ((AppCompatActivity) activity).getSupportActionBar();
      if (supportActionBar != null) {
        if (fullScreen) {
          supportActionBar.hide();
        } else {
          supportActionBar.show();
        }
      }
    }
    setFullScreen(fullScreen);
  }

  private void setFullScreen(boolean fullScreen) {
    if (activity != null) {
      WindowManager.LayoutParams attrs = activity.getWindow().getAttributes();
      if (fullScreen) {
        attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
        activity.getWindow().setAttributes(attrs);
        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
      } else {
        attrs.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
        activity.getWindow().setAttributes(attrs);
        activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
      }
    }
  }

  /**
   * <pre>
   *   fitParent:可能會剪裁,保持原視頻的大小,顯示在中心,當原視頻的大小超過view的大小超過部分裁剪處理
   *   fillParent:可能會剪裁,等比例放大視頻,直到填滿View為止,超過View的部分作裁剪處理
   *   wrapContent:將視頻的內容完整居中顯示,如果視頻大于view,則按比例縮視頻直到完全顯示在view中
   *   fitXY:不剪裁,非等比例拉伸畫面填滿整個View
   *   16:9:不剪裁,非等比例拉伸畫面到16:9,并完全顯示在View中
   *   4:3:不剪裁,非等比例拉伸畫面到4:3,并完全顯示在View中
   * </pre>
   * @param scaleType
   */
  public void setScaleType(String scaleType) {
    if (SCALETYPE_FITPARENT.equals(scaleType)) {
      videoView.setAspectRatio(IRenderView.AR_ASPECT_FIT_PARENT);
    }else if (SCALETYPE_FILLPARENT.equals(scaleType)) {
      videoView.setAspectRatio(IRenderView.AR_ASPECT_FILL_PARENT);
    }else if (SCALETYPE_WRAPCONTENT.equals(scaleType)) {
      videoView.setAspectRatio(IRenderView.AR_ASPECT_WRAP_CONTENT);
    }else if (SCALETYPE_FITXY.equals(scaleType)) {
      videoView.setAspectRatio(IRenderView.AR_MATCH_PARENT);
    }else if (SCALETYPE_16_9.equals(scaleType)) {
      videoView.setAspectRatio(IRenderView.AR_16_9_FIT_PARENT);
    }else if (SCALETYPE_4_3.equals(scaleType)) {
      videoView.setAspectRatio(IRenderView.AR_4_3_FIT_PARENT);
    }
  }

  public void start() {
    videoView.start();
  }

  public void pause() {
    videoView.pause();
  }

  public boolean onBackPressed() {
    if (!fullScreenOnly && getScreenOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
      activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
      return true;
    }
    return false;
  }

  class Query {
    private final Activity activity;
    private View view;

    public Query(Activity activity) {
      this.activity=activity;
    }

    public Query id(int id) {
      view = activity.findViewById(id);
      return this;
    }

    public Query image(int resId) {
      if (view instanceof ImageView) {
        ((ImageView) view).setImageResource(resId);
      }
      return this;
    }

    public Query visible() {
      if (view != null) {
        view.setVisibility(View.VISIBLE);
      }
      return this;
    }

    public Query gone() {
      if (view != null) {
        view.setVisibility(View.GONE);
      }
      return this;
    }

    public Query invisible() {
      if (view != null) {
        view.setVisibility(View.INVISIBLE);
      }
      return this;
    }

    public Query clicked(View.OnClickListener handler) {
      if (view != null) {
        view.setOnClickListener(handler);
      }
      return this;
    }

    public Query text(CharSequence text) {
      if (view!=null && view instanceof TextView) {
        ((TextView) view).setText(text);
      }
      return this;
    }

    public Query visibility(int visible) {
      if (view != null) {
        view.setVisibility(visible);
      }
      return this;
    }

    private void size(boolean width, int n, boolean dip){
      if(view != null){
        ViewGroup.LayoutParams lp = view.getLayoutParams();
        if(n > 0 && dip){
          n = dip2pixel(activity, n);
        }
        if(width){
          lp.width = n;
        }else{
          lp.height = n;
        }
        view.setLayoutParams(lp);
      }
    }

    public void height(int height, boolean dip) {
      size(false,height,dip);
    }

    public int dip2pixel(Context context, float n){
      int value = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, n, context.getResources().getDisplayMetrics());
      return value;
    }

    public float pixel2dip(Context context, float n){
      Resources resources = context.getResources();
      DisplayMetrics metrics = resources.getDisplayMetrics();
      float dp = n / (metrics.densityDpi / 160f);
      return dp;
    }
  }

  public class PlayerGestureListener extends GestureDetector.SimpleOnGestureListener {
    private boolean firstTouch;
    private boolean volumeControl;
    private boolean toSeek;

    /**
     * 雙擊
     */
    @Override
    public boolean onDoubleTap(MotionEvent e) {
      videoView.toggleAspectRatio();
      return true;
    }

    @Override
    public boolean onDown(MotionEvent e) {
      firstTouch = true;
      return super.onDown(e);
    }

    /**
     * 滑動
     */
    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
      float mOldX = e1.getX(), mOldY = e1.getY();
      float deltaY = mOldY - e2.getY();
      float deltaX = mOldX - e2.getX();
      if (firstTouch) {
        toSeek = Math.abs(distanceX) >= Math.abs(distanceY);
        volumeControl=mOldX > screenWidthPixels * 0.5f;
        firstTouch = false;
      }

      if (toSeek) {
        if (!isLive) {
          onProgressSlide(-deltaX / videoView.getWidth());
        }
      } else {
        float percent = deltaY / videoView.getHeight();
        if (volumeControl) {
          onVolumeSlide(percent);
        } else {
          onBrightnessSlide(percent);
        }
      }

      return super.onScroll(e1, e2, distanceX, distanceY);
    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
      return true;
    }
  }

  /**
   * is player support this device
   * @return
   */
  public boolean isPlayerSupport() {
    return playerSupport;
  }

  /**
   * 是否正在播放
   * @return
   */
  public boolean isPlaying() {
    return videoView!=null?videoView.isPlaying():false;
  }

  public void stop(){
    videoView.stopPlayback();
  }

  public int getCurrentPosition(){
    return videoView.getCurrentPosition();
  }

  /**
   * get video duration
   * @return
   */
  public int getDuration(){
    return videoView.getDuration();
  }

  public PlayerManager playInFullScreen(boolean fullScreen){
    if (fullScreen) {
      activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }
    return this;
  }

  public PlayerManager onError(OnErrorListener onErrorListener) {
    this.onErrorListener = onErrorListener;
    return this;
  }

  public PlayerManager onComplete(OnCompleteListener onCompleteListener) {
    this.onCompleteListener = onCompleteListener;
    return this;
  }

  public PlayerManager onInfo(OnInfoListener onInfoListener) {
    this.onInfoListener = onInfoListener;
    return this;
  }

  public PlayerManager onControlPanelVisibilityChange(OnControlPanelVisibilityChangeListener listener){
    this.onControlPanelVisibilityChangeListener = listener;
    return this;
  }

  /**
   * set is live (can't seek forward)
   * @param isLive
   * @return
   */
  public PlayerManager live(boolean isLive) {
    this.isLive = isLive;
    return this;
  }

  public PlayerManager toggleAspectRatio(){
    if (videoView != null) {
      videoView.toggleAspectRatio();
    }
    return this;
  }

  public interface PlayerStateListener{
    void onComplete();
    void onError();
    void onLoading();
    void onPlay();
  }

  public interface OnErrorListener{
    void onError(int what, int extra);
  }

  public interface OnCompleteListener{
    void onComplete();
  }

  public interface OnControlPanelVisibilityChangeListener{
    void change(boolean isShowing);
  }

  public interface OnInfoListener{
    void onInfo(int what, int extra);
  }
}

使用封裝后的PlayerManager播放視頻:

public class MainActivity extends AppCompatActivity implements PlayerManager.PlayerStateListener{
  private String url1 = "rtmp://203.207.99.19:1935/live/CCTV5";
  private String url2 = "http://zv.3gv.ifeng.com/live/zhongwen800k.m3u8";
  private String url3 = "rtsp://184.72.239.149/vod/mp4:BigBuckBunny_115k.mov";
  private String url4 = "http://42.96.249.166/live/24035.m3u8";
  private PlayerManager player;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initPlayer();
  }

  private void initPlayer() {
    player = new PlayerManager(this);
    player.setFullScreenOnly(true);
    player.setScaleType(PlayerManager.SCALETYPE_FILLPARENT);
    player.playInFullScreen(true);
    player.setPlayerStateListener(this);
    player.play(url1);
  }

  @Override
  public boolean onTouchEvent(MotionEvent event) {
    if (player.gestureDetector.onTouchEvent(event))
      return true;
    return super.onTouchEvent(event);
  }

  @Override
  public void onComplete() {
  }

  @Override
  public void onError() {
  }

  @Override
  public void onLoading() {
  }

  @Override
  public void onPlay() {
  }
}

Demo下載地址

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

余干县| 塔河县| 郯城县| 满城县| 介休市| 襄汾县| 盖州市| 扎囊县| 旬邑县| 仙游县| 南皮县| 万州区| 怀远县| 营山县| 互助| 夏河县| 平湖市| 察雅县| 南雄市| 鲜城| 蛟河市| 灌南县| 宣城市| 宁安市| 调兵山市| 全南县| 营口市| 临汾市| 柏乡县| 子洲县| 云龙县| 罗甸县| 霍邱县| 蒲城县| 睢宁县| 夏河县| 邵东县| 页游| 金寨县| 福贡县| 板桥市|