MediaPlayer实现带播放条的音乐播放和视频播放,可以作为程序参考

复习了一下android,写了一个mediaplayer播放的程序实现音乐的播放和视频的播放,程序效果图如图:


完成的基本功能有:1,读取音乐文件 2.播放音乐文件 3,读取视频文件4,播放视频文件

主要程序源代码:

package com.huangcheng.media;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Toast;

public class MediaDemoActivity extends Activity {
	//控制音乐
	private SeekBar seekbar_music;
	private Button start_music_btn;
	private Button stop_music_btn;
	//控制视频
	private SeekBar seekbar_video;
	private Button start_video_btn;
	private Button stop_video_btn;
	private SurfaceView surfaceview_video;
	private SurfaceHolder surfaceview_holder;
	
	//播放器
	private MediaPlayer media=null;
	private MediaPlayer media_video=null;
	private Timer timer;
	private TimerTask timertask_music;
	private TimerTask timertask_video;
	
	//互斥量,seekbar拖动和Timer
	private volatile boolean ischanging=false;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //控制media
        media=new MediaPlayer();
        media_video=new MediaPlayer();
        media.setOnCompletionListener(new OnCompletionListener() {
			
			@Override
			public void onCompletion(MediaPlayer arg0) {
				// TODO Auto-generated method stub
				Toast.makeText(MediaDemoActivity.this, "播放结束", Toast.LENGTH_SHORT).show();
				media.release();
			}
		});
        media_video.setOnCompletionListener(new OnCompletionListener() {
			
			@Override
			public void onCompletion(MediaPlayer arg0) {
				// TODO Auto-generated method stub
				Toast.makeText(MediaDemoActivity.this, "播放结束", Toast.LENGTH_SHORT).show();
				media_video.release();
			}
		});
        //Timer记录播放时刻
        timer=new Timer();
        timertask_music=new TimerTask() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				//正在拖动seekbar,停止Timer
				if(ischanging==true){
					return;
				}
				else{
						seekbar_music.setProgress(media.getCurrentPosition());
				}
			}
		};
		timertask_video=new TimerTask() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				//正在拖动seekbar,停止Timer
				if(ischanging==true){
					return;
				}
				else{
						seekbar_video.setProgress(media_video.getCurrentPosition());
				}
			}
		};
		
		timer.schedule(timertask_music, 0, 10);//开始记录,每10ms记录一次
		timer.schedule(timertask_video, 0, 10);//开始记录,每10ms记录一次
		//更新控件
		seekbar_music=(SeekBar)findViewById(R.id.seekbar_music);
		seekbar_video=(SeekBar)findViewById(R.id.seekbar_video);
		start_music_btn=(Button)findViewById(R.id.start_music_btn);
		stop_music_btn=(Button)findViewById(R.id.stop_music_btn);
		start_video_btn=(Button)findViewById(R.id.start_video_btn);
		stop_video_btn=(Button)findViewById(R.id.stop_video_btn);
		surfaceview_video=(SurfaceView)findViewById(R.id.surfaceview_video);
        surfaceview_holder=surfaceview_video.getHolder();
        surfaceview_holder.setFixedSize(100, 100);
        surfaceview_holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        
        seekbar_music.setEnabled(false);
        seekbar_video.setEnabled(false);
        // 添加处理seekbar事件
        seekbar_music.setOnSeekBarChangeListener(new SeekbarmusicEvent());
        seekbar_video.setOnSeekBarChangeListener(new SeekbarvideoEvent());
        //添加music按键处理
        start_music_btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				media.reset();
				media=MediaPlayer.create(MediaDemoActivity.this, R.raw.m);
				seekbar_music.setMax(media.getDuration());
				try {
					media.prepare();
				} catch (IllegalStateException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				media.start();
		        seekbar_music.setEnabled(true);
			}
		});
        stop_music_btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				media.stop();
		        seekbar_music.setEnabled(false);
			}
		});
        start_video_btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				media_video.reset();
				media_video=MediaPlayer.create(MediaDemoActivity.this, R.raw.v);
				seekbar_video.setMax(media_video.getDuration());
				media_video.setAudioStreamType(AudioManager.STREAM_MUSIC);
				media_video.setDisplay(surfaceview_holder);
				try {
					media_video.prepare();
				} catch (IllegalStateException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				media_video.start();
				seekbar_video.setEnabled(true);
			}
		});
        stop_video_btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				media_video.stop();
				seekbar_video.setEnabled(false);
			}
		});
        
    }
    //处理拖动seekbarmusic事件
    class SeekbarmusicEvent implements SeekBar.OnSeekBarChangeListener{
    	@Override
    	public void onProgressChanged(SeekBar seekBar, int progress,
    			boolean fromUser) {
    		// TODO Auto-generated method stub
    		
    	}
    	@Override
    	public void onStartTrackingTouch(SeekBar seekBar) {
    		// TODO Auto-generated method stub
    		ischanging=true;//停止timer
    	}
    	@Override
    	public void onStopTrackingTouch(SeekBar seekBar) {
    		// TODO Auto-generated method stub
    		media.seekTo(seekBar.getProgress());
    		ischanging=false;//开启timer
    	}
    }
    class SeekbarvideoEvent implements SeekBar.OnSeekBarChangeListener{
    	@Override
    	public void onProgressChanged(SeekBar seekBar, int progress,
    			boolean fromUser) {
    		// TODO Auto-generated method stub
    		
    	}
    	@Override
    	public void onStartTrackingTouch(SeekBar seekBar) {
    		// TODO Auto-generated method stub
    		ischanging=true;//停止timer
    	}
    	@Override
    	public void onStopTrackingTouch(SeekBar seekBar) {
    		// TODO Auto-generated method stub
    		media_video.seekTo(seekBar.getProgress());
    		ischanging=false;//开启timer
    	}
    }
}
程序的布局文件是:


    

基本上代码注释的已经比较详细,我会把源码打包上传,有需要的可以下载

源码地址: http://download.csdn.net/detail/hc260164797/4331677

你可能感兴趣的:(android)