zoukankan      html  css  js  c++  java
  • 【Android 多媒体应用】使用 MediaPlayer 播放视频

    1.MainActivity.java

    import android.media.AudioManager;
    import android.media.MediaPlayer;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.SurfaceHolder;
    import android.view.SurfaceView;
    import android.view.View;
    import android.widget.Button;
    
    import java.io.IOException;
    
    public class MainActivity extends AppCompatActivity implements View.OnClickListener, SurfaceHolder.Callback {
    
        private MediaPlayer mPlayer;
        private SurfaceView sfv_show;
        private SurfaceHolder surfaceHolder;
        private Button btn_start;
        private Button btn_pause;
        private Button btn_stop;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initUI();
    
        }
    
        private void initUI() {
            sfv_show = (SurfaceView) findViewById(R.id.sfv_show);
            btn_start = (Button) findViewById(R.id.btn_start);
            btn_pause = (Button) findViewById(R.id.btn_pause);
            btn_stop = (Button) findViewById(R.id.btn_stop);
    
            btn_start.setOnClickListener(this);
            btn_pause.setOnClickListener(this);
            btn_stop.setOnClickListener(this);
    
            //初始化SurfaceHolder类,SurfaceView的控制器
            surfaceHolder = sfv_show.getHolder();
            surfaceHolder.addCallback(this);
            //显示的分辨率,不设置为视频默认
            surfaceHolder.setFixedSize(320, 240);
    
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn_start:
                    if(!mPlayer.isPlaying()) {
                        mPlayer.start();
                    }
                    break;
                case R.id.btn_pause:
                    mPlayer.pause();
                    break;
                case R.id.btn_stop:
                    mPlayer.stop();
                    break;
            }
        }
    
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            mPlayer = new MediaPlayer();
            mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            //设置显示视频显示在SurfaceView上
            mPlayer.setDisplay(surfaceHolder);
            try {
                mPlayer.setDataSource("/sdcard/TopGirl.mp4");
                mPlayer.prepare();
            }catch (IOException e){
    
                e.printStackTrace();
            }
        }
    
        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}
    
        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {}
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            if (mPlayer.isPlaying()) {
                mPlayer.stop();
            }
            mPlayer.release();
        }
    }

    2.activity_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <SurfaceView
            android:id="@+id/sfv_show"
            android:layout_width="match_parent"
            android:layout_height="300dp" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/btn_start"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="开始" />
    
        <Button
            android:id="@+id/btn_pause"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="暂停 " />
    
        <Button
            android:id="@+id/btn_stop"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="停止" />
    </LinearLayout>
    
    </LinearLayout>

    3.AndroidMainfest.xml中添加权限

     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
  • 相关阅读:
    Git从入门到放弃
    Flex布局
    网络模型与TCP协议
    命令行技巧
    React环境搭建及部署
    Vue环境搭建及部署
    Python 集合set()
    Python-100天代码
    删除Windows启动管理器下的加载项
    windos7操作系统下easyBCD2.3安装Ubuntu18.04.1-desktop-amd64.iso双系统
  • 原文地址:https://www.cnblogs.com/CoderTian/p/6204266.html
Copyright © 2011-2022 走看看