android 2.2 简易音乐播放器实现(附全部实现代码)

1.Mp3Player.java  文件

package com.jacky.multimedia;

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

import android.app.ListActivity;
dia.MediaPlayer;
dia.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
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 Mp3Player extends ListActivity {
/** Called when the activity is first created. */
/* 定义5个图像按钮 */
private ImageButton    mFrontImageButton    = null;
private ImageButton    mStartImageButton    = null;
private ImageButton    mPauseImageButton    = null;
private ImageButton    mStopImageButton    = 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        = null;
邮购盒

/*定于一个数据播放列表,用来存放从指定文件中搜索到的文件*/
private List<String> mMusicList = new ArrayList<String>();

/* 定义在播放列表中的当前选择项 */
private int currentListItme = 0;

/*定义要播放的文件夹路径*/
private static final String MUSIC_PATH = new String("/mnt/sdcard/");

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
Create(savedInstanceState);
setContentView(R.layout.main);
/* 更新播放列表*/
musicList();
/*初始化多媒体对象*/
mMediaPlayer        = new MediaPlayer();

/*初始化图像按钮*/
mFrontImageButton = (ImageButton) findViewById(R.id.LastImageButton); 
mStopImageButton =  (ImageButton) findViewById(R.id.StopImageButton);
旋转式清堵机看看mStartImageButton = (ImageButton) findViewById(R.id.StartImageButton); 
mPauseImageButton = (ImageButton) findViewById(R.id.PauseImageButton);
mNextImageButton = (ImageButton) findViewById(R.id.NextImageButton); 

/*初始化拖动条和当前进度显示值*/
mSeekBar=(SeekBar)findViewById(R.id.SeekBar01);
curProgressText=(TextView)findViewById(R.id.currentProgress);
curtimeAndTotaltime=(TextView)findViewById(R.id.curtimeandtotaltime);

/*监听停止按钮*/
mStopImageButton.setOnClickListener(new ImageButton.OnClickListener() 
{
@Override
public void onClick(View v)
{
/*判断是否正在播放歌曲*/
if (mMediaPlayer.isPlaying())
{
/*如果在播放歌曲时,按下开始按钮,则重开开始播放*/
set();
}
}
}); 

/*监听开始按钮*/
mStartImageButton.setOnClickListener(new ImageButton.OnClickListener() 
{
@Override
public void onClick(View v)
{  /*播放当前选择歌曲,通过listView列表中onListItemClick方法得到选择的时第几项*/
playMusic(MUSIC_PATH + (currentListItme));
/*开始播放歌曲时,同步进行更新拖动条进度*/
startSeekBarUpdate();
}    
});  

/
*监听暂停按钮*/
mPauseImageButton.setOnClickListener(new ImageButton.OnClickListener() 
{
public void onClick(View view)
{
if (mMediaPlayer.isPlaying())
{
/*如果有播放歌曲,暂停*/管道防爬刺
mMediaPlayer.pause();
}
else 
{
/*如果没有播放歌曲,则开始播放*/
mMediaPlayer.start();
}
}
});

/*监听下一首按钮*/
mNextImageButton.setOnClickListener(new ImageButton.OnClickListener() 
{
@Override
public void onClick(View arg0)
 
nextMusic();
}
});
/*监听上一首按钮*/
mFrontImageButton.setOnClickListener(new ImageButton.OnClickListener() 
{
@Override
public void onClick(View arg0)
{
FrontMusic();
}
});

/*监听拖动条*/
mSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{                                        

public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
/*如果拖动进度发生改变,则显示当前进度值*/
curProgressText.setText("当前进度: "+progress);    
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
curProgressText.setText("拖动中...");
}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
int dest = Progress();    
int mMax = Duration();
int sMax = Max();


mMediaPlayer.seekTo(mMax*dest/sMax);

}
}
);



}

/*按键处理时间,当按下返回按键时的处理方法*/
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ( keyCode ==  KeyEvent.KEYCODE_BACK)
{
mMediaPlayer.stop();
lease();
this.finish();
return true;
}
KeyDown(keyCode, event);
}

