package com.curiousby.baoyou.cn.DemoMediaPlayer.activity; import android.app.Activity; import android.media.AudioManager; import android.media.MediaPlayer; import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.view.MotionEvent; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.widget.SeekBar; import java.io.File; import java.io.IOException; import java.util.Timer; import java.util.TimerTask; import com.curiousby.baoyou.cn.R; /* * *1.VideoView本身就是对SurfaceView和MediaPlayer做了一个封装 *2.实现视频列表播放 * * 如果读取本地文件,和网络的话 需要添加权限 * * */ public class MainActivity extends Activity { private MediaPlayer mediaPlayer; private SurfaceView surfaceView; private SeekBar seekbar; //读取本地文件 private File file=new File("/storage/emulated/legacy/Pictures/Screenshots", "1.mp4"); //访问网络视频 private String uri="http://7xqwgk.com1.z0.glb.clouddn.com/mysqlconsole.mp4"; protected TimerTask timeTask; private Timer timer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.e("baoyou", "file.exists()"+file.exists()); seekbar = (SeekBar) findViewById(R.id.sb_video_seekbar); surfaceView = (SurfaceView) findViewById(R.id.surfaceView); mediaPlayer = new MediaPlayer(); //获取SurfaceHolder 可以通过该接口来操作SurfaceView中的Surface SurfaceHolder surfaceHolder = surfaceView.getHolder(); //设置Meiaplayer的准备监听 mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { //准备完成后播放 mediaPlayer.start(); } }); surfaceHolder.addCallback(new SurfaceHolder.Callback() { //当SurfaceView中Surface创建时回掉 //该方法表示Surface已经创建完成,可以在该方法中进行绘图操作 @Override public void surfaceCreated(SurfaceHolder holder) { mediaPlayer.reset(); try { //设置视屏文件图像的显示参数 mediaPlayer.setDisplay(holder); // file.getAbsolutePath(); //本地视频 seekbar.setEnabled(true); //Log.e("baoyou", file.length()+""+"=========mediaPlayer.getDuration()========="); // seekbar.setMax(Integer.parseInt(file.length()+"")); mediaPlayer.setDataSource(file.getAbsolutePath()); //uri 网络视频 //mediaPlayer.setDataSource(MainActivity.this, Uri.parse(uri)); //prepare();表示准备工作同步进行,(准备工作在UI线程中进行) //当播放网络视频时,如果网络不要 会报ARN 所以不采用该方法 mediaPlayer.prepare(); //异步准备 准备工作在子线程中进行 当播放网络视频时候一般采用此方法 //mediaPlayer.prepareAsync(); timer=new Timer(); timeTask=new TimerTask() { @Override public void run() { Log.e("baoyou", mediaPlayer.getCurrentPosition()+"=========mediaPlayer.getCurrentPosition()========="); int position = mediaPlayer.getCurrentPosition(); int duration = mediaPlayer.getDuration(); if (duration > 0) { long pos = seekbar.getMax() * position / duration; seekbar.setProgress((int) pos); } } }; timer.schedule(timeTask, 0, 1000); } catch (Exception e) { e.printStackTrace(); } } //当SurfaceView的大小发生改变时候触发该方法 @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } //Surface销毁时回掉 //当Surface销毁时候,同时把MediaPlayer也销毁 @Override public void surfaceDestroyed(SurfaceHolder holder) { if (mediaPlayer!=null) { mediaPlayer.stop(); //释放资源 mediaPlayer.release(); } } }); //设置 surfaceView点击监听 surfaceView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: if (mediaPlayer.isPlaying()) { mediaPlayer.pause(); } else { mediaPlayer.start(); } break; } //返回True代表事件已经处理了 return true; } }); } }
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_vertical"> <RelativeLayout android:id="@+id/rl_video_content" android:layout_width="wrap_content" android:layout_height="wrap_content" > <SurfaceView android:id="@+id/surfaceView" android:layout_centerInParent="true" android:layout_width="match_parent" android:layout_height="wrap_content" /> <SeekBar android:id="@+id/sb_video_seekbar" android:layout_above="@+id/ll_video_operation" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white" > </SeekBar> <LinearLayout android:id="@+id/ll_video_operation" android:layout_width="match_parent" android:layout_height="30dip" android:gravity="center_horizontal" android:background="@color/white" android:layout_alignParentBottom="true" > <Button android:id="@+id/ll_video_operation_left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开始" android:textColor="@color/black" android:background="@android:color/transparent" /> </LinearLayout> </RelativeLayout> </RelativeLayout>
捐助开发者
在兴趣的驱动下,写一个免费
的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。
谢谢您的赞助,我会做的更好!