In my android application i am playing videos using video view.While the video is getting downloaded i am showing a progress dialog.
At times when the streaming is not supported or when there is some error an error message is displayed onto the screen.After the ok click of the error message the progress dialogue again shows the message and tries to download.
But i would like to dismiss this dialogue if there is any error 开发者_Go百科messgae and as soon as the user clicks Ok and return to the video player
so that the user can go through the next or previous video.
private ProgressDialog mProgressDialog;
Handler myUiHandler = new Handler();
boolean m_prgisShowing = false;
private static final int DIALOG_100 = 0;
ProgressDialog mDialog2;
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
Log.i("Manju", "OnPrepared");
myUiHandler.post(myDilgDismis);
mVideoView.start();
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_100: {
mDialog2 = new ProgressDialog(this);
mDialog2.setMessage("buffering...");
mDialog2.setIndeterminate(true);
mDialog2.setCancelable(true);
return mDialog2;
}
}
return null;
}
Runnable myDilgShow = new Runnable() {
public void run() {
show_My_Dialog();
}
};
Runnable myDilgDismis = new Runnable() {
public void run() {
dismiss_My_Dialog();
}
};
public void show_My_Dialog() {
m_prgisShowing = true;
showDialog(DIALOG_100);
}
public void dismiss_My_Dialog() {
if (m_prgisShowing) {
try {
if (null != mDialog2) {
mDialog2.dismiss();
}
} catch (Exception e) {
e.printStackTrace();
m_prgisShowing = false;
}
}
m_prgisShowing = false;
}
Please share your valuable suggestions.
Thanks in advance
I assume you use AsyncTask for threading, in this case for streaming or downloading. Then you have implemented the
onProgressUpdate
function. In this function you update the progress dialog to show the current streaming status. To dismiss the progress dialog you have to set its visibility to false or remove it completely from the layout. In onProgressUpdate you need an if statement or something similar to catch the error and dismiss the dialog.
EDIT:
The code shows that you use runnable interface to implement threads, this is good for basic java development. But android sdk has a solution especially for android development called async task. It is recommended to use it.
E.g. you can add a private class to your main java file where the oncreate method is located.
private class MediaPlayer extends AsyncTask<Params, Progress, Result> {
}
a. Params: the type of the parameters sent to the task upon execution.
b. Progress: the type of the progress units published during the background computation.
c. Result: the type of the result of the background computation.
Every async task implements the following methods. 4 steps an AsyncTask will go through:
a. onPreExecute() - invoked on UI thread immediately
-> Called before background computation starts
-> Do some setup (such as, display a progress dialog)
b. doInBackground(Params...) - invoked on the background thread
-> Called immediately after onPreExecute()
-> Perform computation in background thread that can take a long time.
-> Use publishProgress(Progress...) to trigger UI update progress
-> Progress will be passed to onProgressUpdate(Progress...)
c. onProgressUpdate(Progress...) - invoked on UI thread
-> Update progress
d. onPostExecute(Result) - invoked on UI thread
-> Called after the background computation finishes
-> The result will be passed from doInBackground(Params...)
How to start AsyncTask?
new MyTask().execute(param1, param2, param3);
This is a little overview. I strongly recommend you to read the doc.
精彩评论