• 【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"/>
  • 相关阅读:
    iOS开发UI篇—Quartz2D使用(信纸条纹)
    iOS开发UI篇—Quartz2D简单使用(三)
    iOS开发UI篇—Quartz2D使用(图片剪切)
    a超链接之返回顶部的两种实现方法
    学习windows编程 day2 之滚动条使用
    首页轮播图
    商城动态菜单
    放大镜二:大图的移动
    放大镜一:图片上部添加可移动遮盖层
    php循环删除文件夹和目录
  • 原文地址:https://www.cnblogs.com/CoderTian/p/6204266.html
Copyright © 2020-2023  润新知