Android开发--简单音乐播放器(附源码)

文章目录

  • 1.效果图
  • 2.功能说明
      • 1.登录功能说明
      • 2.主界面功能的实现
      • 3.音乐播放功能实现
  • 最后

1.效果图

我编写的程序的功能不单单是只有音乐播放,还有几个其他的功能,都是当时在学android的时候编写的。最近突然看到这个程序,觉得不能就这么把它放在自己电脑上的android项目中,因为以后能再看到的机会就更少了,哈哈哈!!。所以我决定写点东西来纪念一下当时编写这个小程序的那段岁月。看完后又稍微改了改,最终我个人觉得就音乐播放和记事本的功能还可以,我这里就只给大家伙看看我的音乐播放器的大致功能,如果有想要源码的小伙伴,可以私信我哦!!!!
1.登录界面

Android开发--简单音乐播放器(附源码)_第1张图片
2.主界面

Android开发--简单音乐播放器(附源码)_第2张图片
3.主界面

Android开发--简单音乐播放器(附源码)_第3张图片
4.播放界面

Android开发--简单音乐播放器(附源码)_第4张图片

2.功能说明

1.登录功能说明

在进行编写登录功能的时候我想过很多用于存储用户信息的方式,最后选择了使用数据库。
下面的逻辑大致就是通过获取到EditText上输入的内容与用户信息的数据库中的数据进行对比实现登录的功能。具体的逻辑实现,直接看源码!!!
数据库如何创建和SQlite的使用语法,这里就不多赘述了,哈哈哈,因为我太懒啦!!!!!
这里我把主要代码贴在下面:
1.java主代码

package com.example.mysubject.mainactivity;

import java.util.ArrayList;

import com.example.mysubject.R;
import com.example.mysubject.Extra.MyDB;
import com.example.mysubject.Extra.User;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements OnKeyListener  {
	private MyDB mydb;
	private Button landbutton;
	private Button logonbutton;
	private EditText Ename;
	private EditText Epassword;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mydb = new MyDB(this);
        Initdata();
    }
    public void Initdata(){
    	landbutton = (Button) findViewById(R.id.button);
        logonbutton = (Button) findViewById(R.id.button2);
        Ename = (EditText) findViewById(R.id.edit1);
        Epassword = (EditText) findViewById(R.id.edit2);
        landbutton.setOnClickListener(new onclicklistener());
        logonbutton.setOnClickListener(new onclicklistener());
        landbutton.setOnKeyListener(this);
    }
    //验证账号
    private void load(String name,String password){
    	ArrayList data = mydb.getAllData();
        boolean match = false;
        for (int i = 0; i < data.size(); i++) {
            User user = data.get(i);
            if (name.equals(user.getName()) && password.equals(user.getPassword())) {
                match = true;
                break;
            } else {
                match = false;
            }
        }
        if (match) {
        	Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
        	Intent intent = new Intent(MainActivity.this,LoginActivity.class);
        	startActivity(intent);
        } else {
            Toast.makeText(MainActivity.this, "用户名或密码不正确,请重新输入", Toast.LENGTH_SHORT).show();
        }
    }
    private class onclicklistener implements View.OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.button:
				String name = Ename.getText().toString().trim();
                String password = Epassword.getText().toString().trim();
                if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(password)) {
                	load(name, password);
                } else {
                    Toast.makeText(MainActivity.this, "请输入你的用户名或密码", Toast.LENGTH_SHORT).show();
                }
                break;
			case R.id.button2:
				Intent intent = new Intent(MainActivity.this, RegisterActivity.class);
				startActivity(intent);
			    break;
			default:
				break;
			}
		}
    	
    }
	@Override
	public boolean onKey(View v, int arg1, KeyEvent event) {
		switch (event.getAction()) {
		case KeyEvent.KEYCODE_ENTER:
			String name = Ename.getText().toString().trim();
            String password = Epassword.getText().toString().trim();
            if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(password)) {
            	load(name, password);
            } else {
                Toast.makeText(MainActivity.this, "请输入你的用户名或密码", Toast.LENGTH_SHORT).show();
            }
			break;

		default:
			break;
		}
		return false;
	}
	
    
}

2.xml代码
xml代码我就不过多的说啥了,小伙伴们可以自行设计,我这里的xml布局不太好看,大家可以自己去设计!!!


    
    
        
        
    
    
        
     
    
       
    

2.主界面功能的实现

