播放媒体文件时,搜索栏不更新

时间:2014-12-10 09:00:40

标签: android listview custom-adapter

我有listview用于播放audio.in布局我有搜索栏和播放/暂停按钮当我点击播放按钮媒体文件播放但搜索栏没有更新我也用户Runnable和处理程序,但它不起作用。 我在getview方法中的baseadapter中这样做。 请帮我解决这个问题

mAudioViewHolder.play.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
    String yofile = "file://" + filepath;
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    try {

        mediaPlayer.setDataSource(yofile);

    } catch (IllegalArgumentException | SecurityException | IllegalStateException | IOException e) {
        // TODO Auto-generated catch block
        Log.e("Node", "ERRORSS Part 1 is" + e);

        mediaPlayer.setOnCompletionListener(new OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer mp) {
                mAudioViewHolder.isPlaying = false;

                /**
                 * since audio is stop playing, remove its position value
                 * */
                playingAudioPosition = -1;
            }
        });
    }
    try {
        mediaPlayer.prepare();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.e("Node", "ERRORSS is Part 2 " + e);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.e("Node", "ERRORSS is  Part 3 " + e);
    } // might take long! (for buffering, etc)
    mediaPlayer.start();

    mAudioViewHolder.progressBar.setMax(mediaPlayer.getDuration());


    Runnable mRunnable = new Runnable() {

        @Override
        public void run() {
            if (mediaPlayer != null) {
                int mCurrentPosition = mediaPlayer.getCurrentPosition() / 1000;
                mAudioViewHolder.progressBar.setProgress(mCurrentPosition);
                Log.d("Node", "get current position " + mCurrentPosition);
            }

        }
    };
    Log.d("Node", "StartTime Updation " + mediaPlayer.getDuration() + "   " + mediaPlayer.getCurrentPosition());
    mHandler.postDelayed(mRunnable, 1000);
    //mAudioViewHolder.isPlaying = false;

}                               

1 个答案:

答案 0 :(得分:1)

当您每次要更新进度条时需要调用它时,您只能调用一次runnable。

你可以这样做

Runnable mRunnable = new Runnable() {

                                        @Override
                                        public void run() {
                                            if(mediaPlayer != null){
                                                int mCurrentPosition = mediaPlayer.getCurrentPosition() / 1000;
                                                mAudioViewHolder.progressBar.setProgress(mCurrentPosition);
                                                Log.d("Node", "get current position "+mCurrentPosition);
                                                mHandler.postDelayed(this, 1000);
                                            }

                                        }
                                    };
                                    Log.d("Node", "StartTime Updation "+mediaPlayer.getDuration()+"   "+mediaPlayer.getCurrentPosition());
                                    mHandler.postDelayed(mRunnable, 1000);

确保在停止播放时从处理程序中删除runnable。

mHandler.removeCallbacks(mRunnable);