最近在做这个视频播放器效果是通过语音来控制视频的播放循序,在一个视频播放完后跳转到一个视频播放列表,用户通过声音来选择播放的视频,这个功能暂时由于语音那边还没做好,先按照传统的点击事件点击播放。
这个问题在于理解activity的生命周期,当一个视频播放完后,回调onCompletion()方法(该方法实现OnCompletionListener 接口),在这个方法中设置Intent跳转到listVideo这个activity显示视频的列表,如图:
当点击item后会根据获得的视频名称在视频播放界面播放视频,主要是当你点击后的Intent跳转到播放的activity是,视频是在activity的oncreate()方法里就要初始化的第一次写有关工作项目的博客,还是直接上代码吧:
首先是播放的activity:
package com.Jw.videoplayer;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.MediaController;
import android.widget.VideoView;
public class PlayingActivity extends Activity implements OnErrorListener,
OnCompletionListener {
private VideoView mVideoView;
private MediaController mMediaController;
private String uri;
private boolean isFirstPlaying ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_playing);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
mVideoView = (VideoView) findViewById(R.id.videoplayer);
mVideoView.requestFocus();
mMediaController = new MediaController(this);
mVideoView.setMediaController(mMediaController);
mVideoView.setOnCompletionListener(this);
mVideoView.setOnErrorListener(this);
isFirstPlaying=getIntent().getBooleanExtra("isFirstPlaying", true);
if (isFirstPlaying) {
initData();
} else {
startPlaying();
}
}
/*
* 初始化数据
*/
public void initData() {
// 初始化数据,第一次加载ghsy
uri = "android.resource://" + getPackageName() + "/" + R.raw.ghsy;
if (!TextUtils.isEmpty(uri)) {
mVideoView.setVideoURI(Uri.parse(uri));
}
mMediaController = new MediaController(this);
mVideoView.setMediaController(mMediaController);
}
@Override
protected void onResume() {
mVideoView.start();
super.onResume();
}
@Override
protected void onStart() {
super.onStart();
}
@Override
protected void onPause() {
mVideoView.stopPlayback();
super.onPause();
}
@Override
public void onCompletion(MediaPlayer arg0) {
finish();
// 视频播放完成后回调该方法
Intent myIntent = new Intent();
myIntent.setClass(this, VideoListActivity.class);
myIntent.putExtra("isFirstPlaying", isFirstPlaying);
startActivity(myIntent);
}
@Override
public boolean onError(MediaPlayer arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
return false;
}
public void startPlaying(){
String videoName = getIntent().getStringExtra("videoName");
Log.v("PlayingActivity", videoName);
isFirstPlaying = getIntent().getBooleanExtra("isFirstPlaying", false);
if (videoName.contains("abc")) {
uri = "android.resource://" + getPackageName() + "/" + R.raw.abc;
mVideoView.setVideoURI(Uri.parse(uri));
}else if(videoName.contains("hhh")){
uri = "android.resource://" + getPackageName() + "/" + R.raw.hhh;
mVideoView.setVideoURI(Uri.parse(uri));
}else if(videoName.contains("ghsy")){
uri = "android.resource://" + getPackageName() + "/" + R.raw.ghsy;
mVideoView.setVideoURI(Uri.parse(uri));
}else if(videoName.contains("uuu")){
uri = "android.resource://" + getPackageName() + "/" + R.raw.uuu;
mVideoView.setVideoURI(Uri.parse(uri));
}
}
}
然后这个是显示视频列表的activity:
package com.Jw.videoplayer;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;
import com.Jw.entity.Video;
import com.jwbot.mscservice.MscServicePlugin;
public class VideoListActivity extends Activity {
//语音引擎
private MscServicePlugin mscServicePlugin;
private List<Video> videos;
private Video mvideo;
private ListView lv_video;
private String videoName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_list);
videos=getVideos();
lv_video=(ListView) findViewById(R.id.lv_video);
lv_video.setAdapter(new MyAdapter());
lv_video.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
//Log.v("VideoListActivity",position+"");
Log.v("VideoListActivity",videos.get(position).getVideoName());
videoName=videos.get(position).getVideoName();
boolean isFirstPlaying=getIntent().getBooleanExtra("isFirstPlaying", true);
Intent myIntent=new Intent(VideoListActivity.this,PlayingActivity.class);
isFirstPlaying=false;
myIntent.putExtra("videoName", videoName);
myIntent.putExtra("isFirstPlaying", isFirstPlaying);
startActivity(myIntent);
/*if(isAdapter(videoName)){
myIntent.putExtra("videoName", videoName);
myIntent.putExtra("isFirstPlaying", isFirstPlaying);
startActivity(myIntent);
}*/
}
});
}
@Override
protected void onResume() {
super.onResume();
Log.v("(((((", "onResume");
mscServicePlugin = MscServicePlugin.createInstance(this,
new MyHandler());
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.v(")))))))))", "onDestroy");
/**
* 销毁当前语音引擎
*/
mscServicePlugin.destroyAsrService();
}
public List<Video> getVideos(){
videos=new ArrayList<Video>();
Field[] fields = R.raw.class.getDeclaredFields();
for(int i=0;i<=fields.length;i++){
try {
mvideo=new Video(fields[i].getName());
videos.add(mvideo);
} catch (Exception e) {
e.printStackTrace();
}
}
fields=null;
return videos;
}
private boolean isAdapter(String name){
if(name.contains("abc")){
return true;
}
return false;
}
class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
return videos.size();
}
@Override
public Object getItem(int arg0) {
return null;
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view=null;
if(convertView==null){
LayoutInflater inflater=VideoListActivity.this.getLayoutInflater();
view=inflater.inflate(R.layout.videos_items, null);
}else{
view=convertView;
}
Video video=videos.get(position);
TextView videoName=(TextView) view.findViewById(R.id.tv_videoName);
videoName.setText(video.getVideoName()+"");
return view;
}
}
/*
* 传递消息的Handler
*/
private class MyHandler extends Handler {
/**
* 提供一个语音命令识别参考
*/
@Override
public void handleMessage(Message msg) {
String text = (String) msg.obj;
Log.v("回到主线程,获取识别到内容", text);
if ("build_success".equals(text))
mscServicePlugin.startAsrService();
}
}
}
第一次写有关工作的的代码,当然其中有很多的方法是在语音那一块需要用到,有些代码就需要大家自动过滤了