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

截图:



代码如下:

1、main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="女神牌音乐播放器(章泽天,我的女神)" 
        />
    
    <EditText 
        android:id="@+id/et_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="2.mp3"
        />
    
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal">
        
        <Button 
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="播放"
            android:onClick="play"
            />
        
        
        
        <Button 
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="暂停"
            android:onClick="pause"
            />
        
        
        <Button 
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="停止"
            android:onClick="stop"
            />
        
        <Button 
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="重播"
            android:onClick="reset"
            />
        
    </LinearLayout>
</LinearLayout>


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

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帅气的东哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值