带有进度条的android自定义对话框

时间:2014-08-10 09:39:20

标签: android mp3

在下面的代码中,Mp3播放效果不错,但我的进度条不起作用..我该如何解决这个问题?当我点击对话框打开好,从网址播放mp3也工作正常,但播放歌曲进度条不起作用。

 public class MainActivity extends Activity {

final Context context = this;
private Button button;
Dialog myDialog;
Button buttonclose;
ImageButton buttonplay, buttonstop;
ProgressBar progressBar;
MediaPlayer mp = new MediaPlayer();

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    button = (Button) findViewById(R.id.buttonShowCustomDialog);
    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            // custom dialog
             myDialog = new Dialog(MainActivity.this);
              myDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
             myDialog.setContentView(R.layout.custom);
             buttonplay=(ImageButton)myDialog.findViewById(R.id.play);
             buttonstop=(ImageButton)myDialog.findViewById(R.id.stop);
             buttonclose=(Button)myDialog.findViewById(R.id.close);
             progressBar = (ProgressBar) findViewById(R.id.progressBar);
             final String url = "http://fileraja.info/Tamil/O/Osthi/Osthi_Maamey-VmusiQ.Com.mp3";
             buttonplay.setOnClickListener(new OnClickListener() {
                  @Override
                   public void onClick(View v) {
                   try {
                        if(mp.isPlaying()){

                            mp.stop();
                            buttonplay.setVisibility(1);
                            buttonplay.setClickable(true);
                            buttonstop.setVisibility(View.GONE);
                            buttonstop.setClickable(true);
                             progressBar.setVisibility(ProgressBar.GONE);

                            }else{
                            mp.reset();
                            mp.setDataSource(url);
                               mp.prepare();
                            mp.start();
                            buttonstop.setVisibility(1);
                            buttonstop.setClickable(true);
                            buttonplay.setVisibility(View.GONE);
                            buttonplay.setClickable(false);
                            progressBar.setVisibility(ProgressBar.VISIBLE);
                            progressBar.setProgress(0);
                            progressBar.setMax(mp.getDuration());


                            }
                      } catch (Exception e) {
                          Log.e("activity", "Couldn't start");
                      }
                  }



             });

             buttonstop.setOnClickListener(new OnClickListener() {
                 @Override
                  public void onClick(View v) {
                    mp.stop();
                    buttonplay.setVisibility(1);
                    buttonplay.setClickable(true);
                    buttonstop.setVisibility(View.GONE);
                    buttonstop.setClickable(true);
                  }
            });



             buttonclose.setOnClickListener(new OnClickListener() {
                 @Override
                  public void onClick(View v) {
                        // TODO Auto-generated method stub
                        myDialog.cancel();
                        mp.stop();
                  }
            });

             myDialog.show();
       }
 });

}

}

0 个答案:

没有答案