本文章将一步一步实现Android 音乐播放器,利用Android强大的API,可以很方便的制作自己风格的音乐播放器。利用本文介绍的例子,实现音乐播放器的关键代码,如果再加上自己的UI设计定制方案,就可以做出自己想要的播放器了。
首先我们写一个xml的界面布局,这里用一个listview来展示音乐列表,并提供简单的音乐播放控制按钮和一个可以拖拽的进度条来控制音量的大小。
然后编写我们的Activity主程序,首先初始化一个mediaPlayer,并设置各个按钮的初始化
mediaPlayer = new MediaPlayer();
Button next = (Button) findViewById(R.id.next);
pause = (Button) findViewById(R.id.pause);
Button pre = (Button) findViewById(R.id.pre);
Button stop = (Button) findViewById(R.id.stop);
audioList();
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
nextMusic();
}
});
stop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(mediaPlayer.isPlaying()){
mediaPlayer.stop();
}
pause.setText("开始");
}
});
pause.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(mediaPlayer.isPlaying()){
mediaPlayer.pause();
((Button)v).setText("继续");
}else{
mediaPlayer.start();
((Button)v).setText("暂停");
}
}
});
next.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
nextMusic();
}
});
pre.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
preMusic();
}
});
private void audioList() {
getFiles(Environment.getExternalStorageDirectory().getPath());
ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1,audioList);
ListView listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView> viewList, View view,
int position,long id) {
currentItem = position;
String path = map.get(audioList.get(position));
playMusic(path);
}
});
}
private void getFiles(String path) {
File files = new File(path);
File[] file = files.listFiles();
try {
for (File f : file) {
if(f.isDirectory()){
getFiles(f.getAbsolutePath());
}else{
if(f.length()>512)
if(isAudioFile(f.getPath())){
audioList.add(f.getPath().substring(f.getPath().lastIndexOf("/")+1));
map.put(f.getPath().substring(f.getPath().lastIndexOf("/")+1), f.getPath());
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
判断是否是音乐文件,可以通过文件的扩展名,此处我们暂时只搜索3种类型的音乐
private static String[] imageFormatSet = new String[]{"ape", "mp3", "wav" };
private boolean isAudioFile(String path) {
// 获取扩展名
String FileEnd = path.substring(path.lastIndexOf(".") + 1,
path.length()).toLowerCase();
for(String format:imageFormatSet){
if(FileEnd.equals(format)){
return true;
}
}
return false;
}
protected void playMusic(String path) {
try {
if(mediaPlayer.isPlaying()){
mediaPlayer.stop();
}
mediaPlayer.reset();
mediaPlayer.setDataSource(path);
mediaPlayer.prepare();
mediaPlayer.start();
pause.setText("暂停");
pause.setEnabled(true);
} catch (Exception e) {
e.printStackTrace();
}
}
protected void nextMusic() {
if(++currentItem >= audioList.size()){
currentItem = 0;
}
String path = map.get(audioList.get(currentItem));
playMusic(path);
}
protected void preMusic() {
if(--currentItem >= 0){
if(currentItem >= audioList.size()){
currentItem = 0;
}
}else{
currentItem = audioList.size() - 1;
}
String path = map.get(audioList.get(currentItem));
playMusic(path);
}
最后,还有音乐播放时拖动进度条改变音量大小的实现。
final AudioManager am = (AudioManager) MainActivity.this.getSystemService(Context.AUDIO_SERVICE);
MainActivity.this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
SeekBar seekBar = (SeekBar) findViewById(R.id.seekBar);
seekBar.setMax(am.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
int progress = am.getStreamVolume(AudioManager.STREAM_MUSIC);
seekBar.setProgress(progress);
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
am.setStreamVolume(AudioManager.STREAM_MUSIC, progress, AudioManager.FLAG_PLAY_SOUND);
}
});
是不是很容易,也很振奋人心。
动手制作你自己的播放器吧。