diff --git a/core/runtime/android/runtime/src/main/java/org/hapjs/component/constants/Attributes.java b/core/runtime/android/runtime/src/main/java/org/hapjs/component/constants/Attributes.java index aec8d3ca..8b970fcd 100755 --- a/core/runtime/android/runtime/src/main/java/org/hapjs/component/constants/Attributes.java +++ b/core/runtime/android/runtime/src/main/java/org/hapjs/component/constants/Attributes.java @@ -526,6 +526,7 @@ public interface Style { String OBJECT_FIT = "objectFit"; String ALT_OBJECT_FIT = "altObjectFit"; String SRC = "src"; + String MARK = "mark"; String SOURCE = "source"; String ALT = "alt"; String QUALITY = "quality"; diff --git a/core/runtime/android/widgets/src/main/java/org/hapjs/widgets/video/DefaultPlayerManager.java b/core/runtime/android/widgets/src/main/java/org/hapjs/widgets/video/DefaultPlayerManager.java index 321d72c7..2c3c2a74 100644 --- a/core/runtime/android/widgets/src/main/java/org/hapjs/widgets/video/DefaultPlayerManager.java +++ b/core/runtime/android/widgets/src/main/java/org/hapjs/widgets/video/DefaultPlayerManager.java @@ -1,11 +1,14 @@ /* - * Copyright (c) 2021, the hapjs-platform Project Contributors + * Copyright (c) 2021-present, the hapjs-platform Project Contributors * SPDX-License-Identifier: Apache-2.0 */ package org.hapjs.widgets.video; import android.content.Context; +import android.text.TextUtils; +import android.util.Log; + import androidx.annotation.NonNull; import java.util.ArrayList; import java.util.List; @@ -24,6 +27,7 @@ public class DefaultPlayerManager implements PlayerManager { private Context mContext; private int mPlayerCountLimit = DEFAULT_PLAYER_COUNT_LIMIT; private List mInstances; + private static final String TAG = "DefaultPlayerManager"; private DefaultPlayerManager() { mInstances = new ArrayList<>(DEFAULT_PLAYER_COUNT_LIMIT); @@ -65,7 +69,7 @@ public

P obtainPlayer(PlayerProxy target) { } trimToSize(mPlayerCountLimit); - PlayerHolder holder = findAvaiableIdlePlayer(); + PlayerHolder holder = findAvaiableIdlePlayer(target); if (holder != null) { // 有可用的空闲player holder.mTarget.unbind(); @@ -98,11 +102,19 @@ private boolean hasLimit() { return mPlayerCountLimit != PLAYER_COUNT_UNLIMITED; } - private PlayerHolder findAvaiableIdlePlayer() { + private PlayerHolder findAvaiableIdlePlayer(PlayerProxy target) { int size = mInstances.size(); for (int i = 0; i < size; i++) { PlayerHolder holder = mInstances.get(i); Player player = holder.mPlayer; + if (null != player && null != target) { + String mark = player.getMark(); + String targetMark = target.getMark(); + if (!TextUtils.isEmpty(targetMark) && !targetMark.equals(mark)) { + Log.w(TAG, "findAvaiableIdlePlayer targetMark mark not same."); + continue; + } + } int targetState = player.getTargetState(); int currentState = player.getCurrentState(); if (targetState == currentState @@ -117,7 +129,7 @@ private PlayerHolder findAvaiableIdlePlayer() { } private PlayerHolder createInternal(PlayerProxy target) { - Player player = new ExoPlayer(mContext); + Player player = new ExoPlayer(mContext, (null != target ? target.getMark() : null)); PlayerHolder holder = new PlayerHolder(player, target); mInstances.add(holder); return holder; diff --git a/core/runtime/android/widgets/src/main/java/org/hapjs/widgets/video/ExoPlayer.java b/core/runtime/android/widgets/src/main/java/org/hapjs/widgets/video/ExoPlayer.java index b6016631..3fd89953 100644 --- a/core/runtime/android/widgets/src/main/java/org/hapjs/widgets/video/ExoPlayer.java +++ b/core/runtime/android/widgets/src/main/java/org/hapjs/widgets/video/ExoPlayer.java @@ -40,6 +40,8 @@ import com.google.android.exoplayer2.util.Util; import org.hapjs.component.constants.Attributes; +import java.util.HashMap; + public class ExoPlayer extends Player implements SimpleExoPlayer.VideoListener, com.google.android.exoplayer2.Player.EventListener { @@ -56,14 +58,17 @@ public class ExoPlayer extends Player private boolean isBuffering; private SuspendLoadControl mSuspendLoadControl; - public ExoPlayer(@NonNull Context context) { + public ExoPlayer(@NonNull Context context, String mark) { super(context); mManifestDataSourceFactory = - new DefaultDataSourceFactory(context, Util.getUserAgent(context, "default")); + new DefaultDataSourceFactory( + context, Util.getUserAgent(context, TextUtils.isEmpty(mark) ? "default" : mark)); mMediaDataSourceFactory = new DefaultDataSourceFactory( - context, Util.getUserAgent(context, "default"), + context, + Util.getUserAgent(context, TextUtils.isEmpty(mark) ? "default" : mark), new DefaultBandwidthMeter()); + mMark = mark; } @Override @@ -406,7 +411,9 @@ public void onPlayerError(ExoPlaybackException e) { if (e != null) { what = e.type; } - notifyError(what, -1); + HashMap datas = new HashMap<>(); + datas.put(Attributes.Style.MARK, !TextUtils.isEmpty(mMark) ? mMark : ""); + notifyError(what, -1, datas); } @Override diff --git a/core/runtime/android/widgets/src/main/java/org/hapjs/widgets/video/IMediaPlayer.java b/core/runtime/android/widgets/src/main/java/org/hapjs/widgets/video/IMediaPlayer.java index 14dd5c61..1c982593 100644 --- a/core/runtime/android/widgets/src/main/java/org/hapjs/widgets/video/IMediaPlayer.java +++ b/core/runtime/android/widgets/src/main/java/org/hapjs/widgets/video/IMediaPlayer.java @@ -10,6 +10,7 @@ import androidx.annotation.IntDef; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +import java.util.HashMap; import java.util.Map; public interface IMediaPlayer { @@ -62,6 +63,10 @@ public interface IMediaPlayer { void setMuted(boolean muted); + void setMark(String mark); + + String getMark(); + long getCurrentPosition(); long getDuration(); @@ -121,7 +126,7 @@ public interface IMediaPlayer { interface EventListener { void onLoadingChanged(IMediaPlayer player, boolean isLoading); - boolean onError(IMediaPlayer player, @MediaErrorType int what, int extra); + boolean onError(IMediaPlayer player, @MediaErrorType int what, int extra, HashMap datas); void onPlayerStateChanged(IMediaPlayer player, @PlayerState int playbackState); diff --git a/core/runtime/android/widgets/src/main/java/org/hapjs/widgets/video/Player.java b/core/runtime/android/widgets/src/main/java/org/hapjs/widgets/video/Player.java index 4c4919c9..1baad1f5 100644 --- a/core/runtime/android/widgets/src/main/java/org/hapjs/widgets/video/Player.java +++ b/core/runtime/android/widgets/src/main/java/org/hapjs/widgets/video/Player.java @@ -15,6 +15,8 @@ import android.view.TextureView; import androidx.annotation.NonNull; import androidx.annotation.Nullable; + +import java.util.HashMap; import java.util.Map; import java.util.Objects; import org.hapjs.common.executors.Executors; @@ -46,6 +48,7 @@ public abstract class Player implements IMediaPlayer { private boolean mMuted; private boolean mAutoPlay; private boolean mSuspendBuffer = false; + protected String mMark; protected Player(@NonNull Context context) { mApplicationContext = context.getApplicationContext(); @@ -246,6 +249,16 @@ public boolean isSuspendBuffer() { return mSuspendBuffer; } + public String getMark() { + return mMark; + } + + //no use now + @Override + public void setMark(String mark) { + mMark = mark; + } + @Override public void setSuspendBuffer(boolean suspendBuffer) { if (mSuspendBuffer == suspendBuffer) { @@ -409,9 +422,9 @@ private void notifyStateChanged(@PlayerState int state) { } } - protected void notifyError(int what, int extra) { + protected void notifyError(int what, int extra, HashMap datas) { if (mEventListener != null) { - mEventListener.onError(this, what, extra); + mEventListener.onError(this, what, extra, datas); } } diff --git a/core/runtime/android/widgets/src/main/java/org/hapjs/widgets/video/PlayerProxy.java b/core/runtime/android/widgets/src/main/java/org/hapjs/widgets/video/PlayerProxy.java index fed6fe3d..978db448 100644 --- a/core/runtime/android/widgets/src/main/java/org/hapjs/widgets/video/PlayerProxy.java +++ b/core/runtime/android/widgets/src/main/java/org/hapjs/widgets/video/PlayerProxy.java @@ -23,6 +23,7 @@ public class PlayerProxy

implements IMediaPlayer { private float mSpeed = 1.0f; public long mCachedPosition = -1; private boolean mSuspendBuffer = false; + private String mMark; @Override public void prepare() { @@ -154,6 +155,14 @@ public void setMuted(boolean muted) { } } + public String getMark() { + return mMark; + } + + public void setMark(String mark) { + this.mMark = mark; + } + @Override public long getCurrentPosition() { if (mPlayer != null) { diff --git a/core/runtime/android/widgets/src/main/java/org/hapjs/widgets/video/Video.java b/core/runtime/android/widgets/src/main/java/org/hapjs/widgets/video/Video.java index 128c2529..73031b17 100644 --- a/core/runtime/android/widgets/src/main/java/org/hapjs/widgets/video/Video.java +++ b/core/runtime/android/widgets/src/main/java/org/hapjs/widgets/video/Video.java @@ -52,6 +52,8 @@ import org.hapjs.runtime.HapEngine; import org.hapjs.widgets.view.video.FlexVideoView; +import static org.hapjs.component.constants.Attributes.Style.MARK; + @WidgetAnnotation( name = Video.WIDGET_NAME, methods = { @@ -216,6 +218,10 @@ protected boolean setAttribute(String key, Object attribute) { String uri = Attributes.getString(attribute); setVideoURI(uri); return true; + case MARK: + String mark = Attributes.getString(attribute); + setVideoMark(mark); + return true; case Attributes.Style.AUTO_PLAY: boolean autoPlay = Attributes.getBoolean(attribute, false); setAutoPlay(autoPlay); @@ -299,9 +305,12 @@ protected boolean addEvent(String event) { mHost.setOnErrorListener( new FlexVideoView.OnErrorListener() { @Override - public boolean onError(int what, int extra) { + public boolean onError(int what, int extra, HashMap datas) { Log.w(TAG, "Error, what:" + what + " extra:" + extra); Map params = new HashMap(); + if (null != datas) { + params.putAll(datas); + } params.put("what", what); params.put("extra", extra); mCallback @@ -476,6 +485,13 @@ public void setVideoURI(String uri) { NetworkReportManager.getInstance().reportNetwork(NetworkReportManager.KEY_VIDEO, uri.toString()); } + public void setVideoMark(String mark) { + if (mHost == null) { + return; + } + mHost.setMark(mark); + } + public void setAutoPlay(boolean autoPlay) { mAutoPlay = autoPlay; diff --git a/core/runtime/android/widgets/src/main/java/org/hapjs/widgets/view/video/FlexVideoView.java b/core/runtime/android/widgets/src/main/java/org/hapjs/widgets/view/video/FlexVideoView.java index 7e6b55bc..bd4b7120 100644 --- a/core/runtime/android/widgets/src/main/java/org/hapjs/widgets/view/video/FlexVideoView.java +++ b/core/runtime/android/widgets/src/main/java/org/hapjs/widgets/view/video/FlexVideoView.java @@ -17,6 +17,7 @@ import android.net.Uri; import android.os.Handler; import android.os.Message; +import android.text.TextUtils; import android.util.Log; import android.view.KeyEvent; import android.view.LayoutInflater; @@ -40,6 +41,7 @@ import com.facebook.yoga.YogaNode; import java.util.Arrays; +import java.util.HashMap; import java.util.HashSet; import java.util.Set; @@ -97,6 +99,7 @@ public class FlexVideoView extends FrameLayout private Uri mPosterUri; private Boolean mMuted; private String mPlayCount; + private String mMark; private float mSpeed = Video.SPEED_DEFAULT; private OnErrorListener mOnErrorListener; private OnIdleListener mOnIdleListener; @@ -176,6 +179,20 @@ private void initBorderRadiusParams() { } } + public String getMark() { + return mMark; + } + + public void setMark(String mark) { + this.mMark = mark; + if (null != mPlayer) { + String curMark = mPlayer.getMark(); + if (!TextUtils.isEmpty(mMark) && !mMark.equals(curMark)) { + releasePlayer(); + } + } + } + @Override protected void onDraw(Canvas canvas) { if (isRoundedBorders()) { @@ -534,13 +551,13 @@ public void onPlayerStateChanged(IMediaPlayer player, int playbackState) { } } - protected boolean onError(int what, int extra) { + protected boolean onError(int what, int extra, HashMap datas) { if (mOnTimeUpdateListener != null) { mTimeUpdateHandler.removeMessages(MSG_TIME_UPDATE); } if (mOnErrorListener != null) { - mOnErrorListener.onError(what, extra); + mOnErrorListener.onError(what, extra, datas); } switchKeepScreenOnFlagsByState(Player.STATE_ERROR); @@ -557,8 +574,8 @@ protected boolean onError(int what, int extra) { } @Override - public boolean onError(IMediaPlayer player, int what, int extra) { - return onError(what, extra); + public boolean onError(IMediaPlayer player, int what, int extra, HashMap datas) { + return onError(what, extra, datas); } @Override @@ -1317,6 +1334,7 @@ public void initPlayer() { mPlayer.setEventListener(this); mControlsManager.attachPlayer(mPlayer); } + mPlayer.setMark(mMark); } public void setSpeed(float speed) { @@ -1327,7 +1345,7 @@ public void setSpeed(float speed) { } public interface OnErrorListener { - boolean onError(int what, int extra); + boolean onError(int what, int extra, HashMap datas); } public interface OnIdleListener {