/*ListView选择项监听方法,即当前鼠标在列表中选择的第几项*/
protected void onListItemClick(ListView l, View v, int position, long id)
{
隐私保护通话currentListItme = position;
playMusic(MUSIC_PATH + (position));
}



/*更新播放列表*/
public void musicList()
{
/*从指定的路径中读取文件,并与播放列表关联*/
File home = new File(MUSIC_PATH);
/*读取指定类型的文件,在本程序,指定播放类型为mp3*/
if (home.listFiles(new MusicFilter()).length > 0)
{
/*读取文件*/
for (File file : home.listFiles(new MusicFilter()))
{
mMusicList.Name());
}
/
*播放文件与播放列表关联*/
ArrayAdapter<String> musicList = new ArrayAdapter<String>(Mp3Player.this,R.layout.musicitme, mMusicList);
setListAdapter(musicList);
}
}
/*播放指定路径中的音乐*/
private void playMusic(String path)
{
try
{
/* 重置多媒体 */
set();
/*读取mp3文件*/
mMediaPlayer.setDataSource(path);
/
*准备播放*/
mMediaPlayer.prepare();
/*开始播放*/
mMediaPlayer.start();
/*监听播放是否完成*/
mMediaPlayer.setOnCompletionListener(new OnCompletionListener() 
{
public void onCompletion(MediaPlayer arg0)
{
/*播放完当前歌曲,自动播放下一首*/
nextMusic();
}
});
}catch (IOException e){}
}


/*播放下一首*/
private void nextMusic()
{
if (++currentListItme >= mMusicList.size())
{
currentListItme = 0;
}
else
{
playMusic(MUSIC_PATH + (currentListItme));
}
}




/*播放上一首歌曲*/
private void FrontMusic()
{
if (--currentListItme >= 0)
{
currentListItme = 0;
}
else
{
playMusic(MUSIC_PATH + (currentListItme));
}
}

/*更新拖动条进度*/

书立

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

Runnable start=new Runnable(){

@Override
public void run() {
// TODO Auto-generated method stub
mPercentHandler.post(updatesb);
//用一个handler更新SeekBar
}

};

Runnable updatesb =new Runnable(){

@Override
public void run() {
// TODO Auto-generated method stub
int position = CurrentPosition();               
int mMax = Duration();
int sMax = Max();             
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 (dsWith(".mp3"));
}
}



2.main.xls文件

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout 
xmlns:android="schemas.android/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView 
android:id="@id/android:list"  
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
<ImageButton
android:id="@+id/LastImageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"  
android:layout_x="10px"
android:layout_y="70px"
android:src="@drawable/last"  
/>
<ImageButton
android:id="@+id/StopImageButton" 
android:layout_height="wrap_content"  
android:layout_width="wrap_content"
android:layout_x="70px"
android:layout_y="70px"
android:src="@drawable/stop" 
/>  
<ImageButton
android:id="@+id/StartImageButton" 
android:layout_height="wrap_content"  
android:layout_width="wrap_content" 
android:layout_x="130px"
android:layout_y="70px"
android:src="@drawable/start" 
/>
<ImageButton
android:id="@+id/PauseImageButton" 
android:layout_height="wrap_content"  cn1069
android:layout_width="wrap_content" 
android:layout_x="190px"
android:layout_y="70px" 
android:src="@drawable/pause" 
/>  
<ImageButton
android:id="@+id/NextImageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content" 
android:layout_x="250px"
android:layout_y="70px"
android:src="@drawable/next"  
/>
<SeekBar android:id="@+id/SeekBar01" 
android:layout_height="wrap_content" 
android:layout_x="0dp" 
android:layout_y="200dp" 
android:layout_width="fill_parent" 
android:max="100" 
android:progress="0" 
android:secondaryProgress="50" 
android:visibility="visible"></SeekBar>

<TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_y="250dp" android:id="@+id/currentProgress"></TextView>

<TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_y="300dp" android:id="@+id/curtimeandtotaltime"></TextView>
</AbsoluteLayout>

本文发布于:2024-09-22 12:39:20,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/4/277364.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:播放   文件   指定   监听   按钮   开始   播放列表   定义
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议