Android学习之MediaPlayer视频播放

来源:互联网 发布:mac如何关闭dashboard 编辑:程序博客网 时间:2024/05/17 06:23

播放视频常用的方法
ActivityMain.java

package com.example.playvdieo;import android.content.pm.PackageManager;import android.os.Environment;import android.support.annotation.NonNull;import android.support.v4.app.ActivityCompat;import android.support.v4.content.ContextCompat;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.Toast;import android.widget.VideoView;import android.Manifest;import java.io.File;public class MainActivity extends AppCompatActivity implements View.OnClickListener{    private Button play;    private Button pause;    private Button stop;    private VideoView videoView;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        play = (Button)findViewById(R.id.play);        play.setOnClickListener(this);        pause = (Button)findViewById(R.id.pause);        pause.setOnClickListener(this);        stop = (Button)findViewById(R.id.stop);        stop.setOnClickListener(this);        videoView = (VideoView)findViewById(R.id.video_view);        //判断有无授权,有就直接进行资源初始化,没有就向用户请求授权        if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)                != PackageManager.PERMISSION_GRANTED){            ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},                    1);        }else {            initVideoSource();//初始化MediaPlayer        }    }    @Override    public void onClick(View view) {        switch (view.getId()){            case R.id.play:                if (!videoView.isPlaying()){                    videoView.start();                }                break;            case R.id.pause:                if (videoView.isPlaying()){//                    videoView.pause();                    videoView.suspend();                }                break;            case R.id.stop:                if (videoView.isPlaying()){                    videoView.resume();//重新播放                }        }    }    public void initVideoSource(){        File file = new File(Environment.getExternalStorageDirectory(),"video.mp4");        videoView.setVideoPath(file.getPath());    }    @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){                    initVideoSource();                }else {                    Toast.makeText(this,"拒绝授权将无法使用程序!",Toast.LENGTH_SHORT).show();                    finish();                }                break;            default:                break;        }    }    @Override    protected void onDestroy() {        super.onDestroy();        if (videoView != null){            videoView.suspend();        }    }}

布局文件

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content">        <Button            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:id="@+id/play"            android:text="播放"/>        <Button            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:id="@+id/pause"            android:text="暂停"/>        <Button            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:id="@+id/stop"            android:text="停止"/>    </LinearLayout>    <VideoView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/video_view"/></LinearLayout>

注册AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.playvdieo">    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:roundIcon="@mipmap/ic_launcher_round"        android:supportsRtl="true"        android:theme="@style/AppTheme">        <activity android:name=".MainActivity">            <intent-filter>                <action android:name="android.intent.action.MAIN"/>                <category android:name="android.intent.category.LAUNCHER"/>            </intent-filter>        </activity>    </application></manifest>
原创粉丝点击