音乐播放器(一)

package com.gyarmy.musicplay;

import java.io.IOException;

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnErrorListener;
import android.os.Bundle;
import android.app.Activity;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Toast;

public class MainActivity extends Activity implements OnSeekBarChangeListener {
	
	private EditText ed_path;
	private SeekBar ed_bar;
	private Button bt_pause;
	MediaPlayer mp;
	private boolean isOnProcess=false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		ed_path = (EditText) findViewById(R.id.mus_addr);
		ed_bar = (SeekBar) findViewById(R.id.mus_progress);
		bt_pause = (Button)findViewById(R.id.mus_pause);
		
		ed_bar.setOnSeekBarChangeListener(this);
		
	}

	public void play(View v){
		String path= ed_path.getText().toString().trim();
		if(TextUtils.isEmpty(path)){
			Toast.makeText(this, "请输入文件的路径", 0).show();
			return;
		}
		
		if(mp==null){
			mp = new MediaPlayer();
		}
		
		mp.setOnErrorListener(new OnErrorListener() {
			
			@Override
			public boolean onError(MediaPlayer mp, int what, int extra) {
				// TODO Auto-generated method stub
				Toast.makeText(MainActivity.this, "文件不是歌曲,不能播放", 0).show();
				return false;
			}
		});
		mp.reset();
		
		try {
			mp.setDataSource(path);
			mp.prepare();
			
			//播放
			mp.start();
			newProgress();
			
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public void newProgress(){
		new Thread(
				new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						if(mp!=null){
							int duration = mp.getDuration();
							ed_bar.setMax(duration);
							
							while(mp.isPlaying()){
								int curPos = mp.getCurrentPosition();
								if(isOnProcess==false){
									ed_bar.setProgress(curPos);
								}
							}
							
						}
						
						
					}
				}
				).start();
	}
	
	public void pause(View v){
		if(mp!=null)
		{
			if(bt_pause.getText().equals("暂停" ) &&  mp.isPlaying()){
				mp.pause();
				bt_pause.setText("继续");
			}else if(bt_pause.getText().equals("继续")){
				mp.start();
				newProgress();
				bt_pause.setText("暂停");
			}
		}
		
	}
	
	public void stop(View v){
		if(mp!=null)
		{
			mp.stop();
			bt_pause.setText("暂停");
			//释放资源
			mp.release();
			mp=null;
		}
		
	}

	@Override
	public void onProgressChanged(SeekBar seekBar, int progress,
			boolean fromUser) {
		// TODO Auto-generated method stub
		
		//设置歌曲的位置
		isOnProcess = true;
	}

	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {
		// TODO Auto-generated method stub
		
		if(mp!=null){
			
				mp.seekTo(seekBar.getProgress());
				isOnProcess=false;
			
		}
	}
	
}

原文链接: 音乐播放器(一) 版权所有,转载时请注明出处,违者必究。
注明出处格式:流沙团 ( https://gyarmy.com/post-148.html )

发表评论

0则评论给“音乐播放器(一)”