diff --git a/gradle.properties b/gradle.properties index 3d29428..2d0eb98 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,11 +1,11 @@ -VERSION_NAME=1.0.4 -VERSION_CODE=5 +VERSION_NAME=1.0.5 +VERSION_CODE=6 GROUP=com.yqritc ARTIFACT_ID=android-scalablevideoview -COMPILE_SDK_VERSION=23 -BUILD_TOOLS_VERSION=23.0.3 -TARGET_SDK_VERSION=23 +COMPILE_SDK_VERSION=27 +BUILD_TOOLS_VERSION=27.0.3 +TARGET_SDK_VERSION=27 MIN_SDK_VERSION=14 POM_DESCRIPTION=Android texture video view having a variety of scale types diff --git a/library/build.gradle b/library/build.gradle index 1237f91..0fd97de 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -14,7 +14,7 @@ android { dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) - compile 'com.android.support:appcompat-v7:23.4.0' + compile 'com.android.support:appcompat-v7:27.1.1' } android.libraryVariants.all { variant -> diff --git a/library/src/main/java/com/yqritc/scalablevideoview/ScalableVideoView.java b/library/src/main/java/com/yqritc/scalablevideoview/ScalableVideoView.java index 38b7112..9dbe2dc 100644 --- a/library/src/main/java/com/yqritc/scalablevideoview/ScalableVideoView.java +++ b/library/src/main/java/com/yqritc/scalablevideoview/ScalableVideoView.java @@ -21,11 +21,26 @@ /** * Created by yqritc on 2015/06/11. + * Updated by 4eRTuk on 2018/10/23. */ public class ScalableVideoView extends TextureView implements TextureView.SurfaceTextureListener, MediaPlayer.OnVideoSizeChangedListener { protected MediaPlayer mMediaPlayer; + protected MediaPlayer.OnErrorListener mErrorListener; + protected MediaPlayer.OnCompletionListener mCompletionListener; + protected MediaPlayer.OnPreparedListener mPrepareListener; + protected MediaPlayer.OnInfoListener mInfoListener; + protected int mLatestPosition; + protected int mAssetId = -1; + protected long mOffset; + protected long mLength; + protected FileDescriptor mFileDescriptor; + protected Context mContext; + protected Map mHeaders; + protected Uri mUri; + protected String mAssetName; + protected String mFilePath; protected ScalableType mScalableType = ScalableType.NONE; public ScalableVideoView(Context context) { @@ -74,6 +89,41 @@ public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) { public void onSurfaceTextureUpdated(SurfaceTexture surface) { } + @Override + protected void onAttachedToWindow() { + super.onAttachedToWindow(); + if (isDataSet()) { + initializeMediaPlayer(); + try { + if (mFilePath != null) + setDataSource(mFilePath); + else if (mAssetId >= 0) + setRawData(mAssetId); + else if (mAssetName != null) + setAssetData(mAssetName); + else if (mFileDescriptor != null) { + if (mOffset > 0 || mLength > 0) + setDataSource(mFileDescriptor, mOffset, mLength); + else + setDataSource(mFileDescriptor); + } else if (mUri != null && mContext != null) { + if (mHeaders != null) + setDataSource(mContext, mUri, mHeaders); + else + setDataSource(mContext, mUri); + } + } catch (IOException e) { + e.printStackTrace(); + } + setOnCompletionListener(mCompletionListener); + setOnErrorListener(mErrorListener); + setOnInfoListener(mInfoListener); + if (mPrepareListener != null) + prepareAsync(mPrepareListener); + mMediaPlayer.seekTo(mLatestPosition); + } + } + @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); @@ -84,6 +134,7 @@ protected void onDetachedFromWindow() { if (isPlaying()) { stop(); } + mLatestPosition = mMediaPlayer.getCurrentPosition(); release(); } @@ -116,15 +167,35 @@ private void initializeMediaPlayer() { } } + private boolean isDataSet() { + return mAssetId >= 0 || mAssetName != null || mFilePath != null || mUri != null || mFileDescriptor != null; + } + + private void clearData() { + mAssetId = -1; + mAssetName = null; + mFilePath = null; + mContext = null; + mUri = null; + mHeaders = null; + mFileDescriptor = null; + mLength = 0; + mOffset = 0; + } + public void setRawData(@RawRes int id) throws IOException { AssetFileDescriptor afd = getResources().openRawResourceFd(id); setDataSource(afd); + clearData(); + mAssetId = id; } public void setAssetData(@NonNull String assetName) throws IOException { AssetManager manager = getContext().getAssets(); AssetFileDescriptor afd = manager.openFd(assetName); setDataSource(afd); + clearData(); + mAssetName = assetName; } private void setDataSource(@NonNull AssetFileDescriptor afd) throws IOException { @@ -135,28 +206,43 @@ private void setDataSource(@NonNull AssetFileDescriptor afd) throws IOException public void setDataSource(@NonNull String path) throws IOException { initializeMediaPlayer(); mMediaPlayer.setDataSource(path); + clearData(); + mFilePath = path; } public void setDataSource(@NonNull Context context, @NonNull Uri uri, @Nullable Map headers) throws IOException { initializeMediaPlayer(); mMediaPlayer.setDataSource(context, uri, headers); + clearData(); + mContext = context; + mUri = uri; + mHeaders = headers; } public void setDataSource(@NonNull Context context, @NonNull Uri uri) throws IOException { initializeMediaPlayer(); mMediaPlayer.setDataSource(context, uri); + clearData(); + mContext = context; + mUri = uri; } public void setDataSource(@NonNull FileDescriptor fd, long offset, long length) throws IOException { initializeMediaPlayer(); mMediaPlayer.setDataSource(fd, offset, length); + clearData(); + mOffset = offset; + mLength = length; + mFileDescriptor = fd; } public void setDataSource(@NonNull FileDescriptor fd) throws IOException { initializeMediaPlayer(); mMediaPlayer.setDataSource(fd); + clearData(); + mFileDescriptor = fd; } public void setScalableType(ScalableType scalableType) { @@ -168,12 +254,14 @@ public void prepare(@Nullable MediaPlayer.OnPreparedListener listener) throws IOException, IllegalStateException { mMediaPlayer.setOnPreparedListener(listener); mMediaPlayer.prepare(); + mPrepareListener = listener; } public void prepareAsync(@Nullable MediaPlayer.OnPreparedListener listener) throws IllegalStateException { mMediaPlayer.setOnPreparedListener(listener); mMediaPlayer.prepareAsync(); + mPrepareListener = listener; } public void prepare() throws IOException, IllegalStateException { @@ -186,14 +274,17 @@ public void prepareAsync() throws IllegalStateException { public void setOnErrorListener(@Nullable MediaPlayer.OnErrorListener listener) { mMediaPlayer.setOnErrorListener(listener); + mErrorListener = listener; } public void setOnCompletionListener(@Nullable MediaPlayer.OnCompletionListener listener) { mMediaPlayer.setOnCompletionListener(listener); + mCompletionListener = listener; } public void setOnInfoListener(@Nullable MediaPlayer.OnInfoListener listener) { mMediaPlayer.setOnInfoListener(listener); + mInfoListener = listener; } public int getCurrentPosition() { diff --git a/sample/build.gradle b/sample/build.gradle index b7045e0..ec33a48 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -22,6 +22,6 @@ android { dependencies { compile project(':library') // compile 'com.yqritc:android-scalablevideoview:1.0.4' - compile 'com.android.support:appcompat-v7:23.4.0' - compile 'com.android.support:recyclerview-v7:23.4.0' + compile 'com.android.support:appcompat-v7:27.1.1' + compile 'com.android.support:recyclerview-v7:27.1.1' }