在android中实现listview中的音频播放器
我有一个listview,它包含一个带有播放按钮和seekbar的音频文件列表。我使用基本适配器显示了listview 当我单击listview的播放按钮时,我想播放音频文件。我成功地实现了这一点,但当我单击列表中的另一个播放按钮时,两个音频文件正在连续播放,它将在单击播放按钮后继续播放。如何限制mediaplayer在一个位置播放,如果我单击另一个图标,它必须停止旧的媒体播放器并开始播放新的媒体播放器。有人能告诉我如何实现这一点吗 嗨,我正在使用这个代码 公共类PlayerList扩展活动{在android中实现listview中的音频播放器,android,listview,media-player,Android,Listview,Media Player,我有一个listview,它包含一个带有播放按钮和seekbar的音频文件列表。我使用基本适配器显示了listview 当我单击listview的播放按钮时,我想播放音频文件。我成功地实现了这一点,但当我单击列表中的另一个播放按钮时,两个音频文件正在连续播放,它将在单击播放按钮后继续播放。如何限制mediaplayer在一个位置播放,如果我单击另一个图标,它必须停止旧的媒体播放器并开始播放新的媒体播放器。有人能告诉我如何实现这一点吗 嗨,我正在使用这个代码 公共类PlayerList扩展活动{
private static final String TAG = "log";
ListView listV;
ArrayList<HashMap<String, String>> arrList = new ArrayList<HashMap<String,String>>();;
String[] strArray = { "/mnt/sdcard/Nal.mp3", "/mnt/sdcard/Nal2.mp3",
"/mnt/sdcard/Nal3.mp3", "/mnt/sdcard/sample1.mp3", };
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.homepage);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
listV = (ListView) findViewById(R.id.HomePagelistView1);
for (int i = 0; i < strArray.length; i++) {
HashMap<String, String> hmap = new HashMap<String, String>();
hmap.put("title", "File Name");
hmap.put("description", "File description");
hmap.put("url", strArray[i]);
arrList.add(hmap);
}
FileListAdapter sAdapter = new FileListAdapter(arrList, PlayerList.this);
listV.setAdapter(sAdapter);
}
private static final String TAG=“log”;
ListView-listV;
ArrayList arrList=新的ArrayList();;
字符串[]strArray={/mnt/sdcard/Nal.mp3“,“/mnt/sdcard/Nal2.mp3”,
“/mnt/sdcard/Nal3.mp3”,“/mnt/sdcard/sample1.mp3”,};
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.homepage);
StrictMode.ThreadPolicy policy=新建StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(策略);
listV=(ListView)findViewById(R.id.HomePagelistView1);
对于(int i=0;i
}
下面给出了FileListAdapter文件
public class FileListAdapter extends BaseAdapter implements
OnCompletionListener, OnSeekBarChangeListener {
private MediaPlayer mp;
private Handler mHandler = new Handler();;
private Utilities utils;
SeekBar seekBar;// = (SeekBar) findViewById(R.id.homeList_seekBar1);
String songPath = "";
// ImageView imageVPlay;
private ArrayList<HashMap<String, String>> data;
private LayoutInflater inflater = null;
public FileListAdapter(ArrayList<HashMap<String, String>> data,
Context context) {
this.data = data;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView,
ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.homelist, parent, false);
final ImageView imageVDownload = (ImageView) vi
.findViewById(R.id.homeListimageDownload); // download
final ImageView imageVPlay = (ImageView) vi
.findViewById(R.id.homeListimagePlay); // play
final TextView textVTitle = (TextView) vi
.findViewById(R.id.homeListTextTitle); // email ID
final TextView textVDescription = (TextView) vi
.findViewById(R.id.homeListTextDesc); // email ID
seekBar = (SeekBar) vi.findViewById(R.id.homeList_seekBar1);
textVTitle.setText(data.get(position).get("title"));
textVDescription.setText(data.get(position).get("description"));
// /////////////////////////////////// set image tick and download
String loadFilePath = data.get(position).get("url");
// String loadFileName = data.get(position).get("title");
File ffPath = new File(loadFilePath);
String loadfileNameWithExt = ffPath.getName();
Log.i(TAG, "load file and name path " + " " + loadfileNameWithExt
+ " " + loadFilePath);
imageVPlay.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String selectFilePath = data.get(position).get("url");
String selectFileName = data.get(position).get("title");
Log.i(TAG, "selected file and name path " + selectFileName
+ " " + selectFilePath);
songPath = selectFilePath;
mediaplayerMethod(selectFilePath);
imageVPlay.setImageResource(R.drawable.list_pause);
textVTitle.setVisibility(View.INVISIBLE);
textVDescription.setVisibility(View.INVISIBLE);
seekBar.setVisibility(View.VISIBLE);
}
});
return vi;
}
protected void mediaplayerMethod(String filepath) {
Log.d(TAG, "mediaplayerMethod audio file path " + filepath);
mp = new MediaPlayer();
mp.setOnCompletionListener(FileListAdapter.this); // Important
seekBar.setOnSeekBarChangeListener(FileListAdapter.this);
utils = new Utilities();
playSong(filepath);
}
private void playSong(final String fileptath) {
final Handler handler = new Handler() {
@Override
public void handleMessage(Message message) {
String xmlString = (String) message.obj;
Log.d(TAG, "handleMessage ");
try {
// mp.prepare();
mp.start();
seekBar.setProgress(0);
seekBar.setMax(100);
updateProgressBar();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
};
Thread thread = new Thread() {
@Override
public void run() {
Log.d(TAG, "run ");
try {
mp.reset();
mp.setDataSource(fileptath);
Log.i(TAG, "internal file");
mp.prepare();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Message message = handler.obtainMessage(1, "");
handler.sendMessage(message);
}
};
thread.start();
}
public void updateProgressBar() {
mHandler.postDelayed(mUpdateTimeTask, 100);
}
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
try {
long totalDuration = mp.getDuration();
long currentDuration = mp.getCurrentPosition();
int progress = (int) (utils.getProgressPercentage(
currentDuration, totalDuration));
seekBar.setProgress(progress);
try {
double progVal = (progress / 100.0) * (360.0);
int progInt = (int) Math.ceil(progVal);
} catch (NumberFormatException e) {
Log.e(TAG, "NumberFormatException " + e);
}
// Running this thread after 100 milliseconds
mHandler.postDelayed(this, 100);
} catch (IllegalStateException e) {
Log.e(TAG, "IllegalStateException " + e);
}
}
};
@Override
public void onCompletion(MediaPlayer mp) {
mp.stop();
mp.release();
}
@Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
mHandler.removeCallbacks(mUpdateTimeTask);
}
@Override
public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
mHandler.removeCallbacks(mUpdateTimeTask);
int totalDuration = mp.getDuration();
int currentPosition = utils.progressToTimer(seekBar.getProgress(),
totalDuration);
// forward or backward to certain seconds
mp.seekTo(currentPosition);
updateProgressBar();
}
}// FileListAdapter
公共类FileListAdapter扩展BaseAdapter实现
OnCompletionListener,OnSeekbarchangListener{
私人媒体播放器;
私有处理程序mHandler=新处理程序();;
私人公用事业和公用事业;
SeekBar SeekBar;//=(SeekBar)findViewById(R.id.homeList\u seekBar1);
字符串songPath=“”;
//ImageView imageVPlay;
私有数组列表数据;
专用充气机=空;
公共FileListAdapter(ArrayList数据,
上下文(上下文){
这个数据=数据;
充气器=(充气器)上下文
.getSystemService(上下文布局\充气机\服务);
}
public int getCount(){
返回data.size();
}
公共对象getItem(int位置){
返回位置;
}
公共长getItemId(int位置){
返回位置;
}
公共视图getView(最终整型位置,视图转换视图,
视图组(父级){
视图vi=转换视图;
if(convertView==null)
vi=充气机。充气(R.layout.homelist,父项,false);
最终图像视图图像下载=(图像视图)vi
.findviewbyd(R.id.homeListimageDownload);//下载
最终图像视图图像显示=(图像视图)vi
.findviewbyd(R.id.homeListimagePlay);//play
最终文本视图文本VTITLE=(文本视图)vi
.findViewById(R.id.homeListTextTitle);//电子邮件id
最终文本视图文本描述=(文本视图)vi
.findviewbyd(R.id.homeListTextDesc);//电子邮件id
seekBar=(seekBar)vi.findViewById(R.id.homeList\u seekBar1);
textVTitle.setText(data.get(position.get(“title”));
textVDescription.setText(data.get(position.get)(“description”);
////设置图像勾选和下载
String loadFilePath=data.get(position.get(“url”);
//String loadFileName=data.get(位置).get(“标题”);
File ffPath=新文件(loadFilePath);
字符串loadfileNameWithExt=ffPath.getName();
Log.i(标记“加载文件和名称路径”+“”+loadfileNameWithExt
+“”+loadFilePath);
setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
字符串selectFilePath=data.get(位置).get(“url”);
String selectFileName=data.get(位置).get(“标题”);
Log.i(标记“所选文件和名称路径”+选择文件名
+“”+选择文件路径);
songPath=selectFilePath;
mediaplayerMethod(选择文件路径);
imageVPlay.setImageResource(R.drawable.list\u pause);
textVTitle.setVisibility(View.INVISIBLE);
textVDescription.setVisibility(View.INVISIBLE);
seekBar.setVisibility(View.VISIBLE);
}
});
返回vi;
}
受保护的void mediaplayer方法(字符串文件路径){
Log.d(标记“mediaplayerMethod音频文件路径”+文件路径);
mp=新媒体播放器();
mp.setOnCompletionListener(FileListAdapter.this);//重要
seekBar.setonseekbarchaneglistener(FileListAdapter.this);
utils=新的实用程序();
播放歌曲(文件路径);
}
私有void playSong(最终字符串fileptath){
最终处理程序=新处理程序(){
@凌驾
公共无效handleMessage(消息消息){
字符串xmlString=(String)message.obj;
Log.d(标签“handleMessage”);
试一试{
//mp.prepare();
mp.start();
seekBar.setProgress(0);
seekBar.setMax(100);
updateProgressBar();
}捕获(IllegalArgumentException e){
e、 printStackTrace();
}捕获(非法状态){
e、 printStackTrace();
}
}
};
线程线程=新线程(){
@凌驾
公开募捐{
Log.d(标记“运行”);
试一试{
议员
protected void mediaplayerMethod(String filepath) {
Log.d(TAG, "mediaplayerMethod audio file path " + filepath);
if(mp != null){
mp.release();
}
mp = null;
mp = new MediaPlayer();
mp.setOnCompletionListener(FileListAdapter.this); // Important
seekBar.setOnSeekBarChangeListener(FileListAdapter.this);
utils = new Utilities();
playSong(filepath);
}