• 【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"/>
  • 相关阅读:
    Codeforces 379 F. New Year Tree
    「NOI2018」屠龙勇士
    「NOI2018」归程
    深入理解Java枚举类型(enum)
    2018.6.9-学习笔记
    String、StringBuffer与StringBuilder介绍
    HashMap和HashTable到底哪不同?
    HashMap详解
    List,Set和Map详解及其区别和他们分别适用的场景
    Java中高级面试题(1)
  • 原文地址:https://www.cnblogs.com/CoderTian/p/6204266.html
Copyright © 2020-2023  润新知