0

I'm using media player. I want to show progress bar while connection URL and media player get prepare, but I can't show it.

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

1 に答える 1

1

pBar.setVisibility(ProgressBar.GONE);スレッドを開始した直後に呼び出しています..表示されてすぐに消えます..
処理が完了したら、runメソッドに移動します。

于 2012-09-18T11:59:19.910 に答える