1

我需要在单击列表项时创建一个媒体播放器。它已创建。它正在播放、暂停、停止而没有任何干扰。我的问题是搜索栏。只有当我触摸它时它才会移动。在播放开始时音频文件本身应该进步。我仔细观察了代码的每一部分。但无法跟踪问题。提前致谢。
包 com.pdev.activities;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;


public class RecordingActivity2 extends Activity  
 {

    ListView listView;
    VehicleAdapter myadapter;
    ArrayList<String> arr_cars = new ArrayList<String>();
    AlertDialog.Builder alertDialogBuilder;
    MediaPlayer mp;
    Utilities ut;
    Handler handler = new Handler();
    ImageButton play,stop,close;
    TextView cd;
    SeekBar slider;
    Dialog dialog;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_example);

        listView = (ListView)findViewById(R.id.mainList);
        ArrayList<Bitmap> arr_bitmaps1 = new ArrayList<Bitmap>(5);
        arr_bitmaps1.add(BitmapFactory.decodeResource(getResources(),R.drawable.source));
        arr_bitmaps1.add(BitmapFactory.decodeResource(getResources(),R.drawable.source));
        arr_bitmaps1.add(BitmapFactory.decodeResource(getResources(),R.drawable.source));
        arr_bitmaps1.add(BitmapFactory.decodeResource(getResources(),R.drawable.source));
        arr_bitmaps1.add(BitmapFactory.decodeResource(getResources(),R.drawable.source));
        ArrayList<Bitmap> arr_bitmaps2 = new ArrayList<Bitmap>(5);
        arr_bitmaps2.add(BitmapFactory.decodeResource(getResources(),R.drawable.vol));
        arr_bitmaps2.add(BitmapFactory.decodeResource(getResources(),R.drawable.vol));
        arr_bitmaps2.add(BitmapFactory.decodeResource(getResources(),R.drawable.vol));
        arr_bitmaps2.add(BitmapFactory.decodeResource(getResources(),R.drawable.vol));
        arr_bitmaps2.add(BitmapFactory.decodeResource(getResources(),R.drawable.vol));
        ArrayList<Bitmap> arr_bitmaps3 = new ArrayList<Bitmap>(5);
        arr_bitmaps3.add(BitmapFactory.decodeResource(getResources(),R.drawable.timer));
        arr_bitmaps3.add(BitmapFactory.decodeResource(getResources(),R.drawable.timer));
        arr_bitmaps3.add(BitmapFactory.decodeResource(getResources(),R.drawable.timer));
        arr_bitmaps3.add(BitmapFactory.decodeResource(getResources(),R.drawable.timer));
        arr_bitmaps3.add(BitmapFactory.decodeResource(getResources(),R.drawable.timer));
        arr_cars.add("Example1");
        arr_cars.add("Example2");
        arr_cars.add("Example3");
        arr_cars.add("Example4");
        arr_cars.add("Example5");

        myadapter = new VehicleAdapter(RecordingActivity2.this, arr_bitmaps1,arr_bitmaps2,arr_bitmaps3, arr_cars);

        View header = getLayoutInflater().inflate(R.layout.header, null);
        listView.addHeaderView(header);

        listView.setSelector( R.drawable.list_selector);

        listView.setAdapter(myadapter);

        listView.setOnItemLongClickListener(new OnItemLongClickListener() {
         public boolean onItemLongClick(AdapterView<?> av, View v, final int pos, long id) 
            { 
             Object listItem = listView.getItemAtPosition(pos);
             Toast.makeText(RecordingActivity2 .this, "The number of the long clicked item is " + pos, Toast.LENGTH_LONG).show();
             alertDialogBuilder = new AlertDialog.Builder(RecordingActivity2.this);
             alertDialogBuilder.setTitle("Playing");

                AlertDialog alertDialog = alertDialogBuilder.create();
                alertDialog.show();

                return true; //false will also trigger OnItemClick!
            } 
       });
    }


    public class VehicleAdapter extends BaseAdapter
    {

        public String title[];
        public String description[];
        ArrayList<String> arr_calllog_name = new ArrayList<String>();
        public Activity context;
        ArrayList<Bitmap> image1Id,image2Id,image3Id; 

        public LayoutInflater inflater;


        public VehicleAdapter(Activity context, ArrayList<Bitmap> arr_bitmaps1,ArrayList<Bitmap> arrbitmaps2,ArrayList<Bitmap> arrbitmaps3, ArrayList<String> arr_calllog_name) {
            super();

            this.image1Id = arr_bitmaps1;
            this.image2Id = arrbitmaps2;
            this.image3Id = arrbitmaps3;
            this.context = context;
            this.arr_calllog_name = arr_calllog_name;

            this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        public ArrayList<Bitmap> getImage1Id() {
            return image1Id;
        }

        public void setImage1Id(ArrayList<Bitmap> image1Id) {
            this.image1Id = image1Id;
        }

        public ArrayList<Bitmap> getImage2Id() {
            return image2Id;
        }

        public void setImage2Id(ArrayList<Bitmap> image2Id) {
            this.image2Id = image2Id;
        }

        public ArrayList<Bitmap> getImage3Id() {
            return image3Id;
        }

        public void setImage3Id(ArrayList<Bitmap> image3Id) {
            this.image3Id = image3Id;
        }





        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return arr_calllog_name.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        public class ViewHolder
        {
            ImageView source,play,timer;
            TextView txtName;
            Button btn;
            RelativeLayout row;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub

            final ViewHolder holder;
            if(convertView==null)
            {
                holder = new ViewHolder();
                convertView = inflater.inflate(R.layout.list_item, null);

                holder.source = (ImageView) convertView.findViewById(R.id.imageView1);
                holder.play = (ImageView) convertView.findViewById(R.id.imageView2);
                holder.timer = (ImageView) convertView.findViewById(R.id.imageView3);
                holder.txtName = (TextView) convertView.findViewById(R.id.textView1);
                //holder.btn = (Button) convertView.findViewById(R.id.button);
                holder.row = (RelativeLayout) convertView.findViewById(R.id.lineItem);
                convertView.setTag(holder);
            }
            else
            holder=(ViewHolder)convertView.getTag();

            holder.source.setImageBitmap(getImage1Id().get(position));
            holder.play.setImageBitmap(getImage2Id().get(position));
            holder.timer.setImageBitmap(getImage3Id().get(position));
            holder.txtName.setText(arr_calllog_name.get(position));
            holder.source.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "Source",Toast.LENGTH_LONG).show();
                dialog = new Dialog(RecordingActivity2.this);
                dialog.setTitle("Source File");
                dialog.setContentView(R.layout.activity_read);
                ImageButton im = (ImageButton)dialog.findViewById(R.id.closebutton);
                im.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                    dialog.dismiss();                       
                    }
                });
                dialog.show();
                }
            });


            holder.play.setOnClickListener(new View.OnClickListener() { 

                @Override
                public void onClick(View v) {
                Toast.makeText(getApplicationContext(),"Playing",Toast.LENGTH_LONG).show();
                final Dialog dialog = new Dialog(RecordingActivity2.this);
                dialog.setContentView(R.layout.activity_play);
                play = (ImageButton)dialog.findViewById(R.id.play);
                stop = (ImageButton)dialog.findViewById(R.id.stop);
                close = (ImageButton)dialog.findViewById(R.id.close);
                cd =(TextView)dialog.findViewById(R.id.duration);
                slider = (SeekBar)dialog.findViewById(R.id.seeker);
                ut = new Utilities();
                slider.setProgress(0);
                slider.setMax(100);
                slider.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

                    @Override
                    public void onProgressChanged(SeekBar arg0, int progress,
                            boolean arg2) {
                        //Toast.makeText(RecordingActivity2.this, "Seekbar Value : " + progress, Toast.LENGTH_SHORT).show();


                    }

                    @Override
                    public void onStartTrackingTouch(SeekBar seekBar) {
                        //Toast.makeText(RecordingActivity2.this, "Started Tracking Seekbar", Toast.LENGTH_SHORT).show();
                        handler.removeCallbacks(mUpdateTimeTask);
                    }

                    @Override
                    public void onStopTrackingTouch(SeekBar seekBar) {
                        //Toast.makeText(RecordingActivity2.this, "Started Tracking Seekbar", Toast.LENGTH_SHORT).show();
                        handler.removeCallbacks(mUpdateTimeTask);
                        int totalDuration = mp.getDuration();
                        int currentPosition = ut.progressToTimer(seekBar.getProgress(),
                                totalDuration);

                        // forward or backward to certain seconds
                        mp.seekTo(currentPosition);

                        // update timer progress again
                        updateProgressBar();
                    }





                    });

                if(holder.txtName.getText().equals("Example1")){
                    mp = MediaPlayer.create(getApplicationContext(),R.raw.aperture);    
                    Log.d("Check1",holder.txtName.getText().toString());
                       dialog.setTitle("Example1");
                  }
                else if(holder.txtName.getText().equals("Example2")){
                    mp = MediaPlayer.create(getApplicationContext(), R.raw.coffee); 
                    Log.d("Check2",holder.txtName.getText().toString());
                    dialog.setTitle("Example2");    
                    }
                else if(holder.txtName.getText().equals("Example3")){
                    mp = MediaPlayer.create(getApplicationContext(), R.raw.harperactive);
                    Log.d("Check3",holder.txtName.getText().toString());
                    dialog.setTitle("Example3");    
                    }
                else if(holder.txtName.getText().equals("Example4")){
                        mp = MediaPlayer.create(getApplicationContext(), R.raw.nightmares);
                        Log.d("Check4",holder.txtName.getText().toString());
                        dialog.setTitle("Example4");    
                    }
                else{
                        mp = MediaPlayer.create(getApplicationContext(), R.raw.pianomeditation);    
                        Log.d("Check5",holder.txtName.getText().toString());
                        dialog.setTitle("Example5");    
                }

                play.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        if (mp.isPlaying()) {
                            if (mp != null) {
                                mp.pause();
                                // Changing button image to play button
                                play.setImageResource(R.drawable.img_btn_play);

                            }
                        } else {
                            // Resume song
                            if (mp != null) {
                                mp.start();
                                // Changing button image to pause button
                                play.setImageResource(R.drawable.img_btn_pause);

                            }
                        }
                    }
                }
                );

                stop.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                      dialog.dismiss();
                      mp.stop();
                    }
                });
                close.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                        mp.stop();

                    }
                 });

                dialog.show();
                }
                public void updateProgressBar() {
                    handler.postDelayed(mUpdateTimeTask, 100);
                }
                private Runnable mUpdateTimeTask = new Runnable() {
                    public void run() {
                        long totalDuration = mp.getDuration();
                        long currentDuration = mp.getCurrentPosition();

                        // Displaying time completed playing
                        cd.setText(""
                                + ut.milliSecondsToTimer(currentDuration));

                        // Updating progress bar
                        int progress = (int) (ut.getProgressPercentage(currentDuration,
                                totalDuration));
                        // Log.d("Progress", ""+progress);
                        slider.setProgress(progress);

                        // Running this thread after 100 milliseconds
                        handler.postDelayed(this, 100);
                    }
                };
            });


            holder.timer.setOnClickListener(new View.OnClickListener() {

                    @Override
                        public void onClick(View v) {
                            Toast.makeText(getApplicationContext(), "Timer",Toast.LENGTH_LONG).show();
                        }
                });

            holder.row.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Toast.makeText(getApplicationContext(), "You have Selected: "+ holder.txtName.getText(), Toast.LENGTH_SHORT).show();
                }
            });

            return convertView;



        }

    }}