主界面自己定义了一个list的适配器来进行显示和自定义了一个AlertDialog来进行弹窗显示。

package com.example.mysubject.mainactivity;

import com.example.mysubject.R;
import com.example.mysubject.Extra.Adapter_Login;
import com.example.mysubject.Extra.CommonDialog;
import com.example.mysubject.Extra.Setonclicklistnear;

import android.os.Bundle;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;


public class LoginActivity extends Activity implements OnItemClickListener, OnClickListener {
	private ListView listView;
	private Adapter_Login adapter;
	private Intent intent;
	private ImageButton imageButton;
	private CommonDialog dialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_main);
        	Initview();
        
        
        if(NavUtils.getParentActivityName(LoginActivity.this)!=null){
        	getActionBar().setDisplayShowHomeEnabled(true);
        }
        
    }
    private void Initview(){
    	 adapter = new Adapter_Login();
    	 dialog = new CommonDialog(this);
         adapter.setContext(this);
         listView=(ListView) findViewById(R.id.lv);
         imageButton = (ImageButton) findViewById(R.id.imgbutton);
         listView.setAdapter(adapter);
         listView.setOnItemClickListener(this);
         imageButton.setOnClickListener(this);
    }
    private void setselectactivity(int arg){
    	switch (arg) {
		case 0:
			 intent = new Intent(this,MusicActivity.class);
			 dialog.setTitle("提醒");
			 dialog.setMessage("您是否进入音乐??");
			 dialog.setPositive("确定");
			 dialog.setNegtive("取消");
			 dialog.Onsetclicklistnear(new Setonclicklistnear() {
				
				@Override
				public void setpositive() {
					// TODO Auto-generated method stub
					
					startActivity(intent);
				}
				
				@Override
				public void setnegtive() {
					// TODO Auto-generated method stub
					dialog.dismiss();
				}
			});
			 dialog.show();
			 
			break;
		case 1:
			 intent = new Intent(this,NotePadActivity.class);
			 dialog.setTitle("提醒");
			 dialog.setMessage("您是否进入记事本??");
			 dialog.setPositive("确定");
			 dialog.setNegtive("取消");
			 dialog.Onsetclicklistnear(new Setonclicklistnear() {
				
				@Override
				public void setpositive() {
					// TODO Auto-generated method stub
					startActivity(intent);
				}
				
				@Override
				public void setnegtive() {
					// TODO Auto-generated method stub
					dialog.dismiss();
				}
			});
			 dialog.show();
			break;
		case 2:
			 intent = new Intent(this,TranslateActivity.class);
			 dialog.setTitle("提醒");
			 dialog.setMessage("您是否进入翻译??");
			 dialog.setPositive("确定");
			 dialog.setNegtive("取消");
			 dialog.Onsetclicklistnear(new Setonclicklistnear() {
				
				@Override
				public void setpositive() {
					// TODO Auto-generated method stub
					startActivity(intent);
				}
				
				@Override
				public void setnegtive() {
					// TODO Auto-generated method stub
					dialog.dismiss();
				}
			});
			 dialog.show();
			break;
		default:
			break;
		}
    }

	@Override
	public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-gonItemClickenerated method stub
		setselectactivity(arg2);
	}

    @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;
    }
	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		intent = new Intent(this,MainActivity.class);
		startActivity(intent);
	}
    
}

2.list适配器代码

package com.example.mysubject.Extra;

import com.example.mysubject.R;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class Adapter_Login extends BaseAdapter{
	private String[] name={"音乐","笔记本","翻译"};	
	private int[] icon={R.drawable._1,R.drawable._2,R.drawable._4};
	private int[] color={R.color.Music,R.color.Memorial,R.color.translate};
	private Context context;
	
	public void setContext(Context context) {
		this.context = context;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return name.length;
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return name[arg0];
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int arg0, View v, ViewGroup arg2) {
		// TODO Auto-generated method stub
		Holder holder;
		if(v==null){
			v = View.inflate(context, R.layout.activity_adapter_music, null);
			holder = new Holder();
			holder.imageView = (ImageView) v.findViewById(R.id.iv);
			holder.textView = (TextView) v.findViewById(R.id.tv);
			v.setTag(holder);
		}else{
			holder=(Holder) v.getTag();
		}
		holder.imageView.setImageResource(icon[arg0]);
		holder.textView.setText(name[arg0]);
		holder.textView.setBackgroundResource(color[arg0]);
		return v;
	}
	private class Holder{
		ImageView imageView;
		TextView textView;
	}
}

