【菜鸟学安卓】- Mp3播放器

【菜鸟学安卓】- Mp3播放器_第1张图片


做一个简单的Mp3播放器的主要内容包括:


1.获取手机中的mp3文件

在手机中有一个内容提供者供我们使用

主要就是下面的语句来获取

<pre name="code" class="java">Cursor cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER); 

 
 
//			//歌曲ID:MediaStore.Audio.Media._ID
//			int id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID));  
//			//歌曲的名称 :MediaStore.Audio.Media.TITLE
//			String tilte = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));  
//			//歌曲的专辑名:MediaStore.Audio.Media.ALBUM
//			String album = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));  
//			//歌曲的歌手名: MediaStore.Audio.Media.ARTIST 
//			String artist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));  
//			//歌曲文件的路径 :MediaStore.Audio.Media.DATA
//			String url = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));    
//			//歌曲的总播放时长 :MediaStore.Audio.Media.DURATION
//			int duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));    
//			//歌曲文件的大小 :MediaStore.Audio.Media.SIZE
//			int size =(int)cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE));

2.播放获取出来的mp3文件

MediaPlayer mediaPlayer = new MediaPlayer();//实例化一个媒体播放器

mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);//设置媒体播放器的音乐类型

mediaPlayer.setDataSource(path);//设置播放文件的路径

mediaPlayer.setLooping(true);

mediaPlayer.prepareAsync();//播放准备

mediaPlayer.start();//开始播放


主文件代码如下:

由于博主只是学习下这两部分的内容,所以其中有些地方是有问题的,看主要内容就好。

package com.mp3palyer;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;



public class MainActivity extends Activity implements OnClickListener {
	
	
	private MediaPlayer mediaPlayer;
	
	private LinearLayout toControlView;
	private LinearLayout playBtn;
	private LinearLayout favoriteBtn;
	private LinearLayout nextBtn;
	
	private ImageView playBtnImage;
	
	private String currentPath;
	
	private ListView mp3ListView;
	
	private List<Mp3Info> mp3InfoList = new ArrayList<Mp3Info>();
	
	
	private int currentPosition = 0;
	
	private int playMode = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		findMp3();
		
		if(mp3InfoList != null)
			currentPath = mp3InfoList.get(0).getUrl();
		
		findViewId();
		
		
		
	}
	
	
	
	private void findMp3() {
		Cursor cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER); 
		System.out.println("一共有  "+cursor.getCount());
		while(cursor.moveToNext()){
			String url = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
			if(url.endsWith(".mp3")){
				Mp3Info mp3Info = new Mp3Info();

				int id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID));  
				int duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));    
				int size =(int)cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE));
				String tilte = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));  
				String album = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));  
				String artist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));  
				
				mp3Info.setId(id);
				mp3Info.setDuration(duration);
				mp3Info.setSize(size);
				mp3Info.setTilte(tilte);
				mp3Info.setArtist(artist);
				mp3Info.setAlbum(album);
				mp3Info.setUrl(url);
				
				mp3InfoList.add(mp3Info);
			}
		}
		cursor.close();
	}



	private void findViewId(){
		toControlView = (LinearLayout)findViewById(R.id.toControlView);
		playBtn = (LinearLayout)findViewById(R.id.playBtn);
		favoriteBtn = (LinearLayout)findViewById(R.id.favoriteBtn);
		nextBtn = (LinearLayout)findViewById(R.id.nextBtn);
		
		playBtnImage = (ImageView)findViewById(R.id.playBtnImage);
		
		mp3ListView = (ListView)findViewById(R.id.myListView);
		Mp3Adapter mp3ListAdapter = new Mp3Adapter(mp3InfoList);
		mp3ListView.setAdapter(mp3ListAdapter);
		
		toControlView.setOnClickListener(this);
		playBtn.setOnClickListener(this);
		favoriteBtn.setOnClickListener(this);
		nextBtn.setOnClickListener(this);
		
		mp3ListView.setOnItemClickListener(new ListView.OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				String path = mp3InfoList.get(position).getUrl();//获取路径
				play(path);//播放音乐
			}
			
		});
		
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.toControlView:
			startActivity(new Intent(this,PlayActivity.class));
			break;
		case R.id.favoriteBtn:
			break;
		case R.id.playBtn:
			if(playMode == 0){
				playMode = 1;
				playBtnImage.setImageResource(R.drawable.ao9);
				continuePlay(currentPath);
			}else{
				playMode = 0;
				playBtnImage.setImageResource(R.drawable.aoa);
				pause();
			}
			
			break;
		case R.id.nextBtn:
			playNext(mp3InfoList.get(currentPosition+1).getUrl());
			break;
		default:
		}
		
	}
	
	//播放一个新的
	private void playNew(String path){
		try {
			File file = new File(path);
			if(file.exists()&&file.length()>0){
				mediaPlayer = new MediaPlayer();
				mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
				mediaPlayer.setDataSource(path);
				mediaPlayer.setLooping(true);
				mediaPlayer.prepareAsync();
				mediaPlayer.setOnPreparedListener(new OnPreparedListener(){
					@Override
					public void onPrepared(MediaPlayer mp) {
						mediaPlayer.start();
					}
				});
				mediaPlayer.setOnCompletionListener(new OnCompletionListener(){
					@Override
					public void onCompletion(MediaPlayer mp) {
						//mediaPlayer.
					}
				});
				mediaPlayer.setOnErrorListener(new OnErrorListener(){
					@Override
					public boolean onError(MediaPlayer mp, int what, int extra) {
						
						return false;
					}
					
				});
			
			}else{
				Toast.makeText(this, "文件不存在!", 0).show();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	//播放默认的
	private void play(String path){
		//先停止当前的播放
		if(mediaPlayer != null){
			stop();
		}
		//播放新的
		playNew(path);
	}
	
	//播放下一首
	private void playNext(String path){
		play(path);
	}
	
	//暂停
	private void pause(){
		if(mediaPlayer != null && mediaPlayer.isPlaying()){
			mediaPlayer.pause();
		}
	}
	
	//停止
	private void stop(){
		if(mediaPlayer != null&& mediaPlayer.isPlaying()){
			mediaPlayer.stop();
			mediaPlayer.release();
			mediaPlayer = null;
		}
	}
	
	//继续播放
	private void continuePlay(String path){
		if(mediaPlayer != null){
			mediaPlayer.start();
		}else{
			playNew(path);
		}
	}
	
	
	public class Mp3Adapter extends BaseAdapter{
		
		private List<Mp3Info> mp3List;
		
		public Mp3Adapter(List<Mp3Info> mp3List){
			this.mp3List = mp3List;
		}

		@Override
		public int getCount() {
			if(mp3List != null)
				return mp3List.size();
			return 0;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

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

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view = null;
			if(convertView != null){
				view = convertView;
			}else{
				view = View.inflate(MainActivity.this, R.layout.item_list, null);
			}
			ImageView musicImage = (ImageView)view.findViewById(R.id.musicImage);
			TextView musicName= (TextView)view.findViewById(R.id.musicName);
			TextView musicSinger= (TextView)view.findViewById(R.id.musicSinger);
			musicName.setText(mp3List.get(position).getTilte());
			musicSinger.setText(mp3List.get(position).getArtist());
			return view;
		}
		
	}
}


你可能感兴趣的:(mediaplayer,MediaStore)