我是靠谱客的博主 平常御姐,这篇文章主要介绍Android基础之VideoView | MediaPlayer | 带实例VideoViewMediaPlayer,现在分享给大家,希望可以做个参考。

VideoView

简介

  • 在Android上播放音频视频文件一般都是使用VideoView实现的

方法名

功能描述

setVideoPath()

设置要播放的视频文件的位置

start()

开始或继续播放视频

pause()

暂停播放视频

resume()

将视频从头开始播放

seekTo()

从指定的位置开始播放视频

isPalying()

判断当前是否正在播放视频

getDuration()

获取载入的视频文件的时长

实例(视频播放)

  • xml
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<Button android:id="@+id/play" android:layout_width="103dp" android:layout_height="45dp" android:layout_marginTop="4dp" android:onClick="playVideo" android:text="@string/play_video" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/pause" android:layout_width="114dp" android:layout_height="47dp" android:layout_marginTop="4dp" android:text="@string/pause_video" android:onClick="pauseVideo" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.183" app:layout_constraintStart_toEndOf="@+id/play" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/replay" android:layout_width="122dp" android:layout_height="50dp" android:layout_marginTop="4dp" android:text="@string/replay_video" android:onClick="replayVideo" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.947" app:layout_constraintStart_toEndOf="@+id/pause" app:layout_constraintTop_toTopOf="parent" /> <VideoView android:id="@+id/video_view" android:layout_width="409dp" android:layout_height="678dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/pause" />
  • Manifest
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
  • MainActivity
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
public class MainActivity extends AppCompatActivity { private VideoView mVideoView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1); } else { //初始化videoView initVideoPath(); } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { switch (requestCode) { case 1: if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { initVideoPath(); } else { Toast.makeText(this,"拒绝权限将无法使用程序",Toast.LENGTH_SHORT).show(); finish(); } break; default: } } private void initVideoPath() { File file = new File(Environment.getDownloadCacheDirectory(),"movie.mp4"); mVideoView.setVideoPath(file.getPath()); } public void playVideo(View view) { if (!mVideoView.isPlaying()) { mVideoView.start(); } } public void pauseVideo(View view) { if (!mVideoView.isPlaying()) { mVideoView.pause(); } } public void replayVideo(View view) { if (!mVideoView.isPlaying()) { mVideoView.resume(); } } @Override protected void onDestroy() { super.onDestroy(); if (mVideoView != null) { mVideoView.suspend(); } } }
  • 下载地址

https://github.com/qricis/DoSomeAndroidTest/tree/main/PlayVideoTest


MediaPlayer

实例(播放音频)

  • xml
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<Button android:id="@+id/play" android:layout_width="232dp" android:layout_height="58dp" android:layout_marginTop="132dp" android:text="@string/play" android:onClick="playMusic" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.497" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/pause" android:layout_width="232dp" android:layout_height="58dp" android:layout_marginTop="40dp" android:text="@string/pause" android:onClick="pauseMusic" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.497" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/play" /> <Button android:id="@+id/stop" android:layout_width="232dp" android:layout_height="58dp" android:layout_marginTop="40dp" android:text="@string/stop" android:onClick="stopMusic" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.497" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/pause" />
  • MainActivity
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
public class MainActivity extends AppCompatActivity { private MediaPlayer mMediaPlayer = new MediaPlayer(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1); } else { initMediaPlayer(); } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { switch (requestCode) { case 1: if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { initMediaPlayer(); } else { Toast.makeText(this,"拒绝权限将无法使用程序",Toast.LENGTH_SHORT).show(); finish(); } default: } } @Override protected void onDestroy() { super.onDestroy(); if (mMediaPlayer != null) { mMediaPlayer.stop(); mMediaPlayer.release(); } } private void initMediaPlayer() { try { File file = new File(Environment.getExternalStorageState(),"music.mp3"); //指定音频文件路径 mMediaPlayer.setDataSource(file.getPath()); //进入到准备状态 mMediaPlayer.prepare(); } catch (Exception e) { e.printStackTrace(); } } public void playMusic(View view) { if (!mMediaPlayer.isPlaying()) { mMediaPlayer.start(); } } public void pauseMusic(View view) { if (!mMediaPlayer.isPlaying()) { mMediaPlayer.pause(); } } public void stopMusic(View view) { if (!mMediaPlayer.isPlaying()) { mMediaPlayer.stop(); initMediaPlayer(); } } }
  • Manifest
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.mediaplayer"> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
  • 下载地址

https://github.com/qricis/DoSomeAndroidTest/tree/main/MediaPalyer

最后

以上就是平常御姐最近收集整理的关于Android基础之VideoView | MediaPlayer | 带实例VideoViewMediaPlayer的全部内容,更多相关Android基础之VideoView内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(51)

评论列表共有 0 条评论

立即
投稿
返回
顶部