Android音乐播放器

具体功能实现:
欢迎页面
Android音乐播放器_第1张图片

登录页面
可输入账户密码进行登录、登录失败弹出提示、登陆成功转到主页
可选择忘记密码进行修改
可选择新用户注册进行注册
Android音乐播放器_第2张图片Android音乐播放器_第3张图片Android音乐播放器_第4张图片

修改界面
可输入已有账户进行修改
输入成功出现提示
后跳转可进行密码修改
Android音乐播放器_第5张图片Android音乐播放器_第6张图片Android音乐播放器_第7张图片

播放页面
Android音乐播放器_第8张图片

操作及注意事项:
先注册,注册成功后可进行修改密码和和登录
登陆成功后可进行常规点击播放音乐与其他操作
主要类:

音乐播放服务类

/**
 * 类说明:音乐播放服务类
 * @author wangzhihao
 * @version 
 */
public class ControlPlay extends Service {

	//声明一个音频播放器
	public static MediaPlayer myMediaPlayer;

	public static MainActivity c_ma;

	public static int playing_id = 0;

	public static Music_infoAdapter m_in;
	
	//创建线程
	public Thread thread;
	Handler handler = new Handler();
	
	public boolean playFlag = true;
	public int vTemp = 0;
	

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		initMediaSource(initMusicUri(0));
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		if (myMediaPlayer != null) {
			myMediaPlayer.stop();
			myMediaPlayer.release();
			myMediaPlayer = null;
		}
	}

	@Override
	public void onStart(Intent intent, int startId) {
		String playFlag = intent.getExtras().getString("control");
		if ("play".equals(playFlag)) {
			playMusic();
		} else if ("next".equals(playFlag)) {
			playNext();
		} else if ("front".equals(playFlag)) {
			playFront();
		} else if ("listClick".equals(playFlag)) {
			playing_id = intent.getExtras().getInt("musicId_1");
			initMediaSource(initMusicUri(playing_id));
			playMusic();
		} else if ("gridClick".equals(playFlag)) {
			playing_id = intent.getExtras().getInt("musicId_2");
			initMediaSource(initMusicUri(playing_id));
			playMusic();
		}
	}

	/**
	 * 初始化媒体对象
	 * 
	 * @param mp3Path
	 *            mp3路径
	 */
	public void initMediaSource(String mp3Path) {
		Uri mp3Uri = Uri.parse(mp3Path);
		if (myMediaPlayer != null) {
			myMediaPlayer.stop();
			myMediaPlayer.reset();
			myMediaPlayer = null;
		}
		// 为myMediaPlayer创建对象
		myMediaPlayer = MediaPlayer.create(this, mp3Uri);
		//myMediaPlayer = MediaPlayer.create(this, R.raw.aaaa);
	//	c_ma.startRhythm(myMediaPlayer);
	}

	/**
	 * 返回列表第几行的歌曲路径
	 * 
	 * @param _id
	 *            表示歌曲序号,从0开始
	 */
	public String initMusicUri(int _id) {
		playing_id = _id;
		String s;
		// 判断列表和列表长度不为空时才获取
		if (Music_infoAdapter.musicList != null&& Music_infoAdapter.musicList.size() != 0) {
			s = Music_infoAdapter.musicList.get(_id).getMusicPath();
			return s;
		} else {
			// 否则返回空字符串
			return "";
		}
	}
	/**
	 * 音乐播放方法,并且带有暂停方法
	 */
	public void playMusic() {

		// 判断歌曲不能为空
System.out.println("sfajfdsa");
		if (myMediaPlayer != null && Music_infoAdapter.musicList.size() != 0) {
			if (myMediaPlayer.isPlaying()) {
				// 歌曲暂停
				myMediaPlayer.stop();
				// 更换播放按钮背景
				MainActivity.play_ImageButton.setBackgroundResource(R.drawable.play_button);
			} else {
				System.out.println("sfajfdsa2");
		//		myMediaPlayer.start();
				try {       
				     if(myMediaPlayer != null)  
				     {  
				    	 myMediaPlayer.stop();  
				     }      
				     myMediaPlayer.prepare();  
				     myMediaPlayer.start();  
				    
				    } catch (Exception e) {  
				   
				     e.printStackTrace();  
				    }
				// /////////////////////// 初始化歌词配置 /////////////////////// //

				// 更换背景-
				MainActivity.play_ImageButton.setBackgroundResource(R.drawable.pause_button);
				// 启动线程更新SeekBar
				startSeekBarUpdate();
				
				// 截取.mp3字符串
				String a = Music_infoAdapter.musicList.get(playing_id).getMusicName();

				// 切换带动画更新歌曲名
				MainActivity.music_Name.setText(a);
				MainActivity.music_Name.setAnimation(AnimationUtils.loadAnimation(ControlPlay.this, R.anim.translate_z));

				// 带动画更新专辑名
				MainActivity.music_Album.setText(Music_infoAdapter.musicList.get(playing_id).getMusicAlbum());
				MainActivity.music_Album.setAnimation(AnimationUtils.loadAnimation(ControlPlay.this, R.anim.alpha_y));

				// 更新歌曲时间
				MainActivity.time_right.setText(Music_infoAdapter.toTime(Music_infoAdapter.musicList.get(playing_id).getMusicTime()));

			}

			/**
			 * 监听播放是否完成
			 */
			myMediaPlayer.setOnCompletionListener(new OnCompletionListener() {

				@Override
				public void onCompletion(MediaPlayer mp) {
					// TODO Auto-generated method stub

					// 播放完当前歌曲,自动播放下一首
					playNext();
				}
			});

		} else {
			Toast.makeText(ControlPlay.this, "木有在手机里找到歌曲啊...",
					Toast.LENGTH_SHORT).show();
		}
	}

	/**
	 * 播放下一首
	 */
	public void playNext() {

		// 判断歌曲不能为空
		if (Music_infoAdapter.musicList.size() != 0) {
			// 如果到了最后一首则一直播放最后一首
			if (playing_id == Music_infoAdapter.musicList.size() - 1) {
				playing_id = Music_infoAdapter.musicList.size() - 1;
				Toast.makeText(ControlPlay.this, "已经是最后一首啦!",
						Toast.LENGTH_SHORT).show();

				MainActivity.play_ImageButton
						.setBackgroundResource(R.drawable.play_button);
				MainActivity.mNotificationManager.cancel(1);

			} else {
				initMediaSource(initMusicUri(++playing_id));
				playMusic();
			}
		} else {
			Toast.makeText(ControlPlay.this, "木有找到歌曲啊!", Toast.LENGTH_SHORT)
					.show();
		}
	}

	/**
	 * 播放上一首
	 */
	public void playFront() {

		// 判断歌曲不能为空
		if (Music_infoAdapter.musicList.size() != 0) {
			// 如果到了第一首则一直播放第一首
			if (playing_id == 0) {
				playing_id = 0;
				Toast.makeText(ControlPlay.this, "现在就是第一首哦!",
						Toast.LENGTH_SHORT).show();
			} else {
				initMediaSource(initMusicUri(--playing_id));
				playMusic();
			}
		} else {
			Toast.makeText(ControlPlay.this, "木有找到歌曲啊!", Toast.LENGTH_SHORT)
					.show();
		}
	}

	public void startSeekBarUpdate() {
		handler.post(start);
	}

	Runnable start = new Runnable() {

		@Override
		public void run() {
			// TODO Auto-generated method stub

			handler.post(updatesb);
		}

	};

	Runnable updatesb = new Runnable() {

		@Override
		public void run() {
			// 获取SeekBar走动到那的时间
			MainActivity.play_time = myMediaPlayer.getCurrentPosition();

			// 设置填充当前获取的进度
			MainActivity.seekbar.setProgress(MainActivity.play_time);
			// SeekBar的最大值填充歌曲时间
			MainActivity.seekbar.setMax(Music_infoAdapter.musicList.get(playing_id).getMusicTime());

			// 线程延迟1000毫秒启动
			handler.postDelayed(updatesb, 1000);
		}
	};

	

	
}