4

3 回答 3

3

我找到了答案。

package com.pdev.activities;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;


public class RecordingActivity2 extends Activity  
 {

    ListView listView;
    VehicleAdapter myadapter;
    ArrayList<String> arr_cars = new ArrayList<String>();
    AlertDialog.Builder alertDialogBuilder;
    MediaPlayer mp;
    Utilities ut;
    Handler handler = new Handler();
    ImageButton play,stop,close;
    TextView cd;
    SeekBar slider;
    Dialog dialog;
    int currentPosition = 0;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_example);

        listView = (ListView)findViewById(R.id.mainList);
        ArrayList<Bitmap> arr_bitmaps1 = new ArrayList<Bitmap>(5);
        arr_bitmaps1.add(BitmapFactory.decodeResource(getResources(),R.drawable.source));
        arr_bitmaps1.add(BitmapFactory.decodeResource(getResources(),R.drawable.source));
        arr_bitmaps1.add(BitmapFactory.decodeResource(getResources(),R.drawable.source));
        arr_bitmaps1.add(BitmapFactory.decodeResource(getResources(),R.drawable.source));
        arr_bitmaps1.add(BitmapFactory.decodeResource(getResources(),R.drawable.source));
        ArrayList<Bitmap> arr_bitmaps2 = new ArrayList<Bitmap>(5);
        arr_bitmaps2.add(BitmapFactory.decodeResource(getResources(),R.drawable.vol));
        arr_bitmaps2.add(BitmapFactory.decodeResource(getResources(),R.drawable.vol));
        arr_bitmaps2.add(BitmapFactory.decodeResource(getResources(),R.drawable.vol));
        arr_bitmaps2.add(BitmapFactory.decodeResource(getResources(),R.drawable.vol));
        arr_bitmaps2.add(BitmapFactory.decodeResource(getResources(),R.drawable.vol));
        ArrayList<Bitmap> arr_bitmaps3 = new ArrayList<Bitmap>(5);
        arr_bitmaps3.add(BitmapFactory.decodeResource(getResources(),R.drawable.timer));
        arr_bitmaps3.add(BitmapFactory.decodeResource(getResources(),R.drawable.timer));
        arr_bitmaps3.add(BitmapFactory.decodeResource(getResources(),R.drawable.timer));
        arr_bitmaps3.add(BitmapFactory.decodeResource(getResources(),R.drawable.timer));
        arr_bitmaps3.add(BitmapFactory.decodeResource(getResources(),R.drawable.timer));
        arr_cars.add("Example1");
        arr_cars.add("Example2");
        arr_cars.add("Example3");
        arr_cars.add("Example4");
        arr_cars.add("Example5");

        myadapter = new VehicleAdapter(RecordingActivity2.this, arr_bitmaps1,arr_bitmaps2,arr_bitmaps3, arr_cars);

        View header = getLayoutInflater().inflate(R.layout.header, null);
        listView.addHeaderView(header);

        listView.setSelector( R.drawable.list_selector);

        listView.setAdapter(myadapter);

        listView.setOnItemLongClickListener(new OnItemLongClickListener() {
         public boolean onItemLongClick(AdapterView<?> av, View v, final int pos, long id) 
            { 
             Object listItem = listView.getItemAtPosition(pos);
             Toast.makeText(RecordingActivity2 .this, "The number of the long clicked item is " + pos, Toast.LENGTH_LONG).show();
             alertDialogBuilder = new AlertDialog.Builder(RecordingActivity2.this);
             alertDialogBuilder.setTitle("Playing");

                AlertDialog alertDialog = alertDialogBuilder.create();
                alertDialog.show();

                return true; //false will also trigger OnItemClick!
            } 
       });
    }


    public class VehicleAdapter extends BaseAdapter
    {

        public String title[];
        public String description[];
        ArrayList<String> arr_calllog_name = new ArrayList<String>();
        public Activity context;
        ArrayList<Bitmap> image1Id,image2Id,image3Id; 

        public LayoutInflater inflater;


        public VehicleAdapter(Activity context, ArrayList<Bitmap> arr_bitmaps1,ArrayList<Bitmap> arrbitmaps2,ArrayList<Bitmap> arrbitmaps3, ArrayList<String> arr_calllog_name) {
            super();

            this.image1Id = arr_bitmaps1;
            this.image2Id = arrbitmaps2;
            this.image3Id = arrbitmaps3;
            this.context = context;
            this.arr_calllog_name = arr_calllog_name;

            this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        public ArrayList<Bitmap> getImage1Id() {
            return image1Id;
        }

        public void setImage1Id(ArrayList<Bitmap> image1Id) {
            this.image1Id = image1Id;
        }

        public ArrayList<Bitmap> getImage2Id() {
            return image2Id;
        }

        public void setImage2Id(ArrayList<Bitmap> image2Id) {
            this.image2Id = image2Id;
        }

        public ArrayList<Bitmap> getImage3Id() {
            return image3Id;
        }

        public void setImage3Id(ArrayList<Bitmap> image3Id) {
            this.image3Id = image3Id;
        }





        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return arr_calllog_name.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        public class ViewHolder
        {
            ImageView source,play,timer;
            TextView txtName;
            Button btn;
            RelativeLayout row;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub

            final ViewHolder holder;
            if(convertView==null)
            {
                holder = new ViewHolder();
                convertView = inflater.inflate(R.layout.list_item, null);

                holder.source = (ImageView) convertView.findViewById(R.id.imageView1);
                holder.play = (ImageView) convertView.findViewById(R.id.imageView2);
                holder.timer = (ImageView) convertView.findViewById(R.id.imageView3);
                holder.txtName = (TextView) convertView.findViewById(R.id.textView1);
                //holder.btn = (Button) convertView.findViewById(R.id.button);
                holder.row = (RelativeLayout) convertView.findViewById(R.id.lineItem);
                convertView.setTag(holder);
            }
            else
            holder=(ViewHolder)convertView.getTag();

            holder.source.setImageBitmap(getImage1Id().get(position));
            holder.play.setImageBitmap(getImage2Id().get(position));
            holder.timer.setImageBitmap(getImage3Id().get(position));
            holder.txtName.setText(arr_calllog_name.get(position));
            holder.source.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "Source",Toast.LENGTH_LONG).show();
                dialog = new Dialog(RecordingActivity2.this);
                dialog.setTitle("Source File");
                dialog.setContentView(R.layout.activity_read);
                ImageButton im = (ImageButton)dialog.findViewById(R.id.closebutton);
                im.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                    dialog.dismiss();                       
                    }
                });
                dialog.show();
                }
            });


            holder.play.setOnClickListener(new View.OnClickListener() { 

                @Override
                public void onClick(View v) {
                Toast.makeText(getApplicationContext(),"Playing",Toast.LENGTH_LONG).show();
                final Dialog dialog = new Dialog(RecordingActivity2.this);
                dialog.setContentView(R.layout.activity_play);
                play = (ImageButton)dialog.findViewById(R.id.play);
                stop = (ImageButton)dialog.findViewById(R.id.stop);
                close = (ImageButton)dialog.findViewById(R.id.close);
                cd =(TextView)dialog.findViewById(R.id.duration);
                slider = (SeekBar)dialog.findViewById(R.id.seeker);
                ut = new Utilities();
                slider.setProgress(0);
                slider.setMax(100); 
                slider.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

                    @Override
                    public void onProgressChanged(SeekBar arg0, int progress,
                            boolean arg2) {
                        //Toast.makeText(RecordingActivity2.this, "Seekbar Value : " + progress, Toast.LENGTH_SHORT).show();


                    }

                    @Override
                    public void onStartTrackingTouch(SeekBar seekBar) {
                        //Toast.makeText(RecordingActivity2.this, "Started Tracking Seekbar", Toast.LENGTH_SHORT).show();
                        handler.removeCallbacks(mUpdateTimeTask);
                    }

                    @Override
                    public void onStopTrackingTouch(SeekBar seekBar) {
                        //Toast.makeText(RecordingActivity2.this, "Started Tracking Seekbar", Toast.LENGTH_SHORT).show();
                        handler.removeCallbacks(mUpdateTimeTask);
                        int totalDuration = mp.getDuration();
                        currentPosition = ut.progressToTimer(seekBar.getProgress(),
                                totalDuration);

                        // forward or backward to certain seconds
                        mp.seekTo(currentPosition);

                        // update timer progress again
                        updateProgressBar();
                    }





                    });






                if(holder.txtName.getText().equals("Example1")){
                    mp = MediaPlayer.create(getApplicationContext(),R.raw.aperture);    
                    Log.d("Check1",holder.txtName.getText().toString());
                       dialog.setTitle("Example1");
                  }
                else if(holder.txtName.getText().equals("Example2")){
                    mp = MediaPlayer.create(getApplicationContext(), R.raw.coffee); 
                    Log.d("Check2",holder.txtName.getText().toString());
                    dialog.setTitle("Example2");    
                    }
                else if(holder.txtName.getText().equals("Example3")){
                    mp = MediaPlayer.create(getApplicationContext(), R.raw.harperactive);
                    Log.d("Check3",holder.txtName.getText().toString());
                    dialog.setTitle("Example3");    
                    }
                else if(holder.txtName.getText().equals("Example4")){
                        mp = MediaPlayer.create(getApplicationContext(), R.raw.nightmares);
                        Log.d("Check4",holder.txtName.getText().toString());
                        dialog.setTitle("Example4");    
                    }
                else{
                        mp = MediaPlayer.create(getApplicationContext(), R.raw.pianomeditation);    
                        Log.d("Check5",holder.txtName.getText().toString());
                        dialog.setTitle("Example5");    
                }

                play.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        if (mp.isPlaying()) {
                            if (mp != null) {
                                mp.pause();
                                // Changing button image to play button
                                play.setImageResource(R.drawable.img_btn_play);
                                Log.d("CurrentPosition1",""+currentPosition);
                                //slider.setProgress(mp.getCurrentPosition());
                                updateProgressBar();

                            }
                        } else {
                            // Resume song
                            if (mp != null) {
                                mp.start();
                                // Changing button image to pause button
                                //slider.setProgress(currentPosition+1);
                                Log.d("CurrentPosition2",""+currentPosition);
                                play.setImageResource(R.drawable.img_btn_pause);
                                //slider.setProgress(mp.getCurrentPosition()+1);
                                updateProgressBar();
                            }
                        }
                    }
                }
                );

                stop.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                      dialog.dismiss();
                      mp.stop();
                    }
                });
                close.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                        mp.stop();

                    }
                 });

                dialog.show();
                }

                public void updateProgressBar() {
                    handler.postDelayed(mUpdateTimeTask, 100);
                }
                private Runnable mUpdateTimeTask = new Runnable() {
                    public void run() {
                        long totalDuration = mp.getDuration();
                        long currentDuration = mp.getCurrentPosition();

                        // Displaying time completed playing
                        cd.setText(""
                                + ut.milliSecondsToTimer(currentDuration));

                        // Updating progress bar
                        int progress = (int) (ut.getProgressPercentage(currentDuration,
                                totalDuration));
                        // Log.d("Progress", ""+progress);
                        slider.setProgress(progress);

                        // Running this thread after 100 milliseconds
                        handler.postDelayed(this, 100);
                    }
                };
            });


            holder.timer.setOnClickListener(new View.OnClickListener() {

                    @Override
                        public void onClick(View v) {
                            Toast.makeText(getApplicationContext(), "Timer",Toast.LENGTH_LONG).show();
                        }
                });

            holder.row.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Toast.makeText(getApplicationContext(), "You have Selected: "+ holder.txtName.getText(), Toast.LENGTH_SHORT).show();
                }
            });

            return convertView;



        }

    }}
于 2013-07-08T11:32:29.763 回答
0

不要对滚动条做任何事情,只需在 seekbar 触摸侦听器上返回 true 并在单击您的 listview 项目时启动 mediaplyer 并附加以下侦听器。

mediaPlayer.setOnPreparedListener(new OnPreparedListener() 
    {
      @Override
      public void onPrepared(final MediaPlayer mp) 
      {
          seekBar.setMax(mediaPlayer.getDuration());



           thread=  new Thread(new Runnable() {

                     @Override
                     public void run() {
                         if(!Thread.interrupted()){
                             try{
                             while(mediaPlayer!=null && mediaPlayer.isPlaying())
                             { 

                                 seekBar.setProgress(mediaPlayer.getCurrentPosition());

                                 int millis = mediaPlayer.getCurrentPosition();

                                  try {
                                      Thread.sleep(100);
                                  } 
                                  catch (InterruptedException e) {
                                     e.printStackTrace();
                                  }
                             }
                             }
                             catch (IllegalStateException e) {
                                // TODO: handle exception
                            }
                     }
                     }
             });
           thread.start();

      }
  });
于 2013-07-08T07:59:21.300 回答
0

尝试先设置最大值,然后设置进度。

于 2013-07-08T11:25:12.620 回答