surfaceView+mediaplayer+seekBar+自定义视频时间监控

这些天一直都在写一个视频播放器,碰到了很多意想不到的问题,在这里,我把部分代码分享一下:

package com.xbys;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;

import android.view.Choreographer;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Chronometer;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.xbys.io.StreamTools;
import com.xbys.network.NetWorkType;
import com.xbys.timer.GetVideotime;


public class MainActivity extends Activity {

	private GridView gridView;
	String[] imageName = {"学习目标","知识内容","实践","知识结构","学习指南","练习","案例","常见问题"
			
	};
	int[] image = {R.drawable.xuexi3,R.drawable.xuexi1,R.drawable.xuexi2,R.drawable.xuexi3,
			R.drawable.xuexi5,R.drawable.xuexi6,R.drawable.xuexi7,R.drawable.xuexi3,
			
	};
	private static final int CHANGE = 0;
	private SlidingMenu menu;
	private TextView tv_content = null ;
	InputStream is;
	String detail;
	private SurfaceView sv;
	private ImageButton video_play ;
	private ImageButton video_screen;
	MediaPlayer mediaPlayer;
	private int currentPosition = 0 ;
	private boolean isplay = true;
	private PlayVideo playVideo;
	private SeekBar seekBar;
	private int max;
	private int i = 1;
	private int j = 1;
	
	int second = 0;
	int minute = 0;
	
	private int flag1;
	private int flag2;
	private int progress;
	private int firstFlag = 0;
	private boolean isflag = true;
	
	private int position3;
	private boolean isPlaying = true;
	private TextView timer_long;
	private TextView play_timer;
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		seekBar = (SeekBar)findViewById(R.id.seekBar);
		timer_long = (TextView) findViewById(R.id.timer_long);
		play_timer = (TextView)findViewById(R.id.play_timer);
		
		seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				progress = seekBar.getProgress();
				isflag = false;
				flag1 = progress / 1006;
				firstFlag++;
				
				if(firstFlag!=1){
					j = 1;
					second = 0;
					minute = 0;
				}
				
