作者:郭城镣 | 来源:互联网 | 2023-05-19 12:24
今日看书,看到这个播放器,我就写了个例子,感觉还行,这个播放器能播放后缀是。MP3的音乐,这个例子在main.xml设置listView的时候,注意:andr
今日看书,看到这个播放器,我就写了个例子,感觉还行,这个播放器能播放后缀是。MP3的音乐,这个例子在main.xml设置listView的时候,注意:android:id="@+id/android:list"的设置,否则程序会报错,说找不到listview。这个效果还是不错的。可以当做是简单的音乐播放器,可以读取sdcard里面后缀是。MP3的歌曲。有问题可以留言,想要源码可以留言,这个代码比较简单。转载请标明出处:
http://blog.csdn.net/wdaming1986/article/details/6768884
csdn资源下载链接地址:http://download.csdn.net/detail/wdaming1986/3611735
看程序效果图:可以点击每首歌播放,
也可以用下面的按钮: 修改后的程序加了滚动条了
代码说明一切:
一、MainActivity。java类中的代码:
package com.cn.daming;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.GradientDrawable.Orientation;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
public class MainActivity extends ListActivity {
private ImageButton mFrOntImageButton= null;
private ImageButton mStopImageButton = null;
private ImageButton mStartImageButton = null;
private ImageButton mPauseImageButton = null;
private ImageButton mNextImageButton = null;
/*定义进度handler,显示百分比进度*/
Handler mPercentHandler = new Handler();
private SeekBar mSeekBar=null;
private TextView curProgressText=null;
private TextView curtimeAndTotaltime=null;
public MediaPlayer mMediaPlayer;
private List mMusicList = new ArrayList();
private int currentListItem = 0;
private static final String MUSIC_PATH = new String("/sdcard/");
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
drawBackground();
setContentView(R.layout.main);
musicList();
mMediaPlayer = new MediaPlayer();
initmFrontMusic();
initStopMusic();
initStartMusic();
initPauseMusic();
initNextMusic();
initSeekBar();
}
public void drawBackground()
{
GradientDrawable grad = new GradientDrawable(
Orientation.TL_BR,
new int[] {
Color.rgb(0, 0, 127),
Color.rgb(0, 0, 255),
Color.rgb(127, 0, 255),
Color.rgb(127, 127, 255),
Color.rgb(127, 255, 255),
Color.rgb(255, 255, 255)
}
);
this.getWindow().setBackgroundDrawable(grad);
}
public void initmFrontMusic()
{
mFrOntImageButton= (ImageButton)findViewById(R.id.front_button);
mFrontImageButton.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
if(--currentListItem >= 0){
currentListItem = mMusicList.size();
}else{
playMusic(MUSIC_PATH + mMusicList.get(currentListItem));
}
}
});
}
public void initStopMusic()
{
mStopImageButton = (ImageButton)findViewById(R.id.stop_button);
mStopImageButton.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
if(mMediaPlayer.isPlaying())
{
mMediaPlayer.reset();
}
}
});
}
public void initStartMusic()
{
mStartImageButton = (ImageButton)findViewById(R.id.start_button);
mStartImageButton.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
playMusic(MUSIC_PATH + mMusicList.get(currentListItem));
startSeekBarUpdate();
}
});
}
public void initPauseMusic()
{
mPauseImageButton = (ImageButton)findViewById(R.id.pause_button);
mPauseImageButton.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
if(mMediaPlayer.isPlaying()){
mMediaPlayer.pause();
}
else{
mMediaPlayer.start();
}
}
});
}
public void initNextMusic()
{
mNextImageButton = (ImageButton)findViewById(R.id.next_button);
mNextImageButton.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
nextMusic();
}
});
}
public void initSeekBar()
{
/*初始化拖动条和当前进度显示值*/
mSeekBar=(SeekBar)findViewById(R.id.SeekBar01);
curProgressText=(TextView)findViewById(R.id.currentProgress);
curtimeAndTotaltime=(TextView)findViewById(R.id.curtimeandtotaltime);
mSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
/* 如果拖动进度发生改变,则显示当前进度值 */
curProgressText.setText("当前进度: " + progress);
}
public void onStartTrackingTouch(SeekBar arg0) {
curProgressText.setText("拖动中...");
}
public void onStopTrackingTouch(SeekBar arg0) {
int dest = mSeekBar.getProgress();
int mMax = mMediaPlayer.getDuration();
int sMax = mSeekBar.getMax();
mMediaPlayer.seekTo(mMax*dest/sMax);
}
});
}
private void playMusic(String path)
{
try {
mMediaPlayer.reset();
mMediaPlayer.setDataSource(path);
mMediaPlayer.prepare();
mMediaPlayer.start();
mMediaPlayer.setOnCompletionListener(new OnCompletionListener(){
public void onCompletion(MediaPlayer arg0) {
nextMusic();
}
});
}catch (IOException e) {
e.printStackTrace();
}
}
private void nextMusic()
{
if(++currentListItem >= mMusicList.size())
{
currentListItem = 0;
}
else
{
playMusic(MUSIC_PATH + mMusicList.get(currentListItem));
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){
mMediaPlayer.stop();
mMediaPlayer.release();
}
return super.onKeyDown(keyCode, event);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
currentListItem = position;
playMusic(MUSIC_PATH + mMusicList.get(position));
super.onListItemClick(l, v, position, id);
}
//播放列表
public void musicList()
{
File home = new File(MUSIC_PATH);
if(home.listFiles(new MusicFilter()).length > 0)
{
for(File file : home.listFiles(new MusicFilter()))
{
mMusicList.add(file.getName());
}
ArrayAdapter musicList = new ArrayAdapter(MainActivity.this,R.layout.musicitem,mMusicList);
setListAdapter(musicList);
}
}
/*更新拖动条进度*/
public void startSeekBarUpdate() {
mPercentHandler.post(start);
}
Runnable start = new Runnable() {
public void run() {
// 用一个handler更新SeekBar
mPercentHandler.post(updatesb);
}
};
Runnable updatesb =new Runnable(){
public void run() {
int position = mMediaPlayer.getCurrentPosition();
int mMax = mMediaPlayer.getDuration();
int sMax = mSeekBar.getMax();
mSeekBar.setProgress(position * sMax / mMax);
curtimeAndTotaltime.setText("当前播放时间: " + position / 1000 + "秒"
+ "\n歌曲总时间: " + mMax / 1000 + "秒");
// 每秒钟更新一次
mPercentHandler.postDelayed(updatesb, 1000);
}
};
//过滤文件类型
class MusicFilter implements FilenameFilter
{
public boolean accept(File dir, String name) {
//这里还可以设置其他格式的音乐文件
return (name.endsWith(".mp3"));
}
}
}
二、main。xml布局文件的代码:
三、musicitem.xml布局文件的代码:
四、Manifest。xml文件