音乐播放器的实现(简易版)

截图:

音乐播放器的实现(简易版)_第1张图片


代码如下:

1、main.xml



    
    
    
    
    
    
        
        


2、MainActivity

package com.njupt.mp3_1;

import java.io.File;

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.Context;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

	private MediaPlayer mp;
	private File file;
	private EditText et_name;
	private boolean pause;
	private int position = 0;
	private TelephonyManager tm;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		et_name = (EditText) findViewById(R.id.et_name);
		tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
		tm.listen(new MyPhoneStateListener(), PhoneStateListener.LISTEN_CALL_STATE);
	}

	private class MyPhoneStateListener extends PhoneStateListener{
		@Override
		public void onCallStateChanged(int state, String incomingNumber) {
			super.onCallStateChanged(state, incomingNumber);
		    
			switch (state) {
			case TelephonyManager.CALL_STATE_IDLE:
				if(position > 0 && mp != null){
					mp.seekTo(position);
					mp.start();
				}
				break;
			case TelephonyManager.CALL_STATE_OFFHOOK:
				if(mp!= null){
					if(mp.isPlaying()){
						position = mp.getCurrentPosition();
						mp.pause();
					}
				}
				
			case TelephonyManager.CALL_STATE_RINGING:
				if(mp != null){
					if(mp.isPlaying()){
						position = mp.getCurrentPosition();
						mp.pause();
					}
				}
			default:
				break;
			}
		}
	}
	
	public void play(View v){
		String name = et_name.getText().toString();
		file = new File(Environment.getExternalStorageDirectory(),name);
	    if(!file.exists()){
	    	Toast.makeText(this, "sorry,文件不存在", 1).show();
	    }else{
	    	play();
	    }
	}
	
	public void play(){
		try{
			mp = new MediaPlayer();
			mp.reset();
			mp.setDataSource(file.getAbsolutePath());
			mp.prepare();
			mp.setOnPreparedListener(new MyOnPreparedListener());
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
	
	private class MyOnPreparedListener implements OnPreparedListener{
		@Override
		public void onPrepared(MediaPlayer mp) {
			mp.start();
		}
	}
	
	public void pause(View v){
		Button bt = (Button) v;
		
		if(mp != null){
			if(mp.isPlaying()){
				mp.pause();
				bt.setText("继续");
				pause = true;
			}else{
				if(pause){
					mp.start();
					bt.setText("暂停");
					pause = false;
				}
			}
		}
	}
	
	public void stop(View v){
		if(mp != null){
			mp.stop();
		}
	}
	
	public void reset(View v){
		stop(v);
		play(v);
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
	    
		if(mp != null){
			mp.release();
			mp = null;
		}
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}


3、AndroidManifest.xml




你可能感兴趣的:(android,精通android开发)