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;
        /**
         * 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();
         }

        mp.setOnCompletionListener(new OnCompletionListener() {
            //When audio is done will change pause to play
            public void onCompletion(MediaPlayer mp) {
                pauseicon.setImageResource(R.drawable.playicon);

            }
        });


        pauseicon.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                 // No need to check if it is pauseicon

        if(mp.isPlaying()){
            mp.pause();
         ((ImageButton) v).setImageResource(R.drawable.playicon);

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

   }
        //To update progress bar
   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);
        }
    }
   @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);


        if (mp != null)
        if(mp.isPlaying())
              mp.stop();

          mp.release();

            return true;
        default:
            return super.onOptionsItemSelected(item);

    }
     }
    @Override 
    public void onBackPressed(){
      if (mp != null){
          if(mp.isPlaying())
              mp.stop();

          mp.release();
      }

      //there is no reason to call super.finish(); here
      //call super.onBackPressed(); and it will finish that activity for you
      super.onBackPressed(); 
    }
    }
4

2 回答 2

1

您的问题是mp.getDuration()毫秒,每首歌曲都会发生变化。所以不要使用progressBar.setMax(mp.getDuration());. 因为它只适用于第一首歌。改为使用progressBar.setMax(100);。然后使用currentPosition= 100 * mp.getCurrentPosition() / mp.getDuration();这应该可以正常工作。

也不要忘记progressBar.setProgress(0);在完成一首歌曲或单击停止按钮时添加。

于 2013-08-27T18:19:21.090 回答
0

编辑:尝试在您的完成侦听器中重置它。然后刮掉二传手。尝试使用这个:

Thread.sleep(1000);
currentPosition++;

而不是这个:

Thread.sleep(1000);
currentPosition= mp.getCurrentPosition();

那在你的run()功能中。然后它将从 0 增加到总计,您不必担心获取或设置当前位置,只需担心下面的先前更改正在完成的进度。

mp.setOnCompletionListener(new OnCompletionListener() {
    //When audio is done will change pause to play       
    public void onCompletion(MediaPlayer mp) {
        pauseicon.setImageResource(R.drawable.playicon);

        //Reset here
        progressBar.setProgress(0);  
    }
});
于 2013-08-27T13:46:03.377 回答