在android中使用seekbar.setOnSeekBarChangeListener后,媒体播放器无法正常工作

时间:2016-01-20 13:06:19

标签: android audio-player android-seekbar android-music-player

我正在开发一个用户可以在线收听音乐的应用程序。 我从MP3获取api个文件并在音乐播放器中播放。玩家是 工作正常,但Seekbar当时没有工作。当我添加一个 seekbar的代码片段,播放器已停止播放音乐或 反应很慢。

这是我的代码,

public class Playsong extends Activity {
  private Button b1,b2,b3,b4;
   private ImageView iv;
   private MediaPlayer mediaPlayer;
   private double startTime = 0;
   private double finalTime = 0;
   private Handler myHandler = new Handler();;
   private int forwardTime = 5000;
   private int backwardTime = 5000;
   private SeekBar seekbar;
   private TextView tx1,tx2,tx3;

   public static int oneTimeOnly = 0;
   String PATH;

   private final Handler handler = new Handler();

   private final Runnable updatePositionRunnable = new Runnable() {
           public void run() {
                   updatePosition();
           }
   };


   @Override
   protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);  
    setContentView(R.layout.songsplay);
    PATH =getIntent().getExtras().getString("path");

    initializeViews();
}



public void initializeViews(){

     b1 = (Button) findViewById(R.id.button4);
      b2 = (Button) findViewById(R.id.button2);
      b3=(Button)findViewById(R.id.button3);
      b4=(Button)findViewById(R.id.button);
      iv=(ImageView)findViewById(R.id.imageView1);
      seekbar=(SeekBar)findViewById(R.id.seekBar);
      tx1=(TextView)findViewById(R.id.textView2);
      tx2=(TextView)findViewById(R.id.textView3);
      tx3=(TextView)findViewById(R.id.textView4);
      tx3.setText("Song.mp3");

    mediaPlayer = MediaPlayer.create(this, Uri.parse(PATH));

    finalTime = mediaPlayer.getDuration();
//duration = (TextView) findViewById(R.id.songDuration);
    seekbar = (SeekBar) findViewById(R.id.seekBar);

    seekbar.setMax((int) finalTime);

     seekbar.setClickable(false);
      b2.setEnabled(false);

      seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
            // TODO Auto-generated method stub
            mediaPlayer.seekTo(progress);

        }
    });



      b3.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            Toast.makeText(getApplicationContext(), "Playing sound",Toast.LENGTH_SHORT).show();
            mediaPlayer.start();

            finalTime = mediaPlayer.getDuration();
            startTime = mediaPlayer.getCurrentPosition();

            if (oneTimeOnly == 0) {
               seekbar.setMax((int) finalTime);
               oneTimeOnly = 1;
            }
            tx2.setText(String.format("%d min, %d sec",
            TimeUnit.MILLISECONDS.toMinutes((long) finalTime),
            TimeUnit.MILLISECONDS.toSeconds((long) finalTime) -
            TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) finalTime)))
            );

            tx1.setText(String.format("%d min, %d sec",
            TimeUnit.MILLISECONDS.toMinutes((long) startTime),
            TimeUnit.MILLISECONDS.toSeconds((long) startTime) -
            TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) startTime)))
            );

            seekbar.setProgress((int)startTime);
            myHandler.postDelayed(UpdateSongTime,100);
            b2.setEnabled(true);
            b3.setEnabled(false);
         }
      });

      b2.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            Toast.makeText(getApplicationContext(), "Pausing sound",Toast.LENGTH_SHORT).show();
            mediaPlayer.pause();
            b2.setEnabled(false);
            b3.setEnabled(true);
         }
      });

      b1.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            int temp = (int)startTime;

            if((temp+forwardTime)<=finalTime){
               startTime = startTime + forwardTime;
               mediaPlayer.seekTo((int) startTime);
               Toast.makeText(getApplicationContext(),"You have Jumped forward 5 seconds",Toast.LENGTH_SHORT).show();
            }
            else{
               Toast.makeText(getApplicationContext(),"Cannot jump forward 5 seconds",Toast.LENGTH_SHORT).show();
            }
         }
      });

      b4.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            int temp = (int)startTime;

            if((temp-backwardTime)>0){
               startTime = startTime - backwardTime;
               mediaPlayer.seekTo((int) startTime);
               Toast.makeText(getApplicationContext(),"You have Jumped backward 5 seconds",Toast.LENGTH_SHORT).show();
            }
            else
            {
               Toast.makeText(getApplicationContext(),"Cannot jump backward 5 seconds",Toast.LENGTH_SHORT).show();
            }
         }
      });
   }

   private Runnable UpdateSongTime = new Runnable() {
      public void run() {
         startTime = mediaPlayer.getCurrentPosition();
         tx1.setText(String.format("%d min, %d sec",

         TimeUnit.MILLISECONDS.toMinutes((long) startTime),
         TimeUnit.MILLISECONDS.toSeconds((long) startTime)-
         TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.
         toMinutes((long) startTime))));
         seekbar.setProgress((int)startTime);
         myHandler.postDelayed(this, 100);
      }
   };


   protected void updatePosition() {
        // TODO Auto-generated method stub

        handler.removeCallbacks(updatePositionRunnable);

        seekbar.setProgress(mediaPlayer.getCurrentPosition());

        handler.postDelayed(updatePositionRunnable, 2000);

    }


 /*public void onBackPressed() {

     super.onBackPressed();
     try {
            if(mediaPlayer!=null && mediaPlayer.isPlaying()) {
                Log.d("TAG------->", "player is running");
                mediaPlayer.stop();
                Log.d("Tag------->", "player is stopped");
                mediaPlayer.release();
                Log.d("TAG------->", "player is released");
            }
        } catch(Exception e){
        }

 };*/
  }

请告诉我这里缺少的东西。在此先感谢。)

0 个答案:

没有答案