发表于: 2018-11-17 23:08:26
0 899
一、今天完成的事情
通过url播放视频
JIaoZiVideoPlayer这个框架内非常强大,播放网络视频的方式和播放本地视频的方法几乎一样
MyJzvdStd myJzvdStd= findViewById(R.id.videoplayer);
myJzvdStd.setUp(url, "标题", JzvdStd.SCREEN_WINDOW_NORMAL);
调用相机拍摄视频,拍摄成功后刷新视频列表
public class MainActivity extends BaseActivity implements VideoAdapter.OnItemClickListener,
View.OnClickListener {
private static final String TAG = "MainActivity";
private static final int NOTIFY_ADAPTER = 1;
private static final int NO_VIDEO = 2;
private static final int TAKE_VIDEO = 1;
private List<VideoBean> videoBeanList = new ArrayList<VideoBean>();
private static VideoAdapter videoAdapter;
private MyHandler myHandler = new MyHandler();
private ConstraintLayout addressCL;
private View glassView;
private EditText addressET;
private Button goBT;
private Toolbar toolbar;
private static RecyclerView videoRV;
private static ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
requestPermission();
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.toolbar, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.take_video:
takeVideo();
break;
case R.id.refresh:
initVideo();
break;
case R.id.network_resource:
if (glassView.getVisibility() == GONE) {
glassView.setVisibility(VISIBLE);
}
if (addressCL.getVisibility() == GONE) {
addressCL.setVisibility(VISIBLE);
}
break;
case R.id.setting:
Toast.makeText(MainActivity.this, "Setting", Toast.LENGTH_SHORT).show();
break;
default:
}
return true;
}
/*
初始化控件
*/
public void initView() {
addressCL = (ConstraintLayout) findViewById(R.id.cl_address);
addressCL.setVisibility(GONE);
glassView = (View) findViewById(R.id.view_glass);
glassView.setOnClickListener(this);
glassView.setVisibility(GONE);
addressET = (EditText) findViewById(R.id.et_address);
goBT = (Button) findViewById(R.id.bt_go_to_address);
goBT.setOnClickListener(this);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
videoRV = (RecyclerView) findViewById(R.id.rv_video);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
videoRV.setLayoutManager(linearLayoutManager);
videoAdapter = new VideoAdapter(MainActivity.this, videoBeanList);
videoAdapter.setOnItemClickListener(this);
videoRV.setAdapter(videoAdapter);
progressBar = (ProgressBar) findViewById(R.id.bar_progress);
progressBar.setVisibility(GONE);
}
@Override
public void onItemClick(int position) {
Intent intent = new Intent(MainActivity.this, PlayVideoActivity.class);
intent.putExtra("source", videoBeanList.get(position).getPath());
startActivity(intent);
//JzvdStd.startFullscreen(this, JzvdStd.class, videoBeanList.get(position).getPath(), videoBeanList.get(position).getTitle());
}
@Override
public void onClick(View view) {
switch(view.getId()) {
case R.id.view_glass:
if (glassView.getVisibility() == VISIBLE) {
glassView.setVisibility(GONE);
}
if (addressCL.getVisibility() == VISIBLE) {
addressCL.setVisibility(GONE);
}
closeKeyboard();
addressET.setText("");
break;
case R.id.bt_go_to_address:
if (!TextUtils.isEmpty(addressET.getText().toString())) {
Intent intent = new Intent(MainActivity.this, PlayVideoActivity.class);
intent.putExtra("source", addressET.getText().toString());
startActivity(intent);
}
break;
default:
break;
}
}
/*
请求权限
SD卡读写权限
*/
private void requestPermission() {
List<String> permissionList = new ArrayList<String>();
//权限:读写SD卡
if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
permissionList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if (!permissionList.isEmpty()) {
String[] permissions = permissionList.toArray(
new String[permissionList.size()]);
ActivityCompat.requestPermissions(MainActivity.this, permissions, 1);
} else {
initVideo();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case 1:
if (grantResults.length > 0) {
for (int result : grantResults) {
if (result != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this,
"请授予存储权限", Toast.LENGTH_SHORT).show();
finish();
return;
}
}
initVideo();
} else {
Toast.makeText(MainActivity.this, "未知错误", Toast.LENGTH_SHORT).show();
finish();
}
break;
default:
break;
}
}
/*
初始化视频数据
*/
public void initVideo() {
if (progressBar.getVisibility() == View.GONE) {
progressBar.setVisibility(VISIBLE);
}
new Thread(new Runnable() {
@Override
public void run() {
AbstractProvider abstractProvider = new VideoProvider(MainActivity.this);
List<VideoBean> list = (List<VideoBean>) abstractProvider.getList();
if (list == null || list.size() == 0) {
Message message = new Message();
message.what = NO_VIDEO;
myHandler.sendMessage(message);
} else {
videoBeanList.clear();
for (VideoBean videoBean : list) {
videoBeanList.add(videoBean);
}
Message message = new Message();
message.what = NOTIFY_ADAPTER;
myHandler.sendMessage(message);
}
}
}).start();
}
private static class MyHandler extends Handler {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch(msg.what) {
case NOTIFY_ADAPTER:
Log.i(TAG, "handleMessage: 更新RecyclerView");
videoAdapter.notifyDataSetChanged();
if (progressBar.getVisibility() == VISIBLE) {
progressBar.setVisibility(GONE);
}
break;
case NO_VIDEO:
Log.i(TAG, "handleMessage: list为空,可能没有扫描到视频");
if (progressBar.getVisibility() == VISIBLE) {
progressBar.setVisibility(GONE);
}
break;
default:
break;
}
}
}
/*
拍摄视频
*/
public void takeVideo() {
Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
if (takeVideoIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takeVideoIntent, TAKE_VIDEO);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case TAKE_VIDEO:
if (resultCode == RESULT_OK) {
initVideo();
Toast.makeText(MainActivity.this, "拍摄成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "拍摄失败", Toast.LENGTH_SHORT).show();
}
break;
default:
break;
}
}
/*
关闭软键盘
*/
private void closeKeyboard() {
View view = getWindow().getDecorView();
if (view != null) {
InputMethodManager inputMethodManager
= (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
}
TextUtil.isEmpty()
一直用TextView.isEmpty()判断字符串是否为空,但不清楚其具体的实现方式,今天特地看了一下源码,在isEmpty()方法中,先判断字符串是否为null,然后才判断字符串是否为"",相比于str.equals("")方法,可以避免字符串为null的时候造成空指针,相比于(str == null || str.equals("")),可以使代码更简洁、易读。
/**
* Returns true if the string is null or 0-length.
* @param str the string to be examined
* @return true if str is null or zero length
*/
public static boolean isEmpty(@Nullable CharSequence str) {
return str == null || str.length() == 0;
}
二、明天计划的事情
实现播放历史功能,自定义播放器,尝试在播放界面添加额外的控件
三、遇到的问题
四、收获
.调用相机拍摄视频
.isEmpty()
评论