|
@@ -1,119 +1,135 @@
|
1
|
1
|
package com.cfmlg.mlg;
|
2
|
2
|
|
3
|
|
-import androidx.appcompat.app.AppCompatActivity;
|
4
|
|
-
|
5
|
|
-import android.app.Activity;
|
|
3
|
+import android.Manifest;
|
|
4
|
+import android.content.Intent;
|
|
5
|
+import android.content.pm.PackageManager;
|
6
|
6
|
import android.media.AudioManager;
|
7
|
7
|
import android.media.MediaPlayer;
|
|
8
|
+import android.net.Uri;
|
8
|
9
|
import android.os.Bundle;
|
9
|
10
|
import android.os.Environment;
|
10
|
11
|
import android.util.Log;
|
11
|
|
-import android.view.SurfaceHolder;
|
12
|
12
|
import android.view.SurfaceView;
|
13
|
13
|
import android.view.View;
|
14
|
|
-import android.widget.Button;
|
15
|
|
-import android.widget.MediaController;
|
|
14
|
+import android.widget.ImageButton;
|
16
|
15
|
import android.widget.SeekBar;
|
17
|
|
-import android.widget.Toast;
|
18
|
|
-import android.widget.VideoView;
|
|
16
|
+
|
|
17
|
+import androidx.annotation.NonNull;
|
|
18
|
+import androidx.appcompat.app.AppCompatActivity;
|
|
19
|
+import androidx.core.app.ActivityCompat;
|
|
20
|
+import androidx.core.content.ContextCompat;
|
|
21
|
+
|
|
22
|
+import com.hjq.permissions.OnPermissionCallback;
|
|
23
|
+import com.hjq.permissions.Permission;
|
|
24
|
+import com.hjq.permissions.XXPermissions;
|
19
|
25
|
|
20
|
26
|
import java.io.File;
|
21
|
27
|
import java.io.IOException;
|
22
|
|
-import java.util.Timer;
|
23
|
|
-import java.util.TimerTask;
|
|
28
|
+import java.util.List;
|
24
|
29
|
|
25
|
|
-public class VideoViewActivity extends Activity {
|
26
|
|
- private Button btn_start_video = null;
|
27
|
|
- private Button btn_stop_video = null;
|
28
|
|
- private SurfaceView surfaceView;
|
29
|
|
- private SurfaceHolder surfaceHolder;
|
|
30
|
+public class VideoViewActivity extends AppCompatActivity {
|
30
|
31
|
|
31
|
|
- private MediaPlayer m = null;
|
32
|
|
- private Timer mTimer;
|
33
|
|
- private TimerTask mTimerTask;
|
|
32
|
+ private MediaPlayer mediaPlayer;
|
|
33
|
+ private SeekBar sb_main_play;
|
|
34
|
+ private SurfaceView sv_main_sur;
|
34
|
35
|
|
35
|
|
- private boolean isChanging=false;//互斥变量,防止定时器与SeekBar拖动时进度冲突
|
36
|
36
|
@Override
|
37
|
|
- public void onCreate(Bundle savedInstanceState) {
|
|
37
|
+ protected void onCreate(Bundle savedInstanceState) {
|
38
|
38
|
super.onCreate(savedInstanceState);
|
39
|
39
|
setContentView(R.layout.activity_video_view);
|
|
40
|
+ permission();
|
|
41
|
+ sb_main_play = (SeekBar) findViewById(R.id.sb_main_play);
|
|
42
|
+ sv_main_sur = (SurfaceView) findViewById(R.id.sv_main_sur);
|
40
|
43
|
|
41
|
|
- //----------Media控件设置---------//
|
42
|
|
- m=new MediaPlayer();
|
43
|
|
-
|
44
|
|
- //播放结束之后弹出提示
|
45
|
|
- m.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){
|
|
44
|
+ sb_main_play.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
|
46
|
45
|
@Override
|
47
|
|
- public void onCompletion(MediaPlayer arg0) {
|
48
|
|
- Toast.makeText(VideoViewActivity.this, "结束", 1000).show();
|
49
|
|
- m.release();
|
|
46
|
+ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
|
50
|
47
|
}
|
51
|
|
- });
|
52
|
48
|
|
53
|
|
- //----------定时器记录播放进度---------//
|
54
|
|
- mTimer = new Timer();
|
55
|
|
- mTimerTask = new TimerTask() {
|
56
|
49
|
@Override
|
57
|
|
- public void run() {
|
58
|
|
- if(isChanging==true)
|
59
|
|
- return;
|
60
|
|
-
|
|
50
|
+ public void onStartTrackingTouch(SeekBar seekBar) {
|
61
|
51
|
}
|
62
|
|
- };
|
63
|
52
|
|
64
|
|
- mTimer.schedule(mTimerTask, 0, 10);
|
|
53
|
+ @Override
|
|
54
|
+ public void onStopTrackingTouch(SeekBar seekBar) {
|
|
55
|
+ //获取当前进度条位置
|
|
56
|
+ int currentPosition=seekBar.getProgress();
|
|
57
|
+ //跳转到某个位置播放
|
|
58
|
+ mediaPlayer.seekTo(currentPosition);
|
|
59
|
+ }
|
|
60
|
+ });
|
|
61
|
+ }
|
65
|
62
|
|
|
63
|
+ private void permission() {
|
|
64
|
+ if (ContextCompat.checkSelfPermission(VideoViewActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
|
|
65
|
+ } else {
|
|
66
|
+ ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 123);
|
|
67
|
+ }
|
66
|
68
|
|
67
|
|
- btn_start_video = (Button) this.findViewById(R.id.Button03);
|
68
|
|
- btn_stop_video = (Button) this.findViewById(R.id.Button04);
|
69
|
|
- btn_start_video.setOnClickListener(new ClickEvent());
|
70
|
|
- btn_stop_video.setOnClickListener(new ClickEvent());
|
71
|
|
- surfaceView = (SurfaceView) findViewById(R.id.SurfaceView01);
|
72
|
|
- surfaceHolder = surfaceView.getHolder();
|
73
|
|
- surfaceHolder.setFixedSize(100, 100);
|
74
|
|
- surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
|
|
69
|
+ XXPermissions.with(this)
|
|
70
|
+ .permission(Permission.READ_EXTERNAL_STORAGE)
|
|
71
|
+ .request(new OnPermissionCallback() {
|
|
72
|
+
|
|
73
|
+ @Override
|
|
74
|
+ public void onGranted(@NonNull List<String> permissions, boolean allGranted) {
|
|
75
|
+ if (!allGranted) {
|
|
76
|
+ return;
|
|
77
|
+ }
|
|
78
|
+ }
|
|
79
|
+
|
|
80
|
+ @Override
|
|
81
|
+ public void onDenied(@NonNull List<String> permissions, boolean doNotAskAgain) {
|
|
82
|
+ if (doNotAskAgain) {
|
|
83
|
+ } else {
|
|
84
|
+ }
|
|
85
|
+ }
|
|
86
|
+ });
|
75
|
87
|
}
|
76
|
|
- /*
|
77
|
|
- * 按键事件处理
|
78
|
|
- */
|
79
|
|
- class ClickEvent implements View.OnClickListener{
|
80
|
|
- @Override
|
81
|
|
- public void onClick(View v) {
|
82
|
|
- if(v==btn_start_video)
|
83
|
|
- {
|
84
|
|
- m.reset();//恢复到未初始化的状态
|
85
|
|
- File file=new File(getSdPath());
|
86
|
|
- File[] files = file.listFiles();
|
87
|
|
-
|
88
|
|
- File lFile = null;
|
89
|
|
- for (File f : files) {
|
90
|
|
- lFile = new File(f.getPath());
|
91
|
|
- Log.e("czb", f.getPath());
|
92
|
|
- }
|
93
|
|
- try {
|
94
|
|
- m.setDataSource(lFile.getPath());
|
95
|
88
|
|
96
|
|
- } catch (IOException e) {
|
97
|
|
- throw new RuntimeException(e);
|
|
89
|
+ @Override
|
|
90
|
+ public void onRequestPermissionsResult(int requestCode,
|
|
91
|
+ String permissions[], int[] grantResults) {
|
|
92
|
+ super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
|
93
|
+ switch (requestCode) {
|
|
94
|
+ case 123: {
|
|
95
|
+ if (grantResults.length > 0
|
|
96
|
+ && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
|
97
|
+ } else {
|
98
|
98
|
}
|
99
|
|
- m.setAudioStreamType(AudioManager.STREAM_MUSIC);
|
100
|
|
- m.setDisplay(surfaceHolder);
|
101
|
|
-
|
102
|
|
- try {
|
103
|
|
- m.prepare();
|
104
|
|
-
|
105
|
|
- } catch (IllegalArgumentException e) {
|
106
|
|
- // TODO Auto-generated catch block
|
107
|
|
- e.printStackTrace();
|
108
|
|
- } catch (IllegalStateException e) {
|
109
|
|
- // TODO Auto-generated catch block
|
110
|
|
- e.printStackTrace();
|
111
|
|
- } catch (IOException e) {
|
112
|
|
- // TODO Auto-generated catch block
|
113
|
|
- e.printStackTrace();
|
|
99
|
+ return;
|
|
100
|
+ }
|
|
101
|
+ }
|
|
102
|
+ }
|
|
103
|
+
|
|
104
|
+ public void playOrPause(View view){
|
|
105
|
+ final ImageButton ib= (ImageButton) view;
|
|
106
|
+ if(mediaPlayer==null){//如果没有歌
|
|
107
|
+ //mediaPlayer = MediaPlayer.create(this, R.raw.a);
|
|
108
|
+ long tmp = 0l;
|
|
109
|
+ File files = new File(getSdPath());
|
|
110
|
+ if(files.isDirectory()) {
|
|
111
|
+ File jumpFile = null;
|
|
112
|
+ for(File file : files.listFiles()) {
|
|
113
|
+ if(file.lastModified() > tmp) {
|
|
114
|
+ tmp = file.lastModified();
|
|
115
|
+ jumpFile = new File(file.getAbsoluteFile().toURI());
|
|
116
|
+ }
|
114
|
117
|
}
|
115
|
|
- m.start();
|
|
118
|
+ Intent it = new Intent(Intent.ACTION_VIEW);
|
|
119
|
+ it.setDataAndType(Uri.parse(jumpFile.getAbsolutePath()), "video/mp4");
|
|
120
|
+ startActivity(it);
|
116
|
121
|
}
|
|
122
|
+ }else if(mediaPlayer.isPlaying()){//如果正在播放
|
|
123
|
+ //暂停
|
|
124
|
+ mediaPlayer.pause();
|
|
125
|
+ //开始图标
|
|
126
|
+ ib.setImageResource(android.R.drawable.ic_media_play);
|
|
127
|
+ }else {
|
|
128
|
+ //开始播放
|
|
129
|
+ mediaPlayer.start();
|
|
130
|
+ //暂停图标
|
|
131
|
+ ib.setImageResource(android.R.drawable.ic_media_pause);
|
|
132
|
+
|
117
|
133
|
}
|
118
|
134
|
}
|
119
|
135
|
|
|
@@ -126,24 +142,16 @@ public class VideoViewActivity extends Activity {
|
126
|
142
|
}
|
127
|
143
|
return null;
|
128
|
144
|
}
|
129
|
|
- /*
|
130
|
|
- * SeekBar进度改变事件
|
131
|
|
- */
|
132
|
|
- class SeekBarChangeEvent implements SeekBar.OnSeekBarChangeListener{
|
133
|
|
- @Override
|
134
|
|
- public void onProgressChanged(SeekBar seekBar, int progress,
|
135
|
|
- boolean fromUser) {
|
136
|
|
- // TODO Auto-generated method stub
|
137
|
|
- }
|
138
|
|
- @Override
|
139
|
|
- public void onStartTrackingTouch(SeekBar seekBar) {
|
140
|
|
- isChanging=true;
|
141
|
|
- }
|
142
|
145
|
|
|
146
|
+ class MyThread extends Thread{
|
143
|
147
|
@Override
|
144
|
|
- public void onStopTrackingTouch(SeekBar seekBar) {
|
145
|
|
- m.seekTo(seekBar.getProgress());
|
146
|
|
- isChanging=false;
|
|
148
|
+ public void run() {
|
|
149
|
+ super.run();
|
|
150
|
+ while(sb_main_play.getProgress()<sb_main_play.getMax()){
|
|
151
|
+ //获得音乐当前的播放位置
|
|
152
|
+ int currentPosition=mediaPlayer.getCurrentPosition();
|
|
153
|
+ sb_main_play.setProgress(currentPosition);
|
|
154
|
+ }
|
147
|
155
|
}
|
148
|
156
|
}
|
149
|
157
|
}
|