0

音频完成后,我的 ProgressBar 不会重置。它在一些音频后重置,但其他人不确定为什么。还想在音频完成时更改暂停图标以播放,如果有人可以帮忙的话,那就太好了!

继承人代码:

 public class player1 extends Activity implements Runnable {


private  MediaPlayer mp;
private ProgressBar progressBar;
private ImageButton pauseicon;
private final int NUM_SOUND_FILES = 3;  //*****REPLACE THIS WITH THE ACTUAL NUMBER OF SOUND FILES YOU HAVE*****
private int mfile[] = new int[NUM_SOUND_FILES];
private Random rnd = new Random();


   @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.player_1);
        pauseicon = (ImageButton) findViewById(R.id.pauseicon);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        getActionBar().setDisplayHomeAsUpEnabled(true);



        mfile[0] = R.raw.sound01;  //****REPLACE THESE WITH THE PROPER NAMES OF YOUR SOUND FILES
        mfile[1] = R.raw.sound02;  //PLACE THE SOUND FILES IN THE /res/raw/ FOLDER IN YOUR PROJECT*****
        mfile[2] = R.raw.sound03;
        // Listeners
        /**
         * Play button click event
         * plays a song and changes button to pause image
         * pauses a song and changes button to play image
         * */


        try{
             mp = MediaPlayer.create(player1.this, mfile[rnd.nextInt(NUM_SOUND_FILES)]);
             mp.seekTo(0);
             mp.start(); ;         
             progressBar.setVisibility(ProgressBar.VISIBLE);
             progressBar.setProgress(0);
             progressBar.setMax(mp.getDuration());
             new Thread(this).start();

         } catch (IllegalArgumentException e) {
             e.printStackTrace();
         } catch (IllegalStateException e) {
             e.printStackTrace();
         }



        pauseicon.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                 if (v.getId() == R.id.pauseicon)
        if(mp.isPlaying()){
            mp.pause();
          ImageButton pauseicon =(ImageButton) findViewById(R.id.pauseicon);

          pauseicon.setImageResource(R.drawable.playicon);
        } else {
            mp.start();

                 ImageButton pauseicon =(ImageButton) findViewById(R.id.pauseicon);

                 pauseicon.setImageResource(R.drawable.pauseicon);


            }}});

   }

    public void run() {
        int currentPosition= 0;
        int total = mp.getDuration();
        while (mp!=null && currentPosition<total) {
            try {
                Thread.sleep(1000);
                currentPosition= mp.getCurrentPosition();
            } catch (InterruptedException e) {
                return;
            } catch (Exception e) {
                return;
            }            
            progressBar.setProgress(currentPosition);
        }


    }

    public boolean onOptionsItemSelected(MenuItem item){
        Intent myIntent = new Intent(getApplicationContext(), MainActivity.class);
        startActivityForResult(myIntent, 0);
        return true;

        }

    }
4

1 回答 1

1

首要问题:

在线程“运行”方法中,在你说的 while 条件下:

(currentPosition<total)

这样“currentPosition”值将永远不会达到“total”值并且您的进度条不会走,您应该使用“小于或等于”:

(currentPosition<=total)

所以代码将是:

public void run() {
    int currentPosition= 0;
    int total = mp.getDuration();
    while (mp!=null && mp.isPlaying() && currentPosition<=total) {
        try {
            Thread.sleep(1000);
            currentPosition= mp.getCurrentPosition();
        } catch (InterruptedException e) {
            return;
        } catch (Exception e) {
            return;
        }            
        progressBar.setProgress(currentPosition);
    }
}

第二题:

您可以使用“mp.setOnCompletionListener(this)”更改 ImageButton,如下所示:

mp.setOnCompletionListener(new OnCompletionListener() {

        public void onCompletion(MediaPlayer mp) {
            pauseicon.setImageResource(R.drawable.playicon);
        }
    });

在初始化媒体播放器时设置它,在 mp.start() 之前

于 2013-05-28T18:18:48.460 回答