我正在使用媒体播放器。我想在连接 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);
}
}
}