zoukankan      html  css  js  c++  java
  • 音乐播放器的实现(简易版)

    截图:



    代码如下:

    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"/>



  • 相关阅读:
    区别@ControllerAdvice 和@RestControllerAdvice
    Cannot determine embedded database driver class for database type NONE
    使用HttpClient 发送 GET、POST、PUT、Delete请求及文件上传
    Markdown语法笔记
    Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
    Mysql 查看连接数,状态 最大并发数(赞)
    OncePerRequestFilter的作用
    java连接MySql数据库 zeroDateTimeBehavior
    Intellij IDEA 安装lombok及使用详解
    ps -ef |grep xxx 输出的具体含义
  • 原文地址:https://www.cnblogs.com/suncoolcat/p/3329190.html
Copyright © 2011-2022 走看看