android视频播放

视频播放我们用到的是MediaPlayer,显示控件使用的surfaceView

我们向SD卡中先添加个视频文件,我的是xajh.3gp,不要用mp4,MP4会出现 should have subtitle controller already set的错误,应该是格式的问题造成的

我们看下布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.ssln.video.MainActivity" >
    <SurfaceView
        android:id="@+id/surfaceView"
        android:layout_width="320dp"
        android:layout_height="200dp"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="播放" />

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

代码如下

package com.ssln.video;

import java.io.IOException;

import android.app.Activity;
import android.graphics.PixelFormat;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener,Callback {


    
    private Button btnPlay,btnStop;
    private SurfaceHolder surfaceHolder;
    private SurfaceView surfaceView;
    private MediaPlayer  mPlayer;
    private boolean isPause=false;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        btnPlay=(Button)findViewById(R.id.button1);
        btnStop=(Button)findViewById(R.id.button2);
        btnPlay.setOnClickListener(this);
        btnStop.setOnClickListener(this);
        
        this.getWindow().setFormat(PixelFormat.UNKNOWN); //设置窗口为未知像素模式
        surfaceView=(SurfaceView)findViewById(R.id.surfaceView);
        surfaceHolder=surfaceView.getHolder();
        surfaceHolder.addCallback(this);        //添加回调函数
        surfaceHolder.setFixedSize(176, 144);
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        mPlayer=new MediaPlayer();
    }

    /**
     * 自定义播放视频函数
     * @param strPath
     */
    private void PlayVideo(String strPath)
    {
        //如果是播放状态,就从新开始播放
        if(mPlayer.isPlaying()){
            mPlayer.reset();
            mPlayer.release();
        }
        mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mPlayer.setDisplay(surfaceHolder); //设置影片在surfaceHolder中播放
    
        try {
            //设置播放路径
            mPlayer.setDataSource(strPath);
            mPlayer.prepare();
        } catch (Exception e) {
            e.printStackTrace();
        }
        mPlayer.start();
    }
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        
    }

    @Override
    public void onClick(View v) {
        if(v==btnPlay)
        {
            isPause=false; 
            PlayVideo(Environment.getExternalStorageDirectory().getPath()+"/xajh.3gp");
        }
        else if(v==btnStop){    //如果是播放就暂停
            if(!isPause){
                mPlayer.pause();
                isPause=true;
            }
            else    //如果是暂停就继续播放
            {
                mPlayer.start();
                isPause=false;
            }
        }
    }


}

最后不要忘记加入SD卡的读取权限

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ssln.video"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />
    
    <!-- SD卡访问权限 -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

效果如下

原文地址:https://www.cnblogs.com/alwaysfirst/p/4004923.html