Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> mHeaders;
protected Uri mUri;
protected String mAssetName;
protected String mFilePath;
protected ScalableType mScalableType = ScalableType.NONE;

public ScalableVideoView(Context context) {
Expand Down Expand Up @@ -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();
Expand All @@ -84,6 +134,7 @@ protected void onDetachedFromWindow() {
if (isPlaying()) {
stop();
}
mLatestPosition = mMediaPlayer.getCurrentPosition();
release();
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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<String, String> 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) {
Expand All @@ -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 {
Expand All @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}