MainActivity类

public class MainActivity extends Activity {

	private ImageButton left_ImageButton;// 上一曲
	public static ImageButton play_ImageButton;// 播放
	private ImageButton right_ImageButton;// 下一曲
	public static Context mContext;
	private ListView musicListView;// 歌曲列表


	// 绑定SeekBar和各种属性TextView
	public static SeekBar seekbar;
	public static TextView time_left;
	public static TextView time_right;
	public static TextView music_Name;
	public static TextView music_Album;
	public static ImageView music_AlbumArt;

	// 为歌曲时间和播放时间定义静态变量
	public static int song_time = 0;
	public static int play_time = 0;

	public static Music_infoAdapter music_info;
	public static NotificationManager mNotificationManager;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mContext = MainActivity.this;
		musicListView = (ListView) findViewById(R.id.listView1);

		time_left = (TextView) findViewById(R.id.time_tv1);
		time_right = (TextView) findViewById(R.id.time_tv2);
		music_Name = (TextView) findViewById(R.id.music_name);
		music_Album = (TextView) findViewById(R.id.music_album);
		seekbar = (SeekBar) findViewById(R.id.player_seekbar);

		// 创建对象获得系统服务
		mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

		ShowMp3List();
		// 监听播放列表点击事件
		musicListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
				Intent play_1 = new Intent(mContext, ControlPlay.class);
				play_1.putExtra("control", "listClick");
				play_1.putExtra("musicId_1", arg2);
				startService(play_1);
			}
		});

		/**
		 * 监听“上一首”并实现功能
		 */
		left_ImageButton = (ImageButton) findViewById(R.id.ib1);
		left_ImageButton.setOnClickListener(new ImageButton.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent play_left = new Intent(mContext, ControlPlay.class);
				play_left.putExtra("control", "front");
				startService(play_left);
			}
		});

		/**
		 * 监听“下一首”并实现功能
		 */
		right_ImageButton = (ImageButton) findViewById(R.id.ib3);
		right_ImageButton.setOnClickListener(new ImageButton.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent play_right = new Intent(mContext, ControlPlay.class);
				play_right.putExtra("control", "next");
				startService(play_right);
			}
		});

		/**
		 * 播放按钮,实现方法
		 * */
		play_ImageButton = (ImageButton) findViewById(R.id.ib2);
		play_ImageButton.setOnClickListener(new ImageButton.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent play_center = new Intent(mContext, ControlPlay.class);
				play_center.putExtra("control", "play");
				startService(play_center);
			}

		});

		// 判断歌曲不能为空并且后缀为.mp3
		if (music_info.getCount() > 0&& Music_infoAdapter.musicList.get(ControlPlay.playing_id).getMusicName().endsWith(".mp3")) {
			// 显示获取的歌曲时间
			time_right.setText(Music_infoAdapter.toTime(Music_infoAdapter.musicList.get(ControlPlay.playing_id).getMusicTime()));
			// 截取.mp3字符串
			String a = Music_infoAdapter.musicList.get(ControlPlay.playing_id).getMusicName();
			int b = a.indexOf(".mp3");
			String c = a.substring(0, b);
			// 显示获取的歌曲名
			music_Name.setText(c);
			music_Name.setAnimation(AnimationUtils.loadAnimation(mContext,R.anim.translate_z));
		} else {
			System.out.println("请点击列表播放歌曲!");
		}

		/**
		 * 监听拖动SeekBar事件
		 */
		seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

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

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

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				// 判断用户是否触拖SeekBar并且不为空才执行
				if (fromUser && ControlPlay.myMediaPlayer != null) {
					ControlPlay.myMediaPlayer.seekTo(progress);
				}
				time_left.setText(Music_infoAdapter.toTime(progress));
			}
		});

	}


	private void ShowMp3List() {
		// 用游标查找媒体详细信息
		Cursor cursor = this.getContentResolver().query(
				MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
				new String[] { MediaStore.Audio.Media.TITLE,// 标题,游标从0读取
						MediaStore.Audio.Media.DURATION,// 持续时间,1
						MediaStore.Audio.Media.ARTIST,// 艺术家,2
						MediaStore.Audio.Media._ID,// id,3
						MediaStore.Audio.Media.DISPLAY_NAME,// 显示名称,4
						MediaStore.Audio.Media.DATA,// 数据,5
						MediaStore.Audio.Media.ALBUM_ID,// 专辑名称ID,6
						MediaStore.Audio.Media.ALBUM,// 专辑,7
						MediaStore.Audio.Media.SIZE }, null, null,
				MediaStore.Audio.Media.ARTIST);// 大小,8
		/**
		 * 判断游标是否为空,有些地方即使没有音乐也会报异常。而且游标不稳定。稍有不慎就出错了,其次,如果用户没有音乐的话,
		 * 不妨可以告知用户没有音乐让用户添加进去
		 */
		if (cursor != null && cursor.getCount() == 0) {
			Toast.makeText(mContext, "你的手机未找到音乐,请添加音乐", Toast.LENGTH_LONG).show();
			return;
		}
		System.out.println(" cursor size " + cursor.getCount());
		music_info = new Music_infoAdapter(this, cursor);
		new MusicListView().disPlayList(musicListView, this, cursor);

	}

}

