在android中的listview中实现音频播放器

在android中的listview中实现音频播放器,第1张

概述我有listview,其中包含带播放按钮和搜索栏的音频文件列表.我使用基本适配器显示了列表视图. 当我单击列表视图的播放按钮时,我想播放音频文件.我成功地实现了这个,但是当我点击列表中的另一个播放按钮时,两个音频文件正在连续播放,它将继续播放所有播放按钮.如何限制媒体播放器在一个位置播放,如果我点击另一个图标,则必须停止旧媒体播放器并开始播放新媒体播放器.谁能说我怎么实现这个呢? 嗨,我正在使用此 我有ListvIEw,其中包含带播放按钮和搜索栏的音频文件列表.我使用基本适配器显示了列表视图.

当我单击列表视图的播放按钮时,我想播放音频文件.我成功地实现了这个,但是当我点击列表中的另一个播放按钮时,两个音频文件正在连续播放,它将继续播放所有播放按钮.如何限制媒体播放器在一个位置播放,如果我点击另一个图标,则必须停止旧媒体播放器并开始播放新媒体播放器.谁能说我怎么实现这个呢?

嗨,我正在使用此代码

public class PlayerList extends Activity {

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",};@OverrIDeprotected 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);}

}

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
解决方法 您可以使用此代码,因为它为我工作:

public class soundTest extends Activity {  private ListVIEw lv1;  private String lv_arr[]={"test 1","test 2","test 3","test 4","test 5"};  @OverrIDe  public voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.main);    lv1=(ListVIEw)findVIEwByID(R.ID.ListVIEw01);    lv1.setAdapter(new ArrayAdapter<String>(this,R.layout.List_item,lv_arr));    lv1.setonItemClickListener(new OnItemClickListener() {      public voID onItemClick(AdapterVIEw<?> parent,VIEw vIEw,int position,long ID) {        if (lv1.getItemAtposition(position)=="test 1") {          MediaPlayer mp = MediaPlayer.create(getApplicationContext(),R.raw.sound1);          mp.start();          mp.setonCompletionListener(new OnCompletionListener() {            public voID onCompletion(MediaPlayer mp) {              mp.release();            }          });        }        if (lv1.getItemAtposition(position)=="test 2") {          MediaPlayer mp = MediaPlayer.create(getApplicationContext(),R.raw.sound2);          mp.start();          mp.setonCompletionListener(new OnCompletionListener() {            public voID onCompletion(MediaPlayer mp) {              mp.release();            }          });        }        //And the rest of the sounds 3,4,5.      }    });  }}

全局变量MediaPlayer需要设置为private static.这引起了我好几次.

如果您发现任何问题,请回复.

总结

以上是内存溢出为你收集整理的在android中的listview中实现音频播放器全部内容,希望文章能够帮你解决在android中的listview中实现音频播放器所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1127560.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存