VideoView
简介
- 在Android上播放音频视频文件一般都是使用VideoView实现的
方法名 |
功能描述 |
setVideoPath() |
设置要播放的视频文件的位置 |
start() |
开始或继续播放视频 |
pause() |
暂停播放视频 |
resume() |
将视频从头开始播放 |
seekTo() |
从指定的位置开始播放视频 |
isPalying() |
判断当前是否正在播放视频 |
getDuration() |
获取载入的视频文件的时长 |
实例(视频播放)
- xml
<Buttonandroid: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" /><Buttonandroid: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" /><Buttonandroid: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" /><VideoViewandroid: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
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/><applicationandroid: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
public class MainActivity extends AppCompatActivity {private VideoView mVideoView;@Overrideprotected 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 {//初始化videoViewinitVideoPath();}}@Overridepublic 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();}}@Overrideprotected void onDestroy() {super.onDestroy();if (mVideoView != null) {mVideoView.suspend();}}
}
- 下载地址
https://github.com/qricis/DoSomeAndroidTest/tree/main/PlayVideoTest
MediaPlayer
实例(播放音频)
- xml
<Buttonandroid: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" /><Buttonandroid: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" /><Buttonandroid: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
public class MainActivity extends AppCompatActivity {private MediaPlayer mMediaPlayer = new MediaPlayer();@Overrideprotected 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();}}@Overridepublic 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:}}@Overrideprotected 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
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.mediaplayer"><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/><applicationandroid: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