0

我已经问了这个问题 2 次了,现在仍然没有得到它的工作。任何帮助都是极好的

音频完成后,我的 ProgressBar 不会重置,进度条只会停留在最大蓝线。我之前问过一个问题并让它工作,但现在只是停止工作,不知道为什么它不工作。任何帮助都是极好的。

我想要的只是随机选择一个音频然后播放一个,完成后你可以再次按下播放来收听它随机选择的相同音频。

继承人代码:

 public class player2 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_2);
        pauseicon = (ImageButton) findViewById(R.id.pauseicon);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        getActionBar().setDisplayHomeAsUpEnabled(true);



        mfile[0] = R.raw.sound04;  //****REPLACE THESE WITH THE PROPER NAMES OF YOUR SOUND FILES
        mfile[1] = R.raw.sound05;  //PLACE THE SOUND FILES IN THE /res/raw/ FOLDER IN YOUR PROJECT*****
        mfile[2] = R.raw.sound06;
        // 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(player2.this, mfile[rnd.nextInt(NUM_SOUND_FILES)]);
             mp.seekTo(0);
             mp.start(); ;         
             progressBar.setVisibility(ProgressBar.VISIBLE);
             progressBar.setProgress(0);
             progressBar.setMax(100);

             new Thread(this).start();

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

        mp.setOnCompletionListener(new OnCompletionListener() {

            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);
     }}});

   }

   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

1 回答 1

1

我没有彻底检查所有代码,但快速浏览一下,我猜你的线程(更新进度条)在完成时停止并且你永远不会再次启动它(即当用户再次点击播放时)。只需尝试重新启动您的线程pauseicon.setOnClickListener(播放完成后)。例子:

pauseicon.setOnClickListener(new View.OnClickListener() {

   public void onClick(View v) {
      if(mp.isPlaying()) {
         mp.pause();
         ((ImageButton) v).setImageResource(R.drawable.playicon);
      } else {
         mp.start();
         ((ImageButton) v).setImageResource(R.drawable.pauseicon);
         // RESTART THE UPDATE THREAD //
         new Thread(this).start();
      }
   }
});

编辑使用静态变量来存储线程,以便可以从视图的onClick方法重新启动它:

// add this to your class as a member
static Thread progressThread = new Thread(this);

// add this to BOTH onCreate and onClick
progressThread.start();

如果这不起作用(我现在无法测试),您可以简单地保持线程运行,例如:

// flag to set when thread should be actively running
static boolean runThread = true;

// change your run method to something as follows
public void run() {
    while ( runThread )  {
       if ( mp != null && currentPosition <= total )  {
          int currentPosition= 0;
          int total = mp.getDuration();
          try {
             Thread.sleep(1000);
             currentPosition= mp.getCurrentPosition();
          } catch (InterruptedException e) {
             return;
          } catch (Exception e) {
             return;
          }            
          progressBar.setProgress(currentPosition);
       }
       else
          Thread.sleep(1000);
    }
}

// then when you no longer need to update the progress bar set the flag to false,
// which will cause your thread to finish. this can go anywhere, depending on
// your needs
runThread = false; 
于 2013-09-01T13:06:39.267 回答