实现效果图:
安卓使用MediaPlayer实现播放内存卡内的视频文件,并添加进度条显示,可实现视频播放和暂停效果。
Activity代码:
package com.example.android_vedioplayer_camera_recorder; import android.media.MediaPlayer; import android.net.Uri; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.view.SurfaceView; import android.view.View; import android.widget.ImageButton; import android.widget.SeekBar; import android.widget.TextView; import java.io.IOException; /** * Created by Administrator on 2017/2/22. */ public class VedioPlayer extends AppCompatActivity { private MediaPlayer mediaPlayer; private SurfaceView sv_main_surface; private TextView tv_start; private TextView tv_end; private SeekBar seekbar; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_vedioplayer); sv_main_surface = (SurfaceView) findViewById(R.id.sv_main_surface); //开始时间 tv_start = (TextView) findViewById(R.id.tv_start); //结束时间 tv_end = (TextView) findViewById(R.id.tv_end); //进度条 seekbar = (SeekBar) findViewById(R.id.seekbar); //设置监听 seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int i, boolean b) { //获取音乐总时间 int duration2=mediaPlayer.getDuration()/1000; //获取音乐当前播放的位置 int position=mediaPlayer.getCurrentPosition(); //开始时间 tv_start.setText(calculateTime(position/1000)); //结束时间 tv_end.setText(calculateTime(duration2)); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { int progress=seekBar.getProgress(); //在当前位置播放 mediaPlayer.seekTo(progress); } }); } //按钮点击事件 public void isPlayOrPause(View view){ ImageButton imageButton= (ImageButton) view; //判断音频文件是否为空 if(mediaPlayer==null){ //为空则创建音乐文件并播放改变按钮样式 //播放内存卡中文件 mediaPlayer=new MediaPlayer(); try { mediaPlayer.setDataSource(this, Uri.parse("file://mnt/sdcard/movies/sister.mp4")); //准备 mediaPlayer.prepare(); } catch (IOException e) { e.printStackTrace(); } //将媒体播放器捕捉的画面展示到SurfaceView mediaPlayer.setDisplay(sv_main_surface.getHolder()); //开始播放 mediaPlayer.start(); //设置button imageButton.setImageResource(android.R.drawable.ic_media_pause); //获取音乐总时间 int duration=mediaPlayer.getDuration(); //将音乐总时间设置为SeekBar的最大值 seekbar.setMax(duration); //线程修改时间值 new MyThread().start(); //音乐文件正在播放,则暂停并改变按钮样式 }else if(mediaPlayer.isPlaying()){ mediaPlayer.pause(); imageButton.setImageResource(android.R.drawable.ic_media_play); }else{ //启动播放 mediaPlayer.start(); imageButton.setImageResource(android.R.drawable.ic_media_pause); } } //使用线程控制进度条时间 class MyThread extends Thread{ @Override public void run() { super.run(); while(seekbar.getProgress()<=seekbar.getMax()){ //获取音乐当前播放的位置 int position=mediaPlayer.getCurrentPosition(); //放入SeekBar中 seekbar.setProgress(position); } } } //计算播放时间 public String calculateTime(int time){ int minute; int second; if(time>=60){ minute=time/60; second=time%60; return minute+":"+second; }else if(time<60){ second=time; return "0:"+second; } return null; } }
layout.xml代码:
<?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"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="播放内存视频文件" android:gravity="center" android:textSize="30sp" /> <SurfaceView android:layout_width="match_parent" android:layout_height="300dp" android:layout_marginTop="30dp" android:id="@+id/sv_main_surface" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="40dp" > <TextView android:layout_marginLeft="10dp" android:layout_width="40dp" android:layout_height="wrap_content" android:id="@+id/tv_start" /> <SeekBar android:layout_width="270dp" android:layout_height="wrap_content" android:id="@+id/seekbar" /> <TextView android:layout_width="40dp" android:layout_height="wrap_content" android:id="@+id/tv_end" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:gravity="center_horizontal" > <ImageButton android:layout_width="60dp" android:layout_height="60dp" android:id="@+id/bt_media" android:onClick="isPlayOrPause" android:src="@android:drawable/ic_media_play" /> </LinearLayout> </LinearLayout>
注释:本demo实现的是播放内存卡内的视频文件,需要提前准备内存卡资源文件。