鱼C论坛

 找回密码
 立即注册
查看: 1731|回复: 0

[学习笔记] android 8.4.2

[复制链接]
发表于 2017-10-31 08:41:23 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
播放视频没什么可说的跟上次一样,先新建项目,然后。。。
AndroidManifest.xml
  1. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
复制代码

activity_main.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:orientation="vertical">

  6.     <LinearLayout
  7.         android:layout_width="match_parent"
  8.         android:layout_height="wrap_content">

  9.         <Button
  10.             android:id="@+id/play"
  11.             android:layout_width="0dp"
  12.             android:layout_height="wrap_content"
  13.             android:layout_weight="1"
  14.             android:text="play"/>
  15.         <Button
  16.             android:id="@+id/pause"
  17.             android:layout_width="0dp"
  18.             android:layout_height="wrap_content"
  19.             android:layout_weight="1"
  20.             android:text="pause"/>
  21.         <Button
  22.             android:id="@+id/replay"
  23.             android:layout_width="0dp"
  24.             android:layout_height="wrap_content"
  25.             android:layout_weight="1"
  26.             android:text="replay"/>

  27.     </LinearLayout>
  28.     <VideoView
  29.         android:id="@+id/video_view"
  30.         android:layout_width="match_parent"
  31.         android:layout_height="wrap_content" />

  32. </LinearLayout>
复制代码

mainactivity.java
  1. package com.example.xinwei.playvideotest;

  2. import android.Manifest;
  3. import android.content.pm.PackageManager;
  4. import android.os.Environment;
  5. import android.support.annotation.NonNull;
  6. import android.support.v4.app.ActivityCompat;
  7. import android.support.v4.content.ContextCompat;
  8. import android.support.v7.app.AppCompatActivity;
  9. import android.os.Bundle;
  10. import android.view.View;
  11. import android.widget.Button;
  12. import android.widget.Toast;
  13. import android.widget.VideoView;

  14. import java.io.File;

  15. public class MainActivity extends AppCompatActivity implements View.OnClickListener{

  16.     private VideoView videoView;
  17.     @Override
  18.     protected void onCreate(Bundle savedInstanceState) {
  19.         super.onCreate(savedInstanceState);
  20.         setContentView(R.layout.activity_main);
  21.         videoView = (VideoView)findViewById(R.id.video_view);
  22.         Button play = (Button)findViewById(R.id.play);
  23.         Button pause = (Button)findViewById(R.id.pause);
  24.         Button replay = (Button)findViewById(R.id.replay);
  25.         play.setOnClickListener(this);
  26.         pause.setOnClickListener(this);
  27.         replay.setOnClickListener(this);
  28.         if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){
  29.             ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
  30.         }else{
  31.             initVideoPath();
  32.         }
  33.     }

  34.     private void initVideoPath() {
  35.         File file = new File(Environment.getExternalStorageDirectory(),"movie.mp4");
  36.         videoView.setVideoPath(file.getPath());
  37.     }

  38.     @Override
  39.     public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  40.         super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  41.         switch (requestCode){
  42.             case 1:
  43.                 if (grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION_GRANTED){
  44.                     initVideoPath();
  45.                 }else {
  46.                     Toast.makeText(this, "can not use this program if denied permission", Toast.LENGTH_SHORT).show();
  47.                     finish();
  48.                 }
  49.                 break;
  50.             default:
  51.         }
  52.     }

  53.     @Override
  54.     public void onClick(View view) {
  55.         switch (view.getId()){
  56.             case R.id.play:
  57.                 if (videoView.isPlaying()){
  58.                     videoView.start();
  59.                 }
  60.                 break;
  61.             case R.id.pause:
  62.                 if (videoView.isPlaying()){
  63.                     videoView.pause();
  64.                 }
  65.                 break;
  66.             case R.id.replay:
  67.                 if (videoView.isPlaying()){
  68.                     videoView.resume();
  69.                 }
  70.                 break;
  71.         }
  72.     }

  73.     @Override
  74.     protected void onDestroy() {
  75.         super.onDestroy();
  76.         if (videoView!=null){
  77.             videoView.suspend();
  78.         }
  79.     }
  80. }
复制代码

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 16:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表