Skip to content

Commit

Permalink
1.优化视频播放的宽高比例
Browse files Browse the repository at this point in the history
2.优化控制器,便于自定义
  • Loading branch information
jianjunxiao committed Jun 21, 2017
1 parent de21884 commit 9a9fa3a
Show file tree
Hide file tree
Showing 12 changed files with 673 additions and 442 deletions.
Expand Up @@ -7,8 +7,8 @@
import android.widget.Toast;

import com.xiao.nicevideoplayer.NiceVideoPlayer;
import com.xiao.nicevideoplayer.NiceVideoPlayerController;
import com.xiao.nicevideoplayer.NiceVideoPlayerManager;
import com.xiao.nicevideoplayer.TxVideoPlayerController;
import com.xiao.nicevieoplayer.R;

public class MainActivity extends AppCompatActivity {
Expand All @@ -26,7 +26,8 @@ private void init() {
mNiceVideoPlayer = (NiceVideoPlayer) findViewById(R.id.nice_video_player);
mNiceVideoPlayer.setPlayerType(NiceVideoPlayer.PLAYER_TYPE_IJK); // IjkPlayer or MediaPlayer
mNiceVideoPlayer.setUp("http://tanzi27niu.cdsb.mobi/wps/wp-content/uploads/2017/05/2017-05-17_17-33-30.mp4", null);
NiceVideoPlayerController controller = new NiceVideoPlayerController(this);
// mNiceVideoPlayer.setUp("http://thegolfgame-1252399946.costj.myqcloud.com/IOS/DynamicVideo/d7fe046aff234018ad7ea72a509b62491492659824.mp4", null);
TxVideoPlayerController controller = new TxVideoPlayerController(this);
controller.setTitle("办公室小野开番外了,居然在办公室开澡堂!老板还点赞?");
controller.setImage("http://tanzi27niu.cdsb.mobi/wps/wp-content/uploads/2017/05/2017-05-17_17-30-43.jpg");
mNiceVideoPlayer.setController(controller);
Expand Down
Expand Up @@ -6,7 +6,7 @@
import android.view.View;
import android.view.ViewGroup;

import com.xiao.nicevideoplayer.NiceVideoPlayerController;
import com.xiao.nicevideoplayer.TxVideoPlayerController;
import com.xiao.nicevieoplayer.R;
import com.xiao.nicevieoplayer.example.adapter.holder.VideoViewHolder;
import com.xiao.nicevieoplayer.example.bean.Video;
Expand All @@ -31,7 +31,7 @@ public VideoAdapter(Context context, List<Video> videoList) {
public VideoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_video, parent, false);
VideoViewHolder holder = new VideoViewHolder(itemView);
NiceVideoPlayerController controller = new NiceVideoPlayerController(mContext);
TxVideoPlayerController controller = new TxVideoPlayerController(mContext);
holder.setController(controller);
return holder;
}
Expand Down
Expand Up @@ -4,7 +4,7 @@
import android.view.View;

import com.xiao.nicevideoplayer.NiceVideoPlayer;
import com.xiao.nicevideoplayer.NiceVideoPlayerController;
import com.xiao.nicevideoplayer.TxVideoPlayerController;
import com.xiao.nicevieoplayer.R;
import com.xiao.nicevieoplayer.example.bean.Video;

Expand All @@ -14,21 +14,22 @@

public class VideoViewHolder extends RecyclerView.ViewHolder {

private NiceVideoPlayerController mController;
private TxVideoPlayerController mController;
private NiceVideoPlayer mVideoPlayer;

public VideoViewHolder(View itemView) {
super(itemView);
mVideoPlayer = (NiceVideoPlayer) itemView.findViewById(R.id.nice_video_player);
}

public void setController(NiceVideoPlayerController controller) {
public void setController(TxVideoPlayerController controller) {
mController = controller;
}

public void bindData(Video video) {
mController.setTitle(video.getTitle());
mController.setImage(video.getImageUrl());

mVideoPlayer.setController(mController);
mVideoPlayer.setUp(video.getVideoUrl(), null);
}
Expand Down
17 changes: 4 additions & 13 deletions app/src/main/res/layout/activity_main.xml
Expand Up @@ -7,21 +7,12 @@
android:orientation="vertical"
tools:context="com.xiao.nicevieoplayer.example.MainActivity">


<LinearLayout
android:id="@+id/ll_container"
android:layout_width="match_parent"
android:layout_height="0dp"
<com.xiao.nicevideoplayer.NiceVideoPlayer
android:layout_marginBottom="8dp"
android:layout_marginTop="16dp"
android:layout_weight="1.2"
android:orientation="vertical">

<com.xiao.nicevideoplayer.NiceVideoPlayer
android:id="@+id/nice_video_player"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
android:id="@+id/nice_video_player"
android:layout_width="match_parent"
android:layout_height="180dp"/>

<LinearLayout
android:layout_width="match_parent"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/item_video.xml
Expand Up @@ -12,5 +12,5 @@
<com.xiao.nicevideoplayer.NiceVideoPlayer
android:id="@+id/nice_video_player"
android:layout_width="match_parent"
android:layout_height="200dp"/>
android:layout_height="180dp"/>
</LinearLayout>
Expand Up @@ -4,7 +4,7 @@
* Created by XiaoJianjun on 2017/5/5.
* NiceVideoPlayer抽象接口
*/
public interface NiceVideoPlayerControl {
public interface INiceVideoPlayer {

void start();
void restart();
Expand Down
@@ -0,0 +1,104 @@
package com.xiao.nicevideoplayer;

import android.content.Context;
import android.view.TextureView;

/**
* Created by XiaoJianjun on 2017/6/21.
* 重写TextureView,
*/
public class NiceTextureView extends TextureView {

private int videoHeight;
private int videoWidth;

public NiceTextureView(Context context) {
super(context);
}

public void resetVideoSize(int videoWidth, int videoHeight) {
if (this.videoWidth != videoWidth && this.videoHeight != videoHeight) {
this.videoWidth = videoWidth;
this.videoHeight = videoHeight;
requestLayout();
}
}

@Override
public void setRotation(float rotation) {
if (rotation != getRotation()) {
super.setRotation(rotation);
requestLayout();
}
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

float viewRotation = getRotation();

// 如果判断成立,则说明显示的TextureView和本身的位置是有90度的旋转的,所以需要交换宽高参数。
if (viewRotation == 90 || viewRotation == 270) {
int tempMeasureSpec = widthMeasureSpec;
widthMeasureSpec = heightMeasureSpec;
heightMeasureSpec = tempMeasureSpec;
}

int width = getDefaultSize(videoWidth, widthMeasureSpec);
int height = getDefaultSize(videoHeight, heightMeasureSpec);
if (videoWidth > 0 && videoHeight > 0) {

int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);

if (widthSpecMode == MeasureSpec.EXACTLY && heightSpecMode == MeasureSpec.EXACTLY) {
// the size is fixed
width = widthSpecSize;
height = heightSpecSize;
// for compatibility, we adjust size based on aspect ratio
if (videoWidth * height < width * videoHeight) {
width = height * videoWidth / videoHeight;
} else if (videoWidth * height > width * videoHeight) {
height = width * videoHeight / videoWidth;
}
} else if (widthSpecMode == MeasureSpec.EXACTLY) {
// only the width is fixed, adjust the height to match aspect ratio if possible
width = widthSpecSize;
height = width * videoHeight / videoWidth;
if (heightSpecMode == MeasureSpec.AT_MOST && height > heightSpecSize) {
// couldn't match aspect ratio within the constraints
height = heightSpecSize;
width = height * videoWidth / videoHeight;
}
} else if (heightSpecMode == MeasureSpec.EXACTLY) {
// only the height is fixed, adjust the width to match aspect ratio if possible
height = heightSpecSize;
width = height * videoWidth / videoHeight;
if (widthSpecMode == MeasureSpec.AT_MOST && width > widthSpecSize) {
// couldn't match aspect ratio within the constraints
width = widthSpecSize;
height = width * videoHeight / videoWidth;
}
} else {
// neither the width nor the height are fixed, try to use actual video size
width = videoWidth;
height = videoHeight;
if (heightSpecMode == MeasureSpec.AT_MOST && height > heightSpecSize) {
// too tall, decrease both width and height
height = heightSpecSize;
width = height * videoWidth / videoHeight;
}
if (widthSpecMode == MeasureSpec.AT_MOST && width > widthSpecSize) {
// too wide, decrease both width and height
width = widthSpecSize;
height = width * videoHeight / videoWidth;
}
}
} else {
// no size yet, just adopt the given spec sizes
}
setMeasuredDimension(width, height);
}
}
@@ -1,19 +1,13 @@
package com.xiao.nicevideoplayer;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.view.ContextThemeWrapper;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.LinearInterpolator;

import java.util.Formatter;
import java.util.Locale;
Expand Down Expand Up @@ -122,4 +116,31 @@ public static String formatTime(long milliseconds) {
return mFormatter.format("%02d:%02d", minutes, seconds).toString();
}
}

/**
* 保存播放位置,以便下次播放时接着上次的位置继续播放.
*
* @param context
* @param url 视频链接url
*/
public static void savePlayPosition(Context context, String url, long position) {
context.getSharedPreferences("NICE_VIDEO_PALYER_PLAY_POSITION",
Context.MODE_PRIVATE)
.edit()
.putLong(url, position)
.apply();
}

/**
* 取出上次保存的播放位置
*
* @param context
* @param url 视频链接url
* @return 上次保存的播放位置
*/
public static long getSavedPlayPosition(Context context, String url) {
return context.getSharedPreferences("NICE_VIDEO_PALYER_PLAY_POSITION",
Context.MODE_PRIVATE)
.getLong(url, 0);
}
}

0 comments on commit 9a9fa3a

Please sign in to comment.