歌曲适配类

/**
 * 类说明:歌曲列表适配类
 * @author wangzhihao
 * @version 
 */
public class Music_infoAdapter extends BaseAdapter {

	// 用来装查询到得音乐文件数据
	//游标
	public Cursor mCursor;
	// 歌曲列表信息
	public static List<MusicInfomation> musicList;
	public MusicInfomation mi;

	public Context mContext;
	
	// 音乐信息:1、歌曲名,2、歌手,3、歌曲时间,4、专辑,5、歌曲大小
	public Music_infoAdapter(Context context,Cursor cursor) {
	System.out.println("Music_infoAdapter");
		this.mContext = context;
		this.mCursor = cursor;
		musicList = new ArrayList<MusicInfomation>();
		/** 将游标移到第一位 **/
		mCursor.moveToFirst();
		if (mCursor != null) {
			// 移动到第一个
			mCursor.moveToFirst();
			// 获得歌曲的各种属性
			for (int i = 0; i < mCursor.getCount(); i++) {
				// 过滤mp3文件
				if (mCursor.getString(5).endsWith(".mp3")) {
					mi = new MusicInfomation();
					mi.setMusicName(mCursor.getString(0));//歌曲名称
					mi.setMusicTime(mCursor.getInt(1));//歌曲时间长度
					mi.setMusicAlbum(mCursor.getString(2));//专辑
					mi.setMusicSinger(mCursor.getString(3));//歌手
					mi.setMusicSize(mCursor.getInt(4));//大小
					mi.setMusicPath(mCursor.getString(5));//路径
					mi.set_id(mCursor.getInt(6));//歌曲id
					musicList.add(mi);
				}
				// 移动到下一个
				mCursor.moveToNext();
			}
			System.out.println("musicList size "+ musicList.size());
		}
	}