				if(mediaPlayer!=null&&mediaPlayer.isPlaying()){
					mediaPlayer.seekTo(progress);
				}
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onProgressChanged(SeekBar seekBar, final int flag, boolean arg2) {
				// TODO Auto-generated method stub	
			
				if(isflag){
					
					if (flag / 1006 == i) {
						i++;
						second++;
						if (second == 60) {
							minute++;
							second = 0;
						}
						if(second<10){
							
							play_timer.setText(minute + ":" +"0"+ second);
						}else{
							play_timer.setText(minute + ":" + second);
						}	
					}
				}else{
					
					
					/**
					 * 控制进度条
					 * 
					 * 
					 */	
					
					
						// 小于60s时
						if (flag1 < 60) {
							//second = flag1;
							
						/*	if (second < 10) {
								
								play_timer.setText(minute + ":" + "0" + second);
							} else {
								play_timer.setText(minute + ":" + second);
							}*/
							
							
							if (flag/1006-flag1 == j) {
								System.out.println("是否执行到这里====="+j);
								System.out.println("是否执行到这里====="+second);
								j++;
								second = flag1+j;
								if (second == 60) {
									minute++;
									second = 0;
								}
								if (second < 10) {
									
									play_timer.setText(minute + ":" + "0" + second);
								} else {
									play_timer.setText(minute + ":" + second);
								}
							}
							
							
							
							
						}
						// 大于60s
						if (flag1 >= 60) {
							flag2 = flag1/60;
							minute = flag2;
							second = flag2%60;
							
							if (second < 10) {
								
								play_timer.setText(minute + ":" + "0" + second);
							} else {
								play_timer.setText(minute + ":" + second);
							}
							
							if (flag/1006-flag1 == j) {
								j++;
								second = second+j;
								if (second == 60) {
									minute++;
									second = 0;
								}
								if (second < 10) {
									
									play_timer.setText(minute + ":" + "0" + second);
								} else {
									play_timer.setText(minute + ":" + second);
								}
								
							}
							
			
							
						}
						/**
						 * 控制进度条结束	
						 */
					
					
				}
						if(flag<max&&flag>max-500){
							i = 1;
							second = 0;
							minute = 0;
						}
						
						
						
						
				}
			
		});
		
		gridView =  (GridView) findViewById(R.id.gridview);
		video_play = (ImageButton) findViewById(R.id.video_play);
		video_screen = (ImageButton)findViewById(R.id.screen);
		video_play.setBackground(getResources().getDrawable(R.drawable.play));
		video_play.setOnClickListener(new VideoPlayerListener());
		sv = (SurfaceView) findViewById(R.id.sv);
		sv.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
		//回调方法
		sv.getHolder().addCallback(new Callback(){

			@Override
			public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2,
					int arg3) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void surfaceCreated(SurfaceHolder arg0) {
				// TODO Auto-generated method stub
				if(currentPosition>0){
					new PlayVideo(currentPosition).start();
				
				}
			}

			@Override
			public void surfaceDestroyed(SurfaceHolder arg0) {
				// TODO Auto-generated method stub
				if(mediaPlayer!=null&&mediaPlayer.isPlaying()){
					currentPosition = mediaPlayer.getCurrentPosition();
					stop();
				}
			}}
			);
		
		//侧拉菜单
		 menu = new SlidingMenu(this);
	        menu.setMode(SlidingMenu.RIGHT);
	        menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
	        menu.setShadowWidthRes(R.dimen.activity_horizontal_margin);
	        //menu.setShadowDrawable(R.drawable.yuefu);
	        menu.setBehindOffsetRes(R.dimen.activity_horizontal_margin);
	        menu.setFadeDegree(0.5f);
	        menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
	        menu.setMenu(R.layout.slidingmenu);
	        
	        //将资源文件夹中的文本读到tv_content上
			tv_content = (TextView) findViewById(R.id.tv_content);
	     
	        //变换标题栏的背景
		getActionBar().setBackgroundDrawable(this.getBaseContext().
        		getResources().getDrawable(R.drawable.skinpic_blue));
        getActionBar().show();
        