3.自定义AlertDialog代码

package com.example.mysubject.Extra;

import com.example.mysubject.R;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class CommonDialog extends AlertDialog{
	private TextView TVtitle;
	private TextView TVmessage;
	private Button BTpositive;
	private Button BTnegtive;
	private String title,message;
	private String positive,negtive;
	public Setonclicklistnear setonclicklistnear;
	public CommonDialog(Context context) {
		super(context);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_dialog_all);
		Initview();
		setonclicklistnear();
	}
	//初始化控件
	private void Initview(){
		TVtitle = (TextView) findViewById(R.id.title);
		TVmessage = (TextView) findViewById(R.id.message);
		BTpositive = (Button) findViewById(R.id.positive);
		BTnegtive = (Button) findViewById(R.id.negtive);
	}
	//数据初始化
	private void InitData(){
		if (!TextUtils.isEmpty(title)) {
            TVtitle.setText(title);                 //设置标题控件的文本为自定义的title
            TVtitle.setVisibility(View.VISIBLE); //标题控件设置为显示状态
        }else {
        	TVtitle.setVisibility(View.GONE);     //标题控件设置为隐藏状态
        }
        if (!TextUtils.isEmpty(message)) {
            TVmessage.setText(message); //设置消息控件的文本为自定义的message信息
        }
        //如果自定义了按钮的文本,则按钮显示自定义的文本,否则,按钮显示“确定”或“取消”文本
        if (!TextUtils.isEmpty(positive)) {
            BTpositive.setText(positive); //设置按钮的文本为自定义的文本信息
        }else {
        	BTpositive.setText("确定");    //设置按钮文本为“确定”
        }
        if (!TextUtils.isEmpty(negtive)) {
            BTnegtive.setText(negtive);
        }else {
        	BTnegtive.setText("取消");
        }
	}
	//设置确定取消按钮的回响
	public void Onsetclicklistnear(Setonclicklistnear setonclicklistnear1){
		this.setonclicklistnear = setonclicklistnear1;
	}
	//设置确定取消按钮
	public void setonclicklistnear(){
		BTpositive.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(setonclicklistnear!=null){
					setonclicklistnear.setpositive();
				}
			}
		});
		BTnegtive.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(setonclicklistnear!=null){
					setonclicklistnear.setnegtive();
				}
			}
		});
		
		
	}
	 @Override
	    public void show() {
	        super.show();
	        InitData();
	    }
	//数据的设置
	public void setTitle(String title) {
		this.title = title;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public void setPositive(String positive) {
		this.positive = positive;
	}
	public void setNegtive(String negtive) {
		this.negtive = negtive;
	}
	
	
}

3.音乐播放功能实现

直接放代码吧,如果有没懂的地方,在评论区或者私信我都可以!!
1.主代码

package com.example.mysubject.musicfragment;



import java.util.ArrayList;

import com.example.mysubject.R;
import com.example.mysubject.mainactivity.MusicActivity;

import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


