Skip to content

Commit

Permalink
Added support for more data sources
Browse files Browse the repository at this point in the history
  • Loading branch information
yqritc committed Aug 27, 2015
1 parent f2e0b91 commit 67c1a34
Showing 1 changed file with 41 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@
import android.graphics.Matrix;
import android.graphics.SurfaceTexture;
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RawRes;
import android.util.AttributeSet;
import android.view.Surface;
import android.view.TextureView;

import java.io.FileDescriptor;
import java.io.IOException;
import java.util.Map;

/**
* Created by yqritc on 2015/06/11.
Expand Down Expand Up @@ -104,6 +107,16 @@ private void scaleVideoSize(int videoWidth, int videoHeight) {
}
}

private void initializeMediaPlayer() {
if (mMediaPlayer == null) {
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setOnVideoSizeChangedListener(this);
setSurfaceTextureListener(this);
} else {
mMediaPlayer.reset();
}
}

public void setRawData(@RawRes int id) throws IOException {
AssetFileDescriptor afd = getResources().openRawResourceFd(id);
setDataSource(afd);
Expand All @@ -116,18 +129,37 @@ public void setAssetData(@NonNull String assetName) throws IOException {
}

private void setDataSource(@NonNull AssetFileDescriptor afd) throws IOException {
if (mMediaPlayer == null) {
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setOnVideoSizeChangedListener(this);
setSurfaceTextureListener(this);
} else {
mMediaPlayer.reset();
}

mMediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
}

public void setDataSource(@NonNull String path) throws IOException {
initializeMediaPlayer();
mMediaPlayer.setDataSource(path);
}

public void setDataSource(@NonNull Context context, @NonNull Uri uri,
@Nullable Map<String, String> headers) throws IOException {
initializeMediaPlayer();
mMediaPlayer.setDataSource(context, uri, headers);
}

public void setDataSource(@NonNull Context context, @NonNull Uri uri) throws IOException {
initializeMediaPlayer();
mMediaPlayer.setDataSource(context, uri);
}

public void setDataSource(@NonNull FileDescriptor fd, long offset, long length)
throws IOException {
initializeMediaPlayer();
mMediaPlayer.setDataSource(fd, offset, length);
}

public void setDataSource(@NonNull FileDescriptor fd) throws IOException {
initializeMediaPlayer();
mMediaPlayer.setDataSource(fd);
}

public void setScalableType(ScalableType scalableType) {
mScalableType = scalableType;
scaleVideoSize(getVideoWidth(), getVideoHeight());
Expand Down

0 comments on commit 67c1a34

Please sign in to comment.