我创建了一个ListView
,其中有一个用户录制的录音列表。它可以在一个文件中正常工作,但是当有多个录音时,每次用户点击任何录音时,只会播放最后一个录音。此外,它没有正确显示总时间和当前时间。
这是我的Java文件
package com.example.idictate;
import java.io.File;
import java.io.IOException;
import java.util.List;
import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;
public class Unsent_ListAdapter extends BaseAdapter{
int flag = 0;
Context mContext;
LayoutInflater layoutInflater;
int i=0;
int playCount=0,playTag=(-1);
MediaPlayer durationSetPlayer;
// MediaPlayer mPlayer;
TimerManager timerManager;
File unsent_File;
String unsent_root_sd;
int buttonTagHold=-1;
Runnable runCaller1,runCaller2;
Handler mHandler1 = new Handler();
Handler mHandler2 = new Handler();
private int stateMediaPlayer;
private final int stateMP_NotStarter = 0;
private final int stateMP_Playing = 1;
private final int stateMP_Pausing = 2;
// private int mediaFileLengthInMilliseconds;
List<String> str;
public Unsent_ListAdapter(Context context, int textViewResourceId, List<String> allUnsentFiles) {
mContext=context;
str=allUnsentFiles;
timerManager = new TimerManager();
layoutInflater = LayoutInflater.from(context);
unsent_root_sd = Environment.getExternalStorageDirectory().toString();
durationSetPlayer= new MediaPlayer();
}
static class HoldingClass{
static Button bpaly;
TextView title;
TextView curr_duration;
TextView totl_duration;
CheckBox listCheBox;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return str.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return str.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row=layoutInflater.inflate(R.layout.listitem, null);
// View row=convertView;
final HoldingClass holdingClass=new HoldingClass();
holdingClass.title=(TextView)row.findViewById(R.id.list_title_tv);
holdingClass.curr_duration=(TextView)row.findViewById(R.id.list_curr_duration_tv);
holdingClass.totl_duration=(TextView)row.findViewById(R.id.list_total_duration_tv);
holdingClass.bpaly=(Button)row.findViewById(R.id.list_play_btn);
holdingClass.listCheBox=(CheckBox)row.findViewById(R.id.list_chbox);
// Log.v("********* : ",""+position);
holdingClass.title.setTag(position);
holdingClass.curr_duration.setTag(position);
holdingClass.totl_duration.setTag(position);
holdingClass.bpaly.setTag(position);
holdingClass.listCheBox.setTag(position);
// play Pause code
holdingClass.bpaly.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
switch (stateMediaPlayer) {
case stateMP_NotStarter:
durationSetPlayer.start();
holdingClass.bpaly.setBackgroundResource(R.drawable.pause_button);
stateMediaPlayer = stateMP_Playing;
break;
case stateMP_Playing:
durationSetPlayer.pause();
holdingClass.bpaly.setBackgroundResource(R.drawable.paly_button);
stateMediaPlayer = stateMP_Pausing;
break;
case stateMP_Pausing:
durationSetPlayer.start();
holdingClass.bpaly.setBackgroundResource(R.drawable.pause_button);
stateMediaPlayer = stateMP_Playing;
break;
}
// if(!durationSetPlayer.isPlaying()){
// if(durationSetPlayer != null){
// durationSetPlayer.start();
////// // Changing button image to play button
// holdingClass.bpaly.setBackgroundResource(R.drawable.paly_button);
// }
// }
// else if(durationSetPlayer.isPlaying()){
// if(durationSetPlayer != null){
// durationSetPlayer.pause();
////// // Changing button image to pause button
// holdingClass.bpaly.setBackgroundResource(R.drawable.pause_button);
// }
// }
//
// Log.v("*** "+holdingClass.paly.getTag(),holdingClass.listCheBox.getTag()+" "+holdingClass.title.getTag());
unsent_File = new File( unsent_root_sd + "/iDictate/unsnt/"+holdingClass.title.getText()+".wav") ;
try{
if(var.mPlayer_unsnt.isPlaying()){
mHandler1.removeCallbacksAndMessages(null);
mHandler2.removeCallbacksAndMessages(null);
var.mPlayer_unsnt.stop();
updateView(buttonTagHold);
var.tabHost.getTabWidget().getChildTabViewAt(0).setEnabled(true);
var.tabHost.getTabWidget().getChildTabViewAt(2).setEnabled(true);
var.tabHost.getTabWidget().getChildTabViewAt(3).setEnabled(true);
}
// var.mPlayer_unsnt.reset();
var.mPlayer_unsnt.setDataSource(unsent_File.toString());
var.mPlayer_unsnt.prepare();
var.mPlayer_unsnt.start();
buttonTagHold=(Integer)holdingClass.bpaly.getTag();
var.tabHost.getTabWidget().getChildTabViewAt(0).setEnabled(false);
var.tabHost.getTabWidget().getChildTabViewAt(2).setEnabled(false);
var.tabHost.getTabWidget().getChildTabViewAt(3).setEnabled(false);
}catch(Exception e){
e.printStackTrace();
}
playerButton();
var.mPlayer_unsnt.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer arg0) {
mHandler1.removeCallbacksAndMessages(null);
mHandler2.removeCallbacksAndMessages(null);
if((Integer)holdingClass.curr_duration.getTag()==buttonTagHold) {
holdingClass.curr_duration.setText("00:00:00");
}
buttonTagHold=-1;
// Log.v("*** ","complete track");
var.tabHost.getTabWidget().getChildTabViewAt(0).setEnabled(true);
var.tabHost.getTabWidget().getChildTabViewAt(2).setEnabled(true);
var.tabHost.getTabWidget().getChildTabViewAt(3).setEnabled(true);
}
});
}
public void playerButton(){
final Runnable runCaller1=new Runnable() {
@Override
public void run() {
if((Integer)holdingClass.curr_duration.getTag()==buttonTagHold) {
holdingClass.curr_duration.setText(timerManager.milisecondToTime(var.mPlayer_unsnt.getCurrentPosition()));
mHandler1.postDelayed(this, 100); //her 1 minut k antral se chalege
Log.v("1***","runCaller : "+holdingClass.curr_duration.getTag());
}
}
};
mHandler1.postDelayed(runCaller1, 10);
}
private void updateView(int index){
View v = var.unsent_ListView.getChildAt(index -
var.unsent_ListView.getFirstVisiblePosition());
TextView someText = (TextView) v.findViewById(R.id.list_curr_duration_tv);
someText.setText("00:00:00");
}
});
holdingClass.listCheBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton view, boolean arg1) {
int position = (Integer) view.getTag();
// Log.v("***onChecked "+arg1,""+position);
var.unsent_Checkboxs.set(position, arg1);
}
});
holdingClass.listCheBox.setChecked(var.unsent_Checkboxs.get(position));
holdingClass.title.setText(str.get(position));
if(var.mPlayer_unsnt.isPlaying()){
// mHandler1.removeCallbacksAndMessages(null);
// mHandler2.removeCallbacksAndMessages(null);
if((Integer)holdingClass.curr_duration.getTag()==buttonTagHold){
final Runnable runCaller2=new Runnable() {
@Override
public void run() {
holdingClass.curr_duration.setText(timerManager.milisecondToTime(var.mPlayer_unsnt.getCurrentPosition()));
mHandler2.postDelayed(this, 100); //her 1 minut k antral se chalege
Log.v("2***","runCaller : "+holdingClass.curr_duration.getTag());
}
};
mHandler2.postDelayed(runCaller2, 10);
}
}
// if((Integer)holdingClass.curr_duration.getTag()==10)
// {
// runCaller=new Runnable() {
//
// public void run() {
//
// holdingClass.curr_duration.setText(timerManager.milisecondToTime(mPlayer.getCurrentPosition()));
// handler.postDelayed(this, 2000); //her 1 minut k antral se chalege
// Log.v("*** ","runCaller"+holdingClass.curr_duration.getTag());
// }
// };
// handler.postDelayed(runCaller, 10);
// }
try{
durationSetPlayer.reset();
durationSetPlayer.setDataSource(unsent_root_sd + "/iDictate/unsnt/"+holdingClass.title.getText()+".wav");
durationSetPlayer.prepare();
}catch(Exception e){
e.printStackTrace();
}
holdingClass.totl_duration.setText(timerManager.milisecondToTime(durationSetPlayer.getDuration()));
return row;
}
}
和XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:gravity="center|left" >
<Button
android:id="@+id/list_play_btn"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:background="@drawable/paly_button"
android:focusable="false" />
<TextView
android:id="@+id/list_title_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="5"
android:clickable="false"
android:focusable="false"
android:text="title"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#787878" />
<CheckBox
android:id="@+id/list_chbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:focusable="false"
android:padding="2dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/linearLayout1" >
<TextView
android:id="@+id/list_curr_duration_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:clickable="false"
android:focusable="false"
android:text="00:00:00"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#787878" />
<TextView
android:id="@+id/list_total_duration_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:clickable="false"
android:focusable="false"
android:text="00:00:00"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#787878" />
</LinearLayout>
</RelativeLayout>
答案 0 :(得分:0)
检查getView中的位置
public View getView(int position, View convertView, ViewGroup parent);
如果排名不正确,请使用final int position