我是靠谱客的博主 平常御姐,最近开发中收集的这篇文章主要介绍Android基础之VideoView | MediaPlayer | 带实例VideoViewMediaPlayer,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

VideoView

简介

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

方法名

功能描述

setVideoPath()

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

start()

开始或继续播放视频

pause()

暂停播放视频

resume()

将视频从头开始播放

seekTo()

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

isPalying()

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

getDuration()

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

实例(视频播放)

  • xml
<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
<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
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
<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
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
<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 | MediaPlayer | 带实例VideoViewMediaPlayer所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部