[原创]android学习之简单的视频播放器
1.Mainfest.xml文件的配置:<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.video"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.video.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>
2.activity_main.xml文件的配置:
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="视频名称" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/voidename"
android:inputType="none"
/>
<SeekBar
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/seekBar"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="播放"
android:id="@+id/start"
android:onClick="startVideo"
/>
</RelativeLayout>
<SurfaceView
android:layout_width="fill_parent"
android:layout_height="240dp"
android:id="@+id/surfaceView"
/>
</LinearLayout>
3、activity代码的实现:
package com.example.video;
import java.io.File;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.app.Activity;
import android.text.TextUtils;
import android.view.Menu;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText videoText;
private SeekBar sb;
private SurfaceView surfaceView;
private MediaPlayer mediaPlayer;
private File file;
private int position;
private boolean isStart = true;
private Button startButton;
private Handler handler;
private Runnable r;
private MyCallback myCallback;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handler = new Handler();
startButton = (Button) this.findViewById(R.id.start); //播放的按钮
videoText = (EditText) this.findViewById(R.id.voidename); //文件框
sb = (SeekBar) this.findViewById(R.id.seekBar); //进度条
surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView); //视频的显示区域
myCallback = new MyCallback(); //回调类
surfaceView.getHolder().addCallback(myCallback); //获取摄像头添加回调类
r = new MyRunnable();
}
private class MyCallback implements Callback //回调类
{
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder arg0) { //创建surface
// TODO Auto-generated method stub
if(position > 0)
{
start(position);
}
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) { //结束surface
// TODO Auto-generated method stub
if(mediaPlayer.isPlaying())
{
position = mediaPlayer.getCurrentPosition();
mediaPlayer.stop();
mediaPlayer = null;
}
}
}
public void startVideo(View v) //播放按钮的事件
{
String videoName = videoText.getText().toString().trim(); //获取文本框中的内容
file = new File(Environment.getExternalStorageDirectory(), //从sdcard里面获取视频文件
videoName);
if(!TextUtils.isEmpty(videoName) && file.exists())
{
if(isStart)
{
start(0);
isStart = false;
startButton.setText("暂停");
}else
{
stop();
isStart = true;
startButton.setText("播放");
}
}else
{
Toast.makeText(this, "该视频文件不存在", Toast.LENGTH_LONG).show();
}
}
private void stop() //暂停的方法
{
if(mediaPlayer.isPlaying())
{
position = mediaPlayer.getCurrentPosition();
mediaPlayer.pause();
}
}
private void start(int position) //播放的方法
{
try
{
mediaPlayer = new MediaPlayer();
mediaPlayer.reset();
mediaPlayer.seekTo(position);//进度
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDisplay(surfaceView.getHolder());
mediaPlayer.setDataSource(file.getAbsolutePath()); //路径的获取
mediaPlayer.prepare();
//缓冲
mediaPlayer.setOnCompletionListener(new MyOnCompletionListener());
}catch(Exception e)
{
e.printStackTrace();
}
}
private class MyOnCompletionListener implements OnCompletionListener
{
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
sb.setMax(mp.getDuration());//设置视频的最大值
mp.seekTo(position);
mp.start();
handler.post(r); //向Handler中发送一个post请求
}
}
public class MyRunnable implements Runnable //这个类是设置进度条的进度的!
{
@Override
public void run() {
// TODO Auto-generated method stub
try
{
if(mediaPlayer != null && mediaPlayer.isPlaying())
{
sb.setProgress(mediaPlayer.getCurrentPosition());
}
handler.postDelayed(r, 300);
}catch(Exception e)
{
e.printStackTrace();
}
}
}
@Override
protected void onDestroy() { //当activity销毁的时候就移除回调类
// TODO Auto-generated method stub
handler.removeCallbacks(r);
super.onDestroy();
}
}
好帖。收藏了 {:7_139:}支持玄玄~ 支持下 刷个鱼币 刷个鱼币 观摩一下 赞!
页:
[1]