	@Override
	public int getCount() {
		return musicList.size();
	}

	@Override
	public Object getItem(int arg0) {
		return musicList.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		LayoutInflater li = LayoutInflater.from(mContext);
		View v = li.inflate(R.layout.musiclist_item, null);

		TextView tv1;
		TextView tv2;
		TextView tv3;
		TextView tv4;
		// ImageView iv1;

		tv1 = (TextView) v.findViewById(R.id.tv1);
		tv2 = (TextView) v.findViewById(R.id.tv2);
		tv3 = (TextView) v.findViewById(R.id.tv3);
		tv4 = (TextView) v.findViewById(R.id.tv4);

		tv1.setText(musicList.get(arg0).getMusicName());
		tv2.setText(toTime(musicList.get(arg0).getMusicTime()));
		//tv3.setText(musicList.get(arg0).getMusicPath());
		tv3.setText(musicList.get(arg0).getMusicAlbum());
		tv4.setText(toMB(musicList.get(arg0).getMusicSize()) + "MB");
		return v;
	}

	/**
	 * 时间转化处理
	 */
	public static String toTime(int time) {
		time /= 1000;
		int minute = time / 60;
		int second = time % 60;
		minute %= 60;
		return String.format(" %02d:%02d ", minute, second);
	}

	/**
	 * 文件大小转换,将B转换为MB
	 */
	public String toMB(int size) {
		float a = (float) size / (float) (1024 * 1024);
		String b = Float.toString(a);
		int c = b.indexOf(".");
		String fileSize = "";
		fileSize += b.substring(0, c + 2);
		return fileSize;
	}

	/**
	 * 歌曲专辑图片显示,如果有歌曲图片,才会返回,否则为null,要注意判断
	 * 
	 * @param trackId是音乐的id
	 * @return 返回类型是String 类型的图片地址,也就是uri
	 */
	public String getAlbumArt(int trackId) {



		return "";
	}

	/**
	 * 1、歌曲名 2、歌手 3、歌曲时间 4、专辑(专辑图片,专辑名称,专辑ID[用来获取图片]) 5、歌曲大小
	 */
	public class MusicInfomation {

		private int _id;
		private String musicName;
		private String musicSinger;
		private int musicTime;
		private String musicAlbum;
		private int musicSize;
		private String musicPath;

		public int get_id() {
			return _id;
		}

		public void set_id(int _id) {
			this._id = _id;
		}

		public String getMusicName() {
			return musicName;
		}

		public void setMusicName(String musicName) {
			this.musicName = musicName;
		}

		public String getMusicSinger() {
			return musicSinger;
		}

		public void setMusicSinger(String musicSinger) {
			this.musicSinger = musicSinger;
		}

		public int getMusicTime() {
			return musicTime;
		}

		public void setMusicTime(int musicTime) {
			this.musicTime = musicTime;
		}

		public String getMusicAlbum() {
			return musicAlbum;
		}

		public void setMusicAlbum(String musicAlbum) {
			this.musicAlbum = musicAlbum;
		}

		public int getMusicSize() {
			return musicSize;
		}

		public void setMusicSize(int musicSize) {
			this.musicSize = musicSize;
		}

		public String getMusicPath() {
			return musicPath;
		}

		public void setMusicPath(String musicPath) {
			this.musicPath = musicPath;
		}

	}

}

如需源码可私信

你可能感兴趣的:(Android项目)