我的视频播放器活动在几个月内没有任何代码修改就停止了正常运行。
我真的不知道它什么时候停止工作但我觉得这是因为我最近添加了ContentProvider
以达到7 +兼容意图。
症状是永远不会隐藏progressDialog
。事实上,onPrepared
和onError
都没有被召唤过。我只是得到一个无尽的旋转器。
另请注意,天气它是流媒体的本地文件,完全相同的事情附加。
您认为它可能与我最近添加的contentProvider有关吗?或者我应该以另一种方式搜索?
这是一个非常短的活动:
public class VideoPlayerActivity extends M360Activity {
public static final String TAG = "VideoPlayerActivity";
public static final String VIDEOS_URL = AppConstants.SERVER_URL + "/hls/videos/mp4:";
public static final String VIDEOS_SUFFIX = ".mp4/playlist.m3u8";
private static final String ARG_VIDEO_MEDIA_ID = "F9mmPRCFhpQfNtk826ye";
private VideoView videoView;
private String mVideoId;
private Media mMedia;
private MediaController mControls;
private ProgressDialog progressDialog;
private int position = 0;
private int buttonPosition = HQ;
private Button qualityButton;
private String bitRateMobile;
public static Intent newIntent(Context c, String mediaId) {
Intent intent = new Intent(c, VideoPlayerActivity.class);
intent.putExtra(VideoPlayerActivity.ARG_VIDEO_MEDIA_ID, mediaId);
return intent;
}
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_player);
mVideoId = getIntent().getStringExtra(ARG_VIDEO_MEDIA_ID);
Log.i(TAG, "onCreate id : " + mVideoId);
mMedia = realm.where(CourseElement.class)
.equalTo("childType", CourseElementTypes.MEDIAS.name())
.equalTo("id", mVideoId)
.findFirst();
if (mControls == null) {
mControls = new MediaController(VideoPlayerActivity.this);
}
// create a progress bar while the video file is loading
progressDialog = new ProgressDialog(VideoPlayerActivity.this);
// set a name for the progress bar
// set a message for the progress bar
progressDialog.setMessage("Loading...");
//set the progress bar not cancelable on users' touch
//progressDialog.setCancelable(false);
progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override public void onCancel(DialogInterface dialog) {
finish();
}
});
// show the progress bar
progressDialog.show();
videoView = (VideoView) findViewById(R.id.video_view);
videoView.setMediaController(mControls);
if (mMedia != null) {
bitRateMobile = mMedia.getBitrateMobile();
}
qualityButton = (Button) findViewById(R.id.quality_button);
if (FileManager.getMediaFile(mMedia) != null && FileManager.getMediaFile(mMedia).exists()) {
runOnUiThread(new MainThreadLoadVideo(FileManager.getMediaFile(mMedia).getAbsolutePath()));
qualityButton.setVisibility(View.GONE);
} else {
new Thread(new BackGroundRunnable()).start();
qualityButton.setText(buttonPosition == HQ ? getString(R.string.hq) : getString(R.string.sq));
qualityButton.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
videoView.pause();
position = videoView.getCurrentPosition();
progressDialog.show();
new Thread(new BackGroundRunnable(buttonPosition)).start();
if (buttonPosition == HQ) {
qualityButton.setText(getString(R.string.sq));
buttonPosition = SQ;
} else {
qualityButton.setText(getString(R.string.hq));
buttonPosition = HQ;
}
}
});
}
}
public static int HQ = 0;
public static int SQ = 1;
public static int DEFAULT = -1;
private class BackGroundRunnable implements Runnable {
int quality = DEFAULT;
public BackGroundRunnable() {
}
public BackGroundRunnable(int quality) {
this.quality = quality;
}
@Override public void run() {
String path = null;
if (quality == DEFAULT && bitRateMobile != null) {
ConnectionQuality cq = ConnectionClassManager.getInstance().getCurrentBandwidthQuality();
switch (cq) {
case POOR:
case MODERATE:
case UNKNOWN:
path = VIDEOS_URL + mVideoId + "@480p" + VIDEOS_SUFFIX;
break;
case GOOD:
case EXCELLENT:
path = VIDEOS_URL + mVideoId + VIDEOS_SUFFIX;
break;
}
} else if (quality == SQ) {
path = VIDEOS_URL + mVideoId + "@480p" + VIDEOS_SUFFIX;
qualityButton.setText(getString(R.string.hq));
buttonPosition = HQ;
} else {
path = VIDEOS_URL + mVideoId + VIDEOS_SUFFIX;
qualityButton.setText(getString(R.string.sq));
buttonPosition = SQ;
}
runOnUiThread(new MainThreadLoadVideo(path));
}
}
private class MainThreadLoadVideo implements Runnable {
String path;
public MainThreadLoadVideo(String path) {
Log.i(TAG, path);
this.path = path;
}
@Override public void run() {
try {
Uri uri = Uri.parse(path);
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mediaPlayer) {
// close the progress bar and play the video
progressDialog.dismiss();
//if we have a position on savedInstanceState, the video playback should start from here
videoView.seekTo(position);
videoView.start();
}
});
videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override public boolean onError(MediaPlayer mp, int what, int extra) {
new AlertDialog.Builder(VideoPlayerActivity.this).setTitle(
getString(android.R.string.VideoView_error_title))
.setMessage(getString(android.R.string.VideoView_error_text_unknown))
.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override public void onDismiss(DialogInterface dialog) {
finish();
}
})
.create()
.show();
return true;
}
});
videoView.requestFocus();
videoView.setVideoURI(uri);
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
//we use onSaveInstanceState in order to store the video playback position for orientation change
savedInstanceState.putInt("Position", videoView.getCurrentPosition());
videoView.pause();
}
@Override public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
//we use onRestoreInstanceState in order to play the video playback from the stored position
position = savedInstanceState.getInt("Position");
videoView.seekTo(position);
}
和我的布局:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.m360.android.activity.VideoPlayerActivity"
>
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/grey_900"
/>
<VideoView
android:id="@+id/video_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:visibility="invisible"
/>
<Button
android:id="@+id/quality_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
tools:text="HQ"
style="@style/MainButton"
/>
</merge>