第一行代码笔记 系统自带 媒体播放 音频播放MediaPlayer 视频播放VideoView

示例代码

public class MediaPlayActivity extends AppCompatActivity implements View.OnClickListener {
    private MediaPlayer mediaplay = new MediaPlayer();
    private VideoView videoView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_media_play);
        initView();
    }

    private void initView() {
        findViewById(R.id.start).setOnClickListener(this);
        findViewById(R.id.pause).setOnClickListener(this);
        findViewById(R.id.stop).setOnClickListener(this);

        findViewById(R.id.vd_start).setOnClickListener(this);
        findViewById(R.id.vd_pause).setOnClickListener(this);
        findViewById(R.id.vd_replay).setOnClickListener(this);
        videoView = ((VideoView) findViewById(R.id.vd_show));
//        运行时权限判断
        if (ContextCompat.checkSelfPermission(MediaPlayActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(MediaPlayActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
        } else {
            initMediaPlayer();
            initVidoPlay();
        }
    }

    /**
     * 初始化视频播放参数
     */
    private void initVidoPlay() {
        File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/Camera", "20170223_113151.mp4");
        videoView.setVideoPath(file.getPath());
    }

    /**
     * 初始化音频播放参数
     */
    private void initMediaPlayer() {
        try {
            File file = new File(Environment.getExternalStorageDirectory() + "/msc/", "tts.wav");
            mediaplay.setDataSource(file.getPath());//指定音频文件的路径
            mediaplay.prepare();//进入准备状态
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 运行时权限的管理
     *
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    @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();
                    initVidoPlay();
                } else {
                    Toast.makeText(this, "没有权限啦~~", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
//            音频
            case R.id.start:
                if (!mediaplay.isPlaying()) {
                    mediaplay.start();
                }
                break;
            case R.id.pause:
                if (mediaplay.isPlaying()) {
                    mediaplay.pause();
                }
                break;
            case R.id.stop:
                if (mediaplay.isPlaying()) {
                    mediaplay.reset();
                    initMediaPlayer();
                }
                break;

//              视频
            case R.id.vd_start:
                if (!videoView.isPlaying()) {
                    videoView.start();
                }
                break;
            case R.id.vd_pause:
                if (videoView.isPlaying()) {
                    videoView.pause();
                }
                break;
            case R.id.vd_replay:
                if (videoView.isPlaying()) {
                    videoView.resume();
                }
                break;

        }

    }

    /**
     * 记得在这里释放资源
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != mediaplay) {
            mediaplay.stop();
            mediaplay.release();
        }
        if (null != videoView) {
            videoView.suspend();
        }

    }
}


布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_media_play"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.lvyequeen.test.day05.mediaplay.MediaPlayActivity">

    <LinearLayout

        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/start"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="start" />

        <Button
            android:id="@+id/pause"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="pause" />

        <Button
            android:id="@+id/stop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="stop" />
    </LinearLayout>


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:background="@color/colorPrimary"
        android:gravity="center_horizontal"
        android:text="视频播放" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:gravity="center_horizontal"
        android:orientation="horizontal">

        <Button
            android:id="@+id/vd_start"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="start" />

        <Button
            android:id="@+id/vd_pause"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="pause" />

        <Button
            android:id="@+id/vd_replay"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="replay" />
    </LinearLayout>

    <VideoView
        android:id="@+id/vd_show"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值