fix some freezes at multiple orientation changes
This commit is contained in:
parent
c17741fb3d
commit
fd01e7dca5
1 changed files with 20 additions and 9 deletions
|
@ -4,6 +4,7 @@ import android.content.res.Configuration;
|
||||||
import android.content.res.Resources;
|
import android.content.res.Resources;
|
||||||
import android.media.AudioManager;
|
import android.media.AudioManager;
|
||||||
import android.media.MediaPlayer;
|
import android.media.MediaPlayer;
|
||||||
|
import android.media.MediaPlayer.OnPreparedListener;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
|
@ -31,7 +32,7 @@ import java.util.Locale;
|
||||||
|
|
||||||
public class VideoFragment extends ViewPagerFragment
|
public class VideoFragment extends ViewPagerFragment
|
||||||
implements View.OnClickListener, SurfaceHolder.Callback, MediaPlayer.OnCompletionListener, MediaPlayer.OnVideoSizeChangedListener,
|
implements View.OnClickListener, SurfaceHolder.Callback, MediaPlayer.OnCompletionListener, MediaPlayer.OnVideoSizeChangedListener,
|
||||||
SeekBar.OnSeekBarChangeListener {
|
SeekBar.OnSeekBarChangeListener, OnPreparedListener {
|
||||||
private static final String TAG = VideoFragment.class.getSimpleName();
|
private static final String TAG = VideoFragment.class.getSimpleName();
|
||||||
private static final String PROGRESS = "progress";
|
private static final String PROGRESS = "progress";
|
||||||
private MediaPlayer mediaPlayer;
|
private MediaPlayer mediaPlayer;
|
||||||
|
@ -49,6 +50,7 @@ public class VideoFragment extends ViewPagerFragment
|
||||||
private boolean isDragged;
|
private boolean isDragged;
|
||||||
private boolean isFullscreen;
|
private boolean isFullscreen;
|
||||||
private int currTime;
|
private int currTime;
|
||||||
|
private int duration;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||||
|
@ -119,7 +121,6 @@ public class VideoFragment extends ViewPagerFragment
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setupTimeHolder() {
|
private void setupTimeHolder() {
|
||||||
final int duration = mediaPlayer.getDuration() / 1000;
|
|
||||||
seekBar.setMax(duration);
|
seekBar.setMax(duration);
|
||||||
durationView.setText(getTimeString(duration));
|
durationView.setText(getTimeString(duration));
|
||||||
timerHandler = new Handler();
|
timerHandler = new Handler();
|
||||||
|
@ -209,17 +210,18 @@ public class VideoFragment extends ViewPagerFragment
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initMediaPlayer() {
|
private void initMediaPlayer() {
|
||||||
|
if (mediaPlayer != null)
|
||||||
|
return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
mediaPlayer = new MediaPlayer();
|
mediaPlayer = new MediaPlayer();
|
||||||
mediaPlayer.setDataSource(getContext(), Uri.parse(medium.getPath()));
|
mediaPlayer.setDataSource(getContext(), Uri.parse(medium.getPath()));
|
||||||
mediaPlayer.setDisplay(surfaceHolder);
|
mediaPlayer.setDisplay(surfaceHolder);
|
||||||
mediaPlayer.setOnCompletionListener(this);
|
mediaPlayer.setOnCompletionListener(this);
|
||||||
mediaPlayer.setOnVideoSizeChangedListener(this);
|
mediaPlayer.setOnVideoSizeChangedListener(this);
|
||||||
mediaPlayer.prepare();
|
mediaPlayer.setOnPreparedListener(this);
|
||||||
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
|
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
|
||||||
addPreviewImage();
|
mediaPlayer.prepareAsync();
|
||||||
setupTimeHolder();
|
|
||||||
setProgress(currTime);
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Log.e(TAG, "init media player " + e.getMessage());
|
Log.e(TAG, "init media player " + e.getMessage());
|
||||||
}
|
}
|
||||||
|
@ -281,9 +283,7 @@ public class VideoFragment extends ViewPagerFragment
|
||||||
@Override
|
@Override
|
||||||
public void onCompletion(MediaPlayer mp) {
|
public void onCompletion(MediaPlayer mp) {
|
||||||
seekBar.setProgress(seekBar.getMax());
|
seekBar.setProgress(seekBar.getMax());
|
||||||
final int duration = mediaPlayer.getDuration() / 1000;
|
|
||||||
currTimeView.setText(getTimeString(duration));
|
currTimeView.setText(getTimeString(duration));
|
||||||
|
|
||||||
pauseVideo();
|
pauseVideo();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -293,6 +293,9 @@ public class VideoFragment extends ViewPagerFragment
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setVideoSize(int videoWidth, int videoHeight) {
|
private void setVideoSize(int videoWidth, int videoHeight) {
|
||||||
|
if (getActivity() == null)
|
||||||
|
return;
|
||||||
|
|
||||||
final float videoProportion = (float) videoWidth / (float) videoHeight;
|
final float videoProportion = (float) videoWidth / (float) videoHeight;
|
||||||
final Display display = getActivity().getWindowManager().getDefaultDisplay();
|
final Display display = getActivity().getWindowManager().getDefaultDisplay();
|
||||||
int screenWidth;
|
int screenWidth;
|
||||||
|
@ -327,7 +330,7 @@ public class VideoFragment extends ViewPagerFragment
|
||||||
final int minutes = (duration % (60 * 60)) / 60;
|
final int minutes = (duration % (60 * 60)) / 60;
|
||||||
final int seconds = ((duration % (60 * 60)) % 60);
|
final int seconds = ((duration % (60 * 60)) % 60);
|
||||||
|
|
||||||
if (mediaPlayer != null && mediaPlayer.getDuration() > 3600000) {
|
if (duration > 3600) {
|
||||||
sb.append(String.format(Locale.getDefault(), "%02d", hours)).append(":");
|
sb.append(String.format(Locale.getDefault(), "%02d", hours)).append(":");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -363,4 +366,12 @@ public class VideoFragment extends ViewPagerFragment
|
||||||
|
|
||||||
isDragged = false;
|
isDragged = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPrepared(MediaPlayer mp) {
|
||||||
|
duration = mp.getDuration() / 1000;
|
||||||
|
addPreviewImage();
|
||||||
|
setupTimeHolder();
|
||||||
|
setProgress(currTime);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue