自定义圆形进度条显示MP3播放进度

本篇和大家分享一个非常常见的需求,  圆形进度条显示MP3播放进度,  圆形进度条采用Github上的开源工具ProgressWheel

gitHub地址: https://github.com/Todd-Davies/ProgressWheel


自定义圆形进度条显示MP3播放进度_第1张图片


使用说明:

1. 添加 attrs.xml文件到项目的values目录下

2.  在项目根XMl中添加

xmlns:ProgressWheel="http://schemas.android.com/apk/res-auto"

3. 在需要使用圆形进度条的XML中引用自定义控件类
 

4.  调用 spin()方法开启progress ,   stopSpinning()方法停止progress,  setProgress()设置进度条进度   

package com.yangxt.mymp3playerdemo;

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

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnSeekCompleteListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.MediaStore.Audio.Media;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

/**
 * @author yangxt
 * @date  2014-09-03
 */
public class MainActivity extends Activity {
	
	private ProgressWheel mProgressBar;
	boolean isRunning=false;
	int progress = 0;
	MediaPlayer mPlayer=null;
	Timer mTimer=null;
	TimerTask mTimerTask=null;
	float mGrade=1.00f;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		mProgressBar=(ProgressWheel) findViewById(R.id.progressBar);
		
		
	    onClick();
	}


	
		


	public void onClick() {
		mProgressBar.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(isRunning) {                          
					    Log.i("TAG","停止播放  停止精度条");
					    if(mTimerTask!=null&&mPlayer.isPlaying()){
					    	Cls();
					     }
						mProgressBar.stopSpinning();
					    mProgressBar.setProgress(0);
					    mProgressBar.setText("播放");
					
						isRunning=false;
						
				}else{                              
						
					    Log.i("TAG","播放音乐   设置进度条");
						isRunning=true;
						mProgressBar.spin();
					    playMp3();
						
						setProgressPosition();   
						
						
						
						
					}
				
				
			}

			
		});
	}

	
	/**
	 * 播放Mp3
	 */
	protected void playMp3() {
	   
				try {
					mPlayer=MediaPlayer.create(MainActivity.this, R.raw.shq);//读取音频  ;
					mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); // 设置音频类型
					mPlayer.start();
						
					
				} catch (Exception e) {
					e.printStackTrace();
				} 
				
				
					
				mPlayer.setOnCompletionListener(new OnCompletionListener() {
					
					@Override
					public void onCompletion(MediaPlayer mp) {
						// TODO Auto-generated method stub
						Log.i("TAG","播放结束");
						
						if(mTimerTask!=null&&mPlayer!=null){
							
					    	Cls();
					     }
						
						
						
						mProgressBar.stopSpinning();
					    mProgressBar.setProgress(0);
					    mProgressBar.setText("播放");
					
						isRunning=false;
						
					}

					
				});
				
				
			  
		}
	
	/**
	 * 设置进度条进度
	 */
	public void setProgressPosition() {
		final int duration = mPlayer.getDuration();
		mGrade=duration/360;
		
		mTimer = new Timer();    
		mTimerTask = new TimerTask() {    
		    @Override    
		    public void run() {  
		    	 //Log.i("TAG", "-->"+mPlayer.getCurrentPosition()+"/"+duration);
		    	 runOnUiThread(new Runnable() {

					@Override
					public void run() {
						
						
						float nowPosition=mPlayer.getCurrentPosition()/mGrade;
						mProgressBar.setProgress((int)nowPosition);
						
						float grade=nowPosition/360;
						mProgressBar.setText(String.valueOf(grade).substring(2,4)+"%");
						
					
					}

				});
		    	 
		     	 
		    }    
		};   
		mTimer.schedule(mTimerTask, 0,300);
			
		
	}
	
	
	public void Cls() {
		mPlayer.stop();
		mPlayer.release();
		mPlayer=null;
		
		mTimer.cancel();
		mTimerTask.cancel();
		mTimer=null;
		mTimerTask=null;
	}
	
	
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		if(mTimerTask!=null&&mPlayer!=null){
			
	    	Cls();
	     }
		
		
		super.onPause();
	}
	
	 

}

源码地址:

http://download.csdn.net/detail/u010333409/7864287




你可能感兴趣的:(Android自定义控件)