android TextureView и MediaPlayer не отображает видео файл
Добрый день реализую функционал в приложении по обрезке видео файла, но столкнулся с проблемой, TextureView не отображает видео файл.В чем может быть проблема?
TrimActivity.class
public class TrimActivity extends AppCompatActivity implements TextureView.SurfaceTextureListener, MediaPlayer.OnVideoSizeChangedListener, MediaPlayer.OnPreparedListener, MediaPlayer.OnCompletionListener, MediaPlayer.OnBufferingUpdateListener {
private AutoFitTextureView textureView;
private MediaPlayer mediaPlayer;
private Uri uri;
private boolean isPlaying = false;
/*
............................
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_trim);
textureView = findViewById(R.id.textureView);
textureView.setSurfaceTextureListener(this);
mediaPlayer = new MediaPlayer();
Intent i = getIntent();
if (i != null) {
uri = Uri.parse(i.getStringExtra("uri"));
isPlaying = true;
}
textViewRight = findViewById(R.id.tvvRight);
textViewLeft = findViewById(R.id.tvvLeft);
rangeSeekBar = findViewById(R.id.seekbar);
/*
........................
*/
setListeners();
}
@Override
public void onBackPressed() {
super.onBackPressed();
}
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
Surface surface = new Surface(surfaceTexture);
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(String.valueOf(uri));
mediaPlayer.setSurface(surface);
mediaPlayer.prepare();
mediaPlayer.setOnBufferingUpdateListener(this);
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.setOnPreparedListener(this);
mediaPlayer.setOnVideoSizeChangedListener(this);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.start();
} catch (IllegalArgumentException | SecurityException | IllegalStateException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
return false;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
@Override
public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
}
private void setListeners() {
textureView.setOnClickListener(v -> {
if (isPlaying) {
mediaPlayer.pause();
isPlaying = false;
} else {
mediaPlayer.start();
isPlaying = true;
}
});
}
@SuppressLint("DefaultLocale")
private String getTime(int seconds) {
int hr = seconds / 3600;
int rem = seconds % 3600;
int mn = rem / 60;
int sec = rem % 60;
return String.format("%02d : %02d : %02d", hr, mn, sec);
}
@Override
protected void onPause() {
super.onPause();
if (mediaPlayer != null) {
mediaPlayer.pause();
}
}
@Override
protected void onStop() {
super.onStop();
if (mediaPlayer != null) {
mediaPlayer.stop();
}
}
@Override
public void onPrepared(MediaPlayer mp) {
action(mp);
}
@Override
public void onCompletion(MediaPlayer mp) {
action(mp);
}
@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
action(mp);
}
private void action(MediaPlayer mp) {
mp.setLooping(true);
textViewLeft.setText(R.string._00_00_00);
textViewRight.setText(getTime(mp.getDuration() / 1000));
rangeSeekBar.setRangeValues(0, mp.getDuration() / 1000);
rangeSeekBar.setSelectedMaxValue(mp.getDuration() / 1000);
rangeSeekBar.setSelectedMinValue(0);
rangeSeekBar.setEnabled(true);
rangeSeekBar.setOnRangeSeekBarChangeListener((bar, minValue, maxValue) -> {
mediaPlayer.seekTo((int) minValue * 1000);
textViewLeft.setText(getTime((int) bar.getSelectedMinValue()));
textViewRight.setText(getTime((int) bar.getSelectedMaxValue()));
});
}
}
AutoFitTextureView.class
public class AutoFitTextureView extends TextureView {
private int mRatioWidth = 0;
private int mRatioHeight = 0;
public AutoFitTextureView(Context context) {
this(context, null);
}
public AutoFitTextureView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public AutoFitTextureView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
/**
* Sets the aspect ratio for this view. The size of the view will be measured based on the ratio
* calculated from the parameters. Note that the actual sizes of parameters don't matter, that
* is, calling setAspectRatio(2, 3) and setAspectRatio(4, 6) make the same result.
*
* @param width Relative horizontal size
* @param height Relative vertical size
*/
public void setAspectRatio(int width, int height) {
if (width < 0 || height < 0) {
throw new IllegalArgumentException("Size cannot be negative.");
}
mRatioWidth = width;
mRatioHeight = height;
requestLayout();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
if (0 == mRatioWidth || 0 == mRatioHeight) {
setMeasuredDimension(width, height);
} else {
if (width < height * mRatioWidth / mRatioHeight) {
setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
} else {
setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
}
}
}
}
RoundedFrameLayout.class
public class RoundedFrameLayout extends FrameLayout {
private float mRadius;
private Path mPath = new Path();
private RectF mRect = new RectF();
public RoundedFrameLayout(Context context, AttributeSet attrs) {
super(context, attrs);
this.mRadius = attrs.getAttributeFloatValue(null, "corner_radius", 0f);
}
@Override
protected void onDraw(Canvas canvas) {
int savedState = canvas.save();
float w = getWidth();
float h = getHeight();
mPath.reset();
mRect.set(0, 0, w, h);
mPath.addRoundRect(mRect, mRadius, mRadius, CCW);
mPath.close();
boolean debug = canvas.clipPath(mPath);
super.onDraw(canvas);
canvas.restoreToCount(savedState);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
// compute the mPath
float centerX = w / 2f; // calculating half width
float centerY = h / 2f; // calculating half height
mPath.reset();
mPath.addRoundRect(new RectF(0, 0, w, h), 20, 20, Path.Direction.CW);
//mPath.addCircle(centerX, centerY, Math.min(centerX, centerY), Path.Direction.CW);
mPath.close();
}
@Override
protected void dispatchDraw(Canvas canvas) {
int save = canvas.save();
canvas.clipPath(mPath);
super.dispatchDraw(canvas);
canvas.restoreToCount(save);
}
}
xml file
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<com.kotov.diplom.video_proc.view_custom.RoundedFrameLayout
android:id="@+id/card"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerInParent="true"
android:layout_marginStart="24dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="24dp"
app:layout_constraintBottom_toTopOf="@+id/s"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/ssss">
<com.kotov.diplom.video_proc.view_custom.AutoFitTextureView
android:id="@+id/textureView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:layout_centerInParent="true"
app:layout_constraintBottom_toTopOf="@+id/s"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/ssss"/>
</com.kotov.diplom.video_proc.view_custom.RoundedFrameLayout>
<RelativeLayout
android:id="@+id/s"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="false"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<TextView
android:id="@+id/tvvLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/seekbar"
android:layout_marginStart="@dimen/_11sdp"
android:text="@string/_00_00_00"
android:textColor="#FFFFFF" />
<TextView
android:id="@+id/tvvRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/seekbar"
android:layout_alignParentEnd="true"
android:layout_marginEnd="17dp"
android:gravity="end"
android:text="@string/_00_00_00"
android:textColor="#FFFFFF" />
<org.florescu.android.rangeseekbar.RangeSeekBar
android:id="@+id/seekbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:layout_marginLeft="17dp"
android:layout_marginRight="17dp"
android:layout_marginBottom="@dimen/_10sdp"
android:outlineAmbientShadowColor="#FFFFFF"
android:outlineSpotShadowColor="#FFFFFF"
app:activeColor="#FFFFFF"
app:textAboveThumbsColor="#FFFFFF"
app:thumbShadowColor="#FFFFFF" />
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>