ArrayList<HashMap<String, Object>>  list=new ArrayList<HashMap<String,Object>>();
		
		for(int i=0;i<8;i++)
		{
			HashMap<String, Object>  map = new HashMap<String, Object>();
			
			   map.put("ItemImage", image[i]);
			   map.put("imageName", imageName[i]);
			   list.add(map);
		}

		SimpleAdapter   adapter = new SimpleAdapter(this, list,R.layout.griview_item,
				new String[]{"ItemImage","imageName"},  
				new int[]{R.id.gridview_imageview,R.id.gridview_textview}); 
		gridView.setAdapter(adapter);
		gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				// TODO Auto-generated method stub
				switch (position) {
				case 0:
					 detail = StreamTools.getStream("resources/dbms.txt", MainActivity.this);
					tv_content.setText(detail);
					menu.showMenu();
					break;
				case 1:
					detail = StreamTools.getStream("resources/content_1.txt", MainActivity.this);
					tv_content.setText(detail);
					menu.showMenu();
					break;
				case 2:
					detail = StreamTools.getStream("resources/content_1.txt", MainActivity.this);
					tv_content.setText(detail);
					menu.showMenu();
					break;
				case 3:
					detail = StreamTools.getStream("resources/content_1.txt", MainActivity.this);
					tv_content.setText(detail);
					menu.showMenu();
					break;
				case 4:
					detail = StreamTools.getStream("resources/content_1.txt", MainActivity.this);
					tv_content.setText(detail);
					menu.showMenu();
					break;
				case 5:
					detail = StreamTools.getStream("resources/content_1.txt", MainActivity.this);
					tv_content.setText(detail);
					menu.showMenu();
					break;
				case 6:
					detail = StreamTools.getStream("resources/content_1.txt", MainActivity.this);
					tv_content.setText(detail);
					menu.showMenu();
					break;
				case 7:
					detail = StreamTools.getStream("resources/content_1.txt", MainActivity.this);
					tv_content.setText(detail);
					menu.showMenu();
					break;
				default:
					break;
				}
			}
		});
	}


	class PlayVideo extends Thread{
		
		int currentPosition;
		
		public PlayVideo(int currentPosition) {
			super();
			this.currentPosition = currentPosition;
		}

		@Override
		public void run() {
			// TODO Auto-generated method stub
			play(currentPosition);
			super.run();
		}
	}
	
	class VideoPlayerListener  implements OnClickListener{

		

		@SuppressWarnings("deprecation")
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.video_play:
				
				new NetWorkType().isWifi(MainActivity.this);
				
				if(isplay){
					video_play.setBackground(getResources().getDrawable(R.drawable.pause));
					new PlayVideo(currentPosition).start();
					isplay = false;
				}else{
					pause();
					video_play.setBackground(getResources().getDrawable(R.drawable.play));
					isplay = true;
				}
				break;
			/*case R.id.screen:
				
					ViewGroup.LayoutParams lp = sv.getLayoutParams();
					
					lp.height = LayoutParams.MATCH_PARENT;
					
					lp.width = LayoutParams.MATCH_PARENT;
					System.out.println(lp.width);
					sv.setLayoutParams(lp);
				break;*/
			default:
				break;
			}
		}
	}
	
	/**
	 * 播放视频
	 * @param currentPosition
	 */

	private void play(final int currentPosition2) {

		String path = "http://192.168.194.51:8080/xbys/oppop.mp4";
		
		try {

			mediaPlayer = new MediaPlayer();
			mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
			mediaPlayer.setDisplay(sv.getHolder());
			mediaPlayer.setDataSource(path);
			mediaPlayer.prepareAsync();
			// mediaPlayer.seekTo(currentPosition);
			mediaPlayer.setOnPreparedListener(new OnPreparedListener() {

				@Override
				public void onPrepared(MediaPlayer mp) {
					// TODO Auto-generated method stub
					mediaPlayer.start();
					mediaPlayer.seekTo(currentPosition2);
					max = mediaPlayer.getDuration();
					
					String timer = new GetVideotime().TimeLong(max);
					timer_long.setText(timer);	
						
						
					
						
					new Thread(){
						public void run() {
						
							seekBar.setMax(max);
							while(mediaPlayer!=null&&mediaPlayer.isPlaying()){
								 position3 = mediaPlayer.getCurrentPosition();
								seekBar.setProgress(position3);
							}
							try {
								Thread.sleep(500);
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							
						};
					}.start();
					
					
				}
			});
			mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
				
				@Override
				public void onCompletion(MediaPlayer arg0) {
					// TODO Auto-generated method stub
					mediaPlayer.seekTo(0);
					video_play.setBackground(getResources().getDrawable(R.drawable.play));
					seekBar.setProgress(0);
					play_timer.setText(0+":"+"00");
				
					//isplay = true;
				}
			});
		} catch (Exception e) {
			// TODO Auto-generated catch block
			Toast.makeText(this, "播放失败", 0).show();
			e.printStackTrace();
		}

	}

/**
 * 停止播放音乐
 */
	private void stop() {
		// TODO Auto-generated method stub
		if (mediaPlayer != null && mediaPlayer.isPlaying()) {
			mediaPlayer.stop();
			mediaPlayer.release();
			mediaPlayer = null;
		}
	}

	private void pause() {

		// TODO Auto-generated method stub
		if (mediaPlayer != null && mediaPlayer.isPlaying()) {
			
			currentPosition = mediaPlayer.getCurrentPosition();
			
			mediaPlayer.pause();
		}
	}

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

}

写的非常乱,但是基本能实现视频播放,如果其中有隐含的bug,希望大牛们能够指导我,我的功力尚浅!

你可能感兴趣的:(surfaceView+mediaplayer+seekBar+自定义视频时间监控)