第一次进来获取到动态权限,拿到权限后把播放控件放到父容器播放,但是现在的情况是,第一次拿到动态权限后不能播放,需要第二次或以后进来才能播放,确定是有拿到播放url的,请问这种问题该怎么解决?想要第一次进来获取到动态权限后也能正常播放,下面是类:
public class PreviewActivity extends BaseActivity {
@BindView(R.id.fl_play)
FrameLayout flPlay;
@BindView(R.id.et_video_title)
EditText mTitle;
private static final String TAG = "PreviewActivity";
private String url;
private KProgressHUD mKProgressHUD;
private static final int PERMISSION_CODE = 0;
public static void open(Context context, String url) {
Intent intent = new Intent(context, PreviewActivity.class);
intent.putExtra("url", url);
context.startActivity(intent);
}
@Override
protected View initView(Bundle savedInstanceState) {
View view = View.inflate(this, R.layout.activity_preview, null);
frameLayout.addView(view);
ButterKnife.bind(this, view);
mTvTitle.setText(getString(R.string.preview_video));
mTvMore.setText(getString(R.string.upload));
return view;
}
@Override
protected void initData() {
boolean hadPermissions = isGrantExternalRW(this);//请求动态权限
if (hadPermissions) {
playVideo();
}
}
/**
* 获取动态权限
*/
public static boolean isGrantExternalRW(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && activity.checkSelfPermission(
Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
activity.requestPermissions(new String[]{
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
}, PERMISSION_CODE);
return false;
}
return true;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == PERMISSION_CODE) {
Toast.makeText(this, "获取权限成功1457", Toast.LENGTH_SHORT).show();
playVideo();
} else if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
ToastUtil.showMessage("获取权限失败,无法预览视频");
}
}
private void playVideo() {
//TODO:可根据type区分上传的视频类型(剪辑,美颜或拍摄的视频)
JZVideoPlayerStandardLoopVideo jzvdStd = new JZVideoPlayerStandardLoopVideo(this);
flPlay.addView(jzvdStd);
url = getIntent().getStringExtra("url");
Log.d(TAG, "initData: 1011=" + url);
jzvdStd.setUp(url, "", Jzvd.SCREEN_WINDOW_FULLSCREEN);
jzvdStd.tinyBackImageView.setVisibility(View.GONE);
jzvdStd.batteryTimeLayout.setVisibility(View.GONE); //隐藏电量和时间
jzvdStd.fullscreenButton.setVisibility(View.GONE); //全屏切换按钮
jzvdStd.startVideo();
}
}