Android:ProgressBar无效

时间:2012-09-18 11:56:07

标签: android progress-bar android-mediaplayer

我正在使用媒体播放器。我想在连接URL和媒体播放器准备时显示进度条,但我无法显示它。

public class MainActivity extends Activity implements OnClickListener, Runnable{

MediaPlayer mPlayer;
ProgressBar pBar = null;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Continue();
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
public void Connect()
{           
    try {
        Toast.makeText(getApplicationContext(),"Yayın Açılana Kadar Lütfen Bekleyiniz" , Toast.LENGTH_SHORT).show();
        mPlayer.prepare();
    } catch (IllegalStateException e) {
        Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
        e.printStackTrace();
    } catch (IOException e) {
        Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
        e.printStackTrace();
    }   

    mPlayer.start();
    pBar.setVisibility(ProgressBar.VISIBLE);
    pBar.setProgress(0);
    pBar.setMax(mPlayer.getDuration());
    new Thread(this).start();
    if(mPlayer!=null)pBar.setVisibility(ProgressBar.GONE);
}
public void Continue()
{
    mPlayer = new MediaPlayer();
    String str = "http:MyURL";
    mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    try {
        mPlayer.setDataSource(str);
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
        e.printStackTrace();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
        e.printStackTrace();
    }   
    Connect();
}
public void run() {
    // TODO Auto-generated method stub
    int currentPosition =0;
    int total = mPlayer.getDuration();
    while(mPlayer!=null && currentPosition<total){
        try{
            Thread.sleep(1000);
            currentPosition = mPlayer.getCurrentPosition();
        }catch(InterruptedException e){
            return;
        }catch(Exception e){
            return;
        }
        pBar.setProgress(currentPosition);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

你在开始线程后立即调用pBar.setVisibility(ProgressBar.GONE); ..它会显示然后立即消失。
处理完成后,在run方法中移动它。