public class MyMediaplayer extends Activity{
	private ImageButton ibtnPlayOrPause;//暂停按钮
    private ImageButton ibtnNext;//下一首按钮
    private ImageButton ibtnprevious;//上一首按钮
    private ImageButton ibtback;//返回按钮
    private TextView musicname;//歌曲名
    private TextView musicauther;//歌曲作者
    private Context context;//上下文对象
    private MediaPlayer player;//播放对象
    private ImageView ImgAnim;//用于设置动画的图片
    private ObjectAnimator animator;
    
    
    private int Position;
    private int[]  musiclist;
    private ArrayList autherlist;
    private ArrayList namelist;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_mediaplayer_fragement);
		InitView();//初始化
		getdata();//获取相关数据
		setmusic();
		player.start();
		/*设置单击事件*/
		ibtnPlayOrPause.setOnClickListener(new Onclicklistener());
		ibtnNext.setOnClickListener(new Onclicklistener());
		ibtnprevious.setOnClickListener(new Onclicklistener());
		ibtback.setOnClickListener(new Onclicklistener());
}
	//初始化
	private void InitView(){
		ibtnprevious = (ImageButton) findViewById(R.id.bt1);
		ibtnPlayOrPause = (ImageButton) findViewById(R.id.bt2);
		ibtnNext = (ImageButton) findViewById(R.id.bt3);
		ibtback = (ImageButton) findViewById(R.id.back);
		musicauther = (TextView) findViewById(R.id.tv2);
		musicname = (TextView) findViewById(R.id.tv1);
		ImgAnim = (ImageView) findViewById(R.id.iv_music);
		animator = ObjectAnimator.ofFloat(ImgAnim, "rotation", 0f, 360.0f);
	    animator.setDuration(10000);  //动画旋转一周的时间为10秒
	    animator.setInterpolator(new LinearInterpolator());
	    animator.setRepeatCount(-1);  //-1表示设置动画无限循环
		player = new MediaPlayer();
		context = MyMediaplayer.this;
	}
	
	
	
	//获取数据
	private void getdata(){
		Intent intent=getIntent();
		musiclist = getmusic(intent);
		autherlist = getauther(intent);
		namelist = getname(intent);
		Position = getnumber(intent);
	}
	//设置播放监听器
	private class Onclicklistener implements View.OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.bt1:
				previous();
				animator.start();
				break;
			case R.id.bt2:
				if(player.isPlaying()){
					pause();
					animator.pause();
				}else{
					play();
					animator.start();
				}
				break;
			case R.id.bt3:
				next();
				animator.start();
				break;
			case R.id.back:
				Intent intent = new Intent(MyMediaplayer.this,MusicActivity.class);
				startActivity(intent);
				player.stop();
			default:
				break;
			}
		}
		
	}
	//设置音乐名称和作者
	private void setmusic(){
		musicauther.setText(autherlist.get(Position));
		musicname.setText(namelist.get(Position));
	}
	//播放音乐
	private void play(){
		 player.reset();//重置
		 try {
			 String uriStr = "android.resource://" + context.getPackageName() + "/"+musiclist[Position];
			 Uri uri=Uri.parse(uriStr); 
			 player.setDataSource(context, uri);
			 player.prepare();
			 player.start();
			 ibtnPlayOrPause.setImageResource(R.drawable.play);
			 setmusic();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	
	//暂停音乐
	private void pause(){
		player.pause();
		ibtnPlayOrPause.setImageResource(R.drawable.zanting1);
	}
	//播放上一首
	private void previous(){
		if(Position==0){
			Toast.makeText(context, "当前已经是第一首歌啦!", Toast.LENGTH_LONG).show();
			return;
		}else{
			Position--;
			ibtnPlayOrPause.setImageResource(R.drawable.zanting1);
			setmusic();
			play();
		}
	}
	//播放下一首
	private void next(){
		if(Position>=musiclist.length){
			Toast.makeText(context, "当前已经是最后一首歌啦!", Toast.LENGTH_LONG).show();
		}else{
			Position++;
			ibtnPlayOrPause.setImageResource(R.drawable.zanting1);
			setmusic();
			play();
		}
	}
	//获取数据
		public int getnumber(Intent intent){
			Bundle bundle = new Bundle();
			bundle = intent.getBundleExtra("number");
			int number = bundle.getInt("number");
			return number;
			
		}
	//获取数据
	private int[] getmusic(Intent intent){
		Bundle bundle = new Bundle();
		bundle = intent.getBundleExtra("musiclist");
		int[] musiclist = bundle.getIntArray("musiclist");
		return musiclist;
	}
	//获取数据
	private ArrayList getname(Intent intent){
		Bundle bundle = new Bundle();
		bundle = intent.getBundleExtra("namelist");
		ArrayList namelist = bundle.getStringArrayList("namelist");
		return namelist;
	}
	//获取数据
	private ArrayList getauther(Intent intent){
		Bundle bundle = new Bundle();
		bundle = intent.getBundleExtra("autherlist");
		ArrayList autherlist = bundle.getStringArrayList("autherlist");
		return autherlist;
	}

	 
	
		   
	
	
	
}
	
	

最后

因为最近本人的要准备考研,所以代码解释的不是很清楚,哈哈!!,别怪我啊,其实一个人代码能力的提升我个人觉得还是得自己去看代码,然后尝试着自己去写,这样你的能力才会提升的快。好了,音乐播放器的分享就到这了,希望喜欢的小伙伴点点收藏!!
对了,这个软件是用ADT写的,如果有小伙伴导入AS有问题,自行去百度哦!!
链接: https://pan.baidu.com/s/1lDlIv-K8mZPW1mIjjPt5aQ
提取码:wwnb

你可能感兴趣的:(经验,学习,android,android,studio,app)