i am trying to make the condition "when the dialog will be dismissed then auto play the video..." with the bellow code , i was accomplished to do that but the videoview doesnt auto play... can you suggest something?
mycode.java
final ProgressDialog progressDialog = new ProgressDialog(PlayVideo.this);
// Set progressbar title
progressDialog.setTitle("Please set the pyramid");
// Set progressbar message
progressDialog.setMessage("Loading...");
// Show progressbar
progressDialog.show();
// Find your VideoView in your video_main.xml layout
myVideoView = (VideoView) findViewById(R.id.video_view);
Uri uri = getIntent().getParcelableExtra("uri");
try {
myVideoView.setMediaController(mediaControls);
myVideoView.setVideoURI(uri);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
myVideoView.requestFocus();
myVideoView.setOnPreparedListener(new OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
if(progressDialog.isShowing()) {
Runnable progressRunnable = new Runnable() {
#Override
public void run() {
progressDialog.cancel();
}
};
Handler pdCanceller = new Handler();
pdCanceller.postDelayed(progressRunnable, 3000);
}
if (!progressDialog.isShowing()) {
myVideoView.seekTo(position);
if (position == 0) {
myVideoView.start();
}
else {
myVideoView.pause();
}
}
}
});
}
This will dismiss your dialog once the video is ready and play the video
if (progressDialog.isShowing()) {
Runnable progressRunnable = new Runnable() {
#Override
public void run() {
progressDialog.cancel();
myVideoView.seekTo(position);
if (position == 0) {
myVideoView.start();
} else {
myVideoView.pause();
}
}
};
Handler pdCanceller = new Handler();
pdCanceller.postDelayed(progressRunnable, 3000);
}
I don't know why you are pausing. If you can explain what exactly is the flow that you are trying to implement then I can help better
Related
I have a QR scanner activity which moves to the next activity on successful code scan. However, I want to have a back button to return to the activity (if this result may be incorrect). When I return to the activity, however, the result is still stored and the scanner is not scanning for codes.
How can I restart the barcode detector? Should I override onPause/onResume? Below is my code thus far.
private void setupBarcodeDetector() {
barcodeDetector =
new BarcodeDetector.Builder(this).setBarcodeFormats(Barcode.QR_CODE).build();
cameraSource =
new CameraSource.Builder(this, barcodeDetector)
.setRequestedPreviewSize(640, 480)
.build();
surfaceView
.getHolder()
.addCallback(
new SurfaceHolder.Callback() {
#Override
public void surfaceCreated(SurfaceHolder holder) {
if (ActivityCompat.checkSelfPermission(
getApplicationContext(), Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
return;
}
try {
cameraSource.start(holder);
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void surfaceChanged(
SurfaceHolder holder, int format, int width, int height) {
// LEAVE EMPTY
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
cameraSource.stop();
}
});
barcodeDetector.setProcessor(
new Detector.Processor<Barcode>() {
#Override
public void release() {
// LEAVE EMPTY
}
#Override
public void receiveDetections(Detector.Detections<Barcode> detections) {
final SparseArray<Barcode> qrCodes = detections.getDetectedItems();
if (qrCodes.size() != 0) {
barcodeDetector.release();
resultQRtv.post(
new Runnable() {
#Override
public void run() {
Vibrator vibrator =
(Vibrator)
getApplicationContext()
.getSystemService(
Context
.VIBRATOR_SERVICE);
vibrator.vibrate(500);
successPrompt.setVisibility(View.VISIBLE);
allGoodTv.setVisibility(View.VISIBLE);
// Loading animation - to be changed with specific
// animation
if (dialog == null) {
dialog =
new ProgressDialog(
QRCodeScannerActivity.this);
dialog.setCancelable(false);
dialog.setMessage("Registering...");
}
dialog.show();
Handler handler = new Handler();
handler.postDelayed(
new Runnable() {
#Override
public void run() {
// On successful scan, go to the required
// activity
Intent
goToAdminAccountConfirmationActivity =
new Intent(
QRCodeScannerActivity
.this,
AdminAccountConfirmSettings
.class);
startActivity(
goToAdminAccountConfirmationActivity);
// Remove the prompts in case the user
// returns to this activity
dialog.dismiss();
successPrompt.setVisibility(View.GONE);
allGoodTv.setVisibility(View.GONE);
}
},
1500);
}
});
}
}
});
} // setupBarcodeDetector
In your case call setupBarcodeDetector() method inside onResume() method of your activty.
for batter optimisation do the initialization in onCreate() method and start and stop scanning inside onPause() and onResume() methods.
I am making a app for training. In my app one video is playing after some second one alert dialog show the dialog contain question and answer student read the question and select the answer until my thread want to wait. Here i successfully get the video timing. I want to do pause the thread until alert dialog close.
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
running = true;
final int duration = mVideoView.getDuration();
new Thread(new Runnable() {
boolean myRunning;
public void run() {
synchronized (mSync) {
myRunning = mSync.running;
}
do {
textView.post(new Runnable() {
public void run() {
int time = Math.round(mVideoView.getCurrentPosition() / 1000);
textView.setText(time + "");
if (time == 5) {
running = false;
mVideoView.pause();
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Page02.this);
alertDialogBuilder.setMessage("Are you sure,You wanted to make decision");
alertDialogBuilder.setPositiveButton("yes",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface arg0, int arg1) {
onResume();
mVideoView.start();
arg0.dismiss();
}
});
alertDialogBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
});
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (!running) break;
}
while (mVideoView.getCurrentPosition() < duration);
}
}).start();
}
});
}
#Override
protected void onPause() {
super.onPause();
synchronized (mSync) {
running = false;// you can not set it here because
// it is possible for the thread to read it and exit the loop before he posts your message
mSync.mustBePost = true;
mSync.message = "Main Activity is going to pause";
}
}
#Override
protected void onResume() {
super.onResume();
threadNameCounter++;
synchronized (mSync) {
running = true;
mSync.mustBePost = true;
mSync.message = "Main Activity is going to resume";
}
t = new Thread("My Name is " + String.valueOf(threadNameCounter));
t.start();
}
}
I've got this code:
#Override
public void onClick(View v) {
progressDoalog = new ProgressDialog(Hack.this);
progressDoalog.setMax(100);
progressDoalog.setMessage("Its loading....");
progressDoalog.setTitle("ProgressDialog bar example");
progressDoalog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDoalog.show();
new Thread(new Runnable() {
#Override
public void run() {
try {
while (progressDoalog.getProgress() <= progressDoalog
.getMax()) {
Thread.sleep(200);
handle.sendMessage(handle.obtainMessage());
if (progressDoalog.getProgress() == progressDoalog
.getMax()) {
progressDoalog.dismiss();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
Handler handle = new Handler() {
#Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
progressDoalog.incrementProgressBy(1);
}
};
});
}
}
Where can I add a code to open new activity when the ProgressDialog will be at 100%? Which and where exactly? Thanks for your help!
You can't start an Activity from a Dialog, but what you can do is start the Activity from the old one using a OnDismissListener.
Take a look at the documemtation :
https://developer.android.com/reference/android/content/DialogInterface.OnDismissListener.html
I haven't noticed but you can check the progress in your Handler, check if it's 100%, dismiss the dialog and start the new Activity, remember that you gotta do this on the UI thread
It is my first time implement horizontal progress bar in my application
The goal is after the progress bar finish, the application show toast message and image bitmap
the toast message is doing fine but the image make the application to force close
this is my code:
Button buttonProceed = (Button) findViewById(R.id.buttonProceed);
buttonProceed.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
progressBar = new ProgressDialog(v.getContext());
progressBar.setCancelable(true);
progressBar.setMessage("Processing");
progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressBar.setProgress(0);
progressBar.setMax(100);
progressBar.show();
progressBarStatus = 0;
fileProcess = 0;
String toastMessage = "Citra host berhasil ditanam citra watermark dan disimpan di internal storage";
final Toast toast = Toast.makeText(getApplicationContext(), toastMessage, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);
final ImageView resultImageView = (ImageView) findViewById(R.id.viewResult);
new Thread(new Runnable() {
public void run() {
while (progressBarStatus < 100) {
progressBarStatus = progressMarker();
try{
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
progressBarHandler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressBarStatus);
}
});
}
if (progressBarStatus >= 100) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
progressBar.dismiss();
toast.show();
//the resultBitmap variable is declared in global
resultImageView.setImageBitmap(resultBitmap);
}
}
}).start();
}
});
}
So, could someone help me figure what wrong in my code?
Regard!
The problem is that you aren't allowed to change UI in new Thread. For this purpose you could use AsyncTask instead of Thread.
Here is some example: How to use asynctask to display a progress bar that counts down?
You should call progressBar.setProgress(progressBarStatus); in onProgressUpdate(String... progress) of AsyncTask.
And this code should be called in onPostExecute(String result):
progressBar.dismiss();
toast.show();
//the resultBitmap variable is declared in global
resultImageView.setImageBitmap(resultBitmap);
Because code in onPostExecute is called on main UI Thread.
I have a task to run which takes a long time. So, I'd like to implement a progress dialog, spinning wheel, to show a message to users that the task is still running in the background. I found many solutions online and I used the following code. I ensured to run the task in separate thread. But it is not showing on UI.
OnClickListener confirmPrintButtonListener = new OnClickListener() {
public void onClick(View v) {
try {
final SalesStockController salesStockController = new SalesStockController();
final ArrayList<ProductReload> productReloadList = reloadActivityAdapter
.getReloadList();
if (productReloadList.size() != 0) {
progressDialog = new ProgressDialog(StockReloadActivity.this);
progressDialog.setTitle("ABC Trading");
progressDialog.setMessage("Wait while loading...");
progressDialog.show();
new Thread(new Runnable() {
public void run()
{
// do the thing that takes a long time
try {
salesStockController.reload(productReloadList);
} catch (SQLiteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ABCException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
runOnUiThread(new Runnable() {
public void run()
{
progressDialog.dismiss();
}
});
}
}).start();
AlertDialog.Builder builder = new AlertDialog.Builder(
v.getContext());
builder.setCancelable(false).setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
finish();
}
});
AlertDialog alert = builder.create();
alert.setTitle("ABC Trading");
alert.setMessage("Reload Successfully");
alert.show();
ReloadSlipPrint print = new ReloadSlipPrint(
productReloadList);
print.print();
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(
v.getContext());
builder.setCancelable(false).setPositiveButton("OK", null);
AlertDialog alert = builder.create();
alert.setTitle("ABC Trading");
alert.setMessage("No Stock To Reload");
alert.show();
}
} catch (Exception e) {
ABCUtil.displayErrorMsg(v.getContext(), e);
}
}
};
Can someone please point me out what is wrong with my code? Any help will be very much appreciated.
Just made up a simple example for you, you can try it.
public class MainActivity extends Activity {
private ProgressDialog mLoadingDialog;
private Handler mHandler = new Handler();
private void showLoadingDialog(final String title, final String msg) {
mHandler.post(new Runnable() {
#Override
public void run() {
if(mLoadingDialog == null) {
mLoadingDialog = ProgressDialog.show(MainActivity.this, title, msg);
}
mLoadingDialog.setTitle(title);
mLoadingDialog.setMessage(msg);
}
});
}
private void hideLoadingDialog() {
mHandler.post(new Runnable() { //Make sure it happens in sequence after showLoadingDialog
#Override
public void run() {
if(mLoadingDialog != null) {
mLoadingDialog.dismiss();
}
}
});
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread() {
#Override
public void run() {
showLoadingDialog("Loading", "Please wait...");
//DO something
hideLoadingDialog();
}
}.start();
}
}
You can use AsyncTask class to perform long runnning task
below is api link for the same
http://developer.android.com/reference/android/os/AsyncTask.html