如何为媒体播放器设置处理程序

时间:2016-03-24 12:04:19

标签: android media-player

我正在尝试制作媒体播放器应用,但在为处理程序设置“postDelayed”时出错。

这是我的代码:

 public class MainActivity 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() {
    @Override
    public void close() {

    }

    @Override
    public void flush() {

    }

    @Override
    public void publish(LogRecord record) {

    }
};

private  int forwardTime=5000;
private int backwardTime=5000;
private SeekBar seekBar;
private TextView tx1,tx2,tx3;
public static int oneTimeOnly =0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    b1= (Button)findViewById(R.id.button);
    b2=(Button)findViewById(R.id.button2);
    b3=(Button)findViewById(R.id.button3);
    b4=(Button)findViewById(R.id.button4);
    tx1=(TextView)findViewById(R.id.textView);
    tx2=(TextView)findViewById(R.id.textView2);
    tx3=(TextView)findViewById(R.id.textView3);
    tx3.setText("Song,mp3");
    iv=(ImageView)findViewById(R.id.imageView);
    mediaPlayer =(MediaPlayer.create(this,R.raw.music));
    seekBar =(SeekBar)findViewById(R.id.seekBar);
    b2.setEnabled(false);
    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) finalTime) - 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(),"pause song",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",Toast.LENGTH_SHORT).show();
            }
            else {
                Toast.makeText(getApplicationContext(),"cannot jump",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 back",Toast.LENGTH_SHORT).show();
            }
            else {
                Toast.makeText(getApplicationContext(),"cannot jump",Toast.LENGTH_SHORT).show();
            }
        }
    });
}
private Runnable UpdateSongTime = new Runnable() {
    @Override
    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.MILLISECONDS.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long)startTime))));
        seekBar.setProgress((int)startTime);
        myHandler.postDelayed(this,100);
    }
};

0 个答案:

没有答案