Android 用service实现视频播放器

第一步:写service方法
package com.geek.video.service;


import java.io.File;
import java.io.IOException;


import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Binder;
import android.os.Environment;
import android.os.Handler;
import android.os.IBinder;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;


public class MyVideoService extends Service {
SurfaceHolder surfaceHolder;
MediaPlayer mediaPlayer;
public MyBinder myBinder;
public static SeekBar progressBar;
File file;


//
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return myBinder;
}


// 创建
@Override
public void onCreate() {
super.onCreate();
mediaPlayer = new MediaPlayer();
myBinder = new MyBinder();


}


// 开始
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
return super.onStartCommand(intent, flags, startId);
}


// 销毁
@Override
public void onDestroy() {
// TODO Auto-generated method stub
mediaPlayer.stop();
mediaPlayer.release();
super.onDestroy();
}


public class MyBinder extends Binder {
// 播放
public void play(SurfaceHolder surfaceHolder) {
MyVideoService.this.play(surfaceHolder);
}


// 停止
public void cease() {
MyVideoService.this.cease();
}


// 重播
public void reset(SurfaceHolder surfaceHolder) {
MyVideoService.this.reset(surfaceHolder);
}


// 暂停
public void suspend() {
MyVideoService.this.suspend();
}
}


// 停止
public void cease() {
// 停止
mediaPlayer.stop();
// 释放资源
mediaPlayer.release();
}


// 重播
public void reset(SurfaceHolder surfaceHolder) {
// 停止
mediaPlayer.stop();
// 调播放方法
play(surfaceHolder);
}


// 暂停
public void suspend() {
// 判断是否是播放状态
if (mediaPlayer.isPlaying()) {
// 如果是播放状态就暂停
mediaPlayer.pause();
} else {
// 如果不是播放状态就播放
mediaPlayer.start();
// 调进程刷新进度条
new Thread(run).start();
}


}


// 播放
public void play(SurfaceHolder surfaceHolder) {
// 重置
mediaPlayer.reset();
// 文件路径
file = new File(Environment.getExternalStorageDirectory()
+ "/video.mp4");
// 准备
try {
// 数据路径
mediaPlayer.setDataSource(file.getAbsolutePath());
// 展示
mediaPlayer.setDisplay(surfaceHolder);
// 准备
mediaPlayer.prepare();
// 调进程刷新进度条
new Thread(run).start();
// 进度条的改变事件来设置播放进度
progressBar.setOnSeekBarChangeListener(new myjindu());


mediaPlayer.setOnPreparedListener(new mypreparedListener());
} catch (Exception e) {
e.printStackTrace();
}
}


//
class mypreparedListener implements OnPreparedListener {


@Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
mediaPlayer.start();
}


}


// 设置进度
public class myjindu implements OnSeekBarChangeListener {


@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
if (fromUser) {
System.out.println(progress + "---" + fromUser);
// 设置播放进度
mediaPlayer.seekTo(progress);
}
}


@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}


@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}


}


//
Handler progressHandler = new Handler();
Runnable run = new Runnable() {


public void run() {
if (mediaPlayer == null) {
mediaPlayer = new MediaPlayer();
}
// 设置最大值
int max = mediaPlayer.getDuration();
System.out.println(max);
progressBar.setMax(max);
// 设置进度条
int currentPosition = mediaPlayer.getCurrentPosition();
System.out.println(currentPosition);
progressBar.setProgress(currentPosition);
// 每隔100 milliseconds更新进度条
if (mediaPlayer.isPlaying()) {
progressHandler.postDelayed(run, 100);
}
}
};
}




第二步:调用service
package com.geek.video;


import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Toast;


import com.geek.video.service.MyVideoService;
import com.geek.video.service.MyVideoService.MyBinder;
import com.geek.video.service.MyVideoService.myjindu;


public class MainActivity extends Activity {
Intent service = new Intent("com.geek.myvideo");
private ServiceConnection conn;
public MyBinder myBinder;
public SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
SeekBar seekBar;
myjindu jindu;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video);
seekBar = (SeekBar) findViewById(R.id.s_rate);
surfaceView = (SurfaceView) findViewById(R.id.sv_videoid);
// 给MyVideoService的progressBar静态属性赋值
MyVideoService.progressBar = seekBar;
// 服务连接
conn = new MyServiceConnection();
bindService(service, conn, Context.BIND_AUTO_CREATE);
startService(service);


// 用来操作Surface,
surfaceHolder = surfaceView.getHolder();
// 设置分辨率
surfaceHolder.setFixedSize(174, 144);
// 不使用surfaceview自身的缓冲机制,把得到的数据直接输出
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
// 保持屏幕的高亮
surfaceHolder.setKeepScreenOn(true);


}


@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
unbindService(conn);
}


class MyServiceConnection implements ServiceConnection {


@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
myBinder = (MyBinder) service;
}


@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub


}


}


public void videoclick(View view) {
int id = view.getId();
switch (id) {
case R.id.iv_play:
Toast.makeText(this, "播放", 500).show();
myBinder.play(surfaceHolder);
break;
case R.id.iv_pause:
Toast.makeText(this, "暂停", 500).show();
myBinder.suspend();
break;
case R.id.iv_reset:
Toast.makeText(this, "重播", 500).show();
myBinder.reset(surfaceHolder);
break;
case R.id.iv_stop:
Toast.makeText(this, "停止", 500).show();
myBinder.cease();
break;
default:
break;
}
}
}

你可能感兴趣的:(Android)