I am attempting to display a ProgressDialog while I am loading a video.
My play(String _videoUri) function doesn't seem to get called.
Can anyone tell me why?
I have this exact same setup for starting a new Intent and it seems to work fine, but not in this case or loading a video from a given URL.
private void showPopupMenu(View v){
PopupMenu popupMenu = new PopupMenu(VideoPlayer.this, v);
if(bitRates != null){
for(int i=0;i<bitRates.size();i++){
int menuItem = i;
popupMenu.getMenu().add(0,menuItem, 0,"BitRate: "+bitRates.get(i));
}
}
popupMenu.show();
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(final MenuItem item) {
progressDialog = ProgressDialog.show(VideoPlayer.this, "", "Loading video...");
new Thread() {
public void run() {
try{
play(streamUrls.get(item.getItemId()));
}
catch (Exception e)
{
Log.e("tag", e.getMessage());
}
progressDialog.dismiss();
}
}.start();
return true;
}
});
}
public void play(String _videoUri){
Log.i(TAG,"URI is : "+Uri.parse(_videoUri).toString());
MediaController mc = new MediaController(VideoPlayer.this);
mVideoView.setMediaController(mc);
mVideoView.setVideoURI(Uri.parse(_videoUri));
mVideoView.requestFocus();
mVideoView.start();
}
Try putting your anonymous thread in parentheses.
(new Thread() { ... }).start()
Related
Here is my Class
public class VideoViewActivity extends Activity implements OnInfoListener, OnBufferingUpdateListener {
boolean isStart;
VideoView mVideoView;
ProgressBar pb;
TextView downloadRateView, loadRateView;
String videourl = "";
ImageView img_add;
ImageLoader imageLoader = ImageLoader.getInstance();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!LibsChecker.checkVitamioLibs(this))
return;
setContentView(R.layout.video_view);
img_add = (ImageView) findViewById(R.id.img_add);
// imageLoader.init(GetOption.getConfig(this));
WebAPIHelper web = new WebAPIHelper(Constant.videoaddimage, VideoViewActivity.this, true);
web.callRequestGet("http://fillatv.com/fillatv/service/get_banners.php");
pb = (ProgressBar) findViewById(R.id.probar);
downloadRateView = (TextView) findViewById(R.id.download_rate);
loadRateView = (TextView) findViewById(R.id.load_rate);
mVideoView = (VideoView) findViewById(R.id.surface_view);
mVideoView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}
});
Button btn_back = (Button) findViewById(R.id.back);
btn_back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
VideoViewActivity.this.finish();
}
});
Intent intent = getIntent();
videourl = ((DataContainer) intent.getSerializableExtra("v_url")).link;
//videourl = intent.getStringExtra("v_url");
PlayVideo();
}
#Override
public void onResume() {
super.onResume();
}
#Override
public void onPause() {
super.onPause();
}
private void PlayVideo() {
Log.e("video url", videourl);
Uri uri = Uri.parse(videourl);
mVideoView.setVideoURI(uri);
mVideoView.setMediaController(new MediaController(this));
mVideoView.requestFocus();
mVideoView.setOnInfoListener(this);
mVideoView.setOnBufferingUpdateListener(this);
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
// optional need Vitamio 4.0
mediaPlayer.setPlaybackSpeed(1.0f);
}
});
mVideoView.setOnErrorListener(new OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Toast.makeText(getApplicationContext(), "no Streaming Found try after some time", Toast.LENGTH_LONG).show();
finish();
return false;
}
});
}
#Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
switch (what) {
case MediaPlayer.MEDIA_INFO_BUFFERING_START:
if (mVideoView.isPlaying()) {
mVideoView.pause();
isStart = true;
pb.setVisibility(View.VISIBLE);
downloadRateView.setVisibility(View.VISIBLE);
loadRateView.setVisibility(View.VISIBLE);
}
break;
case MediaPlayer.MEDIA_INFO_BUFFERING_END:
if (isStart) {
mVideoView.start();
pb.setVisibility(View.GONE);
downloadRateView.setVisibility(View.GONE);
loadRateView.setVisibility(View.GONE);
}
break;
case MediaPlayer.MEDIA_INFO_DOWNLOAD_RATE_CHANGED:
downloadRateView.setText(" " + extra + "kb/s" + " ");
break;
}
return true;
}
#Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
loadRateView.setText(percent + "%");
}
public void viewdata(String data) {
String img;
try {
JSONArray j_array = new JSONArray(data);
for (int i = 0; i < j_array.length(); i++) {
JSONObject j_obj = j_array.getJSONObject(i);
System.out.print(j_obj.getString("status"));
System.out.print(j_obj.getString("id"));
System.out.print(j_obj.getString("Image"));
img = j_obj.getString("Image");
imageLoader.displayImage(img, img_add, GetOption.getOption());
}
} catch (Exception e) {
}
}
}
When Try to run it shows below error. spent one day to resolve this but can't find important hope you guys have solution for this.
E/Vitamio[4.2.6][Player]: FIND_NAME_SYM vvo, render_yuv
11-26 18:59:29.556 28707-28882/com.fillatv.live E/Vitamio[4.2.6][Player]: avformat_open_input: Invalid data found when processing input : -1094995529
11-26 18:59:29.556 28707-28879/com.fillatv.live E/Vitamio[4.2.6][Player]: error (1, -1094995529)
11-26 18:59:29.637 28707-28707/com.fillatv.live E/Vitamio[Player]: Error (1, -1094995529)
If anyone needs more detail please tell me,I will provide.Any help would be appreciated,Thanks in advance.
I have an android app that streams audio by URL (its an infinite stream) and it works. But I need to allow users to rewind X seconds and forward X seconds ( you can only forward if you rewind before )
here's the code
public class MainActivity extends Activity implements View.OnClickListener {
private final String TAG = "Main";
private Button buttonPlay;
private MediaPlayer player;
private boolean paused = false;
private SeekBar volumeSeekbar = null;
private AudioManager audioManager = null;
private Button forward, rewind;
private void initControls()
{
try
{
volumeSeekbar = (SeekBar)findViewById(R.id.volume);
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
volumeSeekbar.setMax(audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
volumeSeekbar.setProgress(audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC));
volumeSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
{
#Override
public void onStopTrackingTouch(SeekBar arg0)
{
}
#Override
public void onStartTrackingTouch(SeekBar arg0)
{
}
#Override
public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
{
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
progress, 0);
}
});
}
catch (Exception e)
{
e.printStackTrace();
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
initControls();
initializeUIElements();
initializeMediaPlayer();
}
private void initializeUIElements() {
buttonPlay = (Button) findViewById(R.id.buttonPlay);
rewind = (Button) findViewById(R.id.rewind);
forward = (Button) findViewById(R.id.forward);
buttonPlay.setOnClickListener(this);
rewind.setOnClickListener(this);
forward.setOnClickListener(this);
}
public void onClick(View v) {
if (v == buttonPlay) {
if(!player.isPlaying())
{
Log.d(TAG,"Encender");
if(!paused) {
player.prepareAsync();
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
player.start();
}
});
}
else
player.start();
}
else
{
Log.d(TAG,"Pausar");
player.pause();
paused = true;
}
}
if(v==rewind){
Log.d(TAG,"RW");
if(player.isPlaying()){
//TODO: CODE FOR REWIND
}
}
if( v== forward){
Log.d(TAG,"FW");
if(player.isPlaying()){
//TODO: CODE FOR FORWARD
}
}
}
private void initializeMediaPlayer() {
player = new MediaPlayer();
try {
player.setDataSource("MYURL");
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
player.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
public void onBufferingUpdate(MediaPlayer mp, int percent) {
Log.i("Buffering", "" + percent);
}
});
}
#Override
protected void onPause() {
super.onPause();
if (player.isPlaying()) {
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void showOKDialog(Activity ac, String msg){
AlertDialog.Builder builder = new AlertDialog.Builder(ac);
builder.setMessage(msg)
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//do things
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
could you help me with this please?
anybody knows an appropriate way to do this?
Thanks!
I want to show custom Crouton alert with a progress bar and I want to update the progress dynamicly. This is my code but it doesn't work. How can i do that? Or how can show progress bar while image uploading
private void showCustomViewCrouton() {
// try {
View view = getLayoutInflater().inflate(R.layout.crouton_custom_view,null);
pB = (ProgressBar) view.findViewById(R.id.progressBar1);
final Crouton crouton;
final Configuration croutonConfig = new Configuration.Builder()
.setDuration(Configuration.DURATION_INFINITE)
.setInAnimation(android.R.anim.fade_in)
.setOutAnimation(android.R.anim.fade_out).build();
crouton = Crouton.make(this, view, R.id.croutonContainer, croutonConfig);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
crouton.hide(crouton);
}
});
crouton.show();
try{
do{
Thread.sleep(100);
deger+=1;
pB.setProgress(deger);
}while(deger<101);
}catch(Exception e){
}
}
Here is how I did it
final ProgressBar progressBar = new ProgressBar(this,null,android.R.attr.progressBarStyleHorizontal);
progressBar.setIndeterminate(false);
Crouton ct = Crouton.make(this,progressBar);
ct.show();
/* Update it While Showing */
Thread progressUpdateThread = new Thread(new Runnable()
{
#Override
public void run()
{
for(int i=0;i<100;i++)
{
final int progress = i;
runOnUiThread(new Runnable()
{
#Override
public void run()
{
progressBar.setProgress(progress);
}
});
try
{
Thread.sleep(100);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
});
progressUpdateThread.start();
i want to share my new code. I inflate a custom Crouton. I wrote this with using #Kirtan Patel ' s code. thanks him again.
private void showCustomViewCrouton() {
// try {
View view = getLayoutInflater().inflate(R.layout.crouton_custom_view,null);
pB = (ProgressBar) view.findViewById(R.id.progressBar1);
final Crouton crouton;
final Configuration croutonConfig = new Configuration.Builder()
.setDuration(Configuration.DURATION_INFINITE)
.setInAnimation(android.R.anim.fade_in)
.setOutAnimation(android.R.anim.fade_out).build();
crouton = Crouton.make(this, view, R.id.croutonContainer, croutonConfig);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
crouton.hide(crouton);
}
});
crouton.show();
try{
Thread progressUpdateThread = new Thread(new Runnable() {
#Override
public void run() {
for (int i = 0; i < 100; i++) {
final int progress = i;
runOnUiThread(new Runnable() {
#Override
public void run() {
pB.setProgress(progress);
}
});
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
progressUpdateThread.start();
}catch(Exception e){
}
}
My ProgressDialog will not show up. In my program the user selects bitrate from a pop up menu, the content is fetched online and displayed in VideoView.
However, all I get is a black screen while the video loads and the progressDialog is display for a split second (or less) at end before the video finally loads.
private void showPopupMenu(View v){
popupMenu = new PopupMenu(VideoPlayer.this, v);
if(bitRates != null){
int menuItem = 0;
int index = bitRates.size()-1;
popupMenu.getMenu().add(0,menuItem, 0,"Hide menu");
for(int i=1;i<bitRates.size();i++){
menuItem = i;
popupMenu.getMenu().add(0,menuItem, 0,"Quality"+" : "+bitrateCheck(bitRates.get(index))).setCheckable(true);
Log.i("ITEM",qualityList.get(i)+" : "+bitRates.get(i));
index--;
}
popupMenu.getMenu().add(0,menuItem++, 0,"Catalog");
}
popupMenu.show();
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(final MenuItem item) {
p_dialog = ProgressDialog.show(VideoPlayer.this, "", "Loading...");
//get current position of video to seek to
final int position = mVideoView.getCurrentPosition();
new Thread() {
public void run() {
try{
runOnUiThread(new Runnable() {
public void run() {
if(item.getTitle().equals("Catalog")){
backButtonVideo(new View(VideoPlayer.this));
}
else if(item.getTitle().equals("Hide menu")){
popupMenu.dismiss();
mVideoView.start();
}
else{
play(streamUrls.get(item.getItemId()),position);
}
}
});
}
catch (Exception e) {
Log.e("tag", e.getMessage());
}
p_dialog.dismiss();
}
}.start();
return true;
}
});
}
public void play(String _videoUri, int position){
Log.i(TAG,"URI is : "+Uri.parse(_videoUri).toString());
//View view = new View()
MediaController mc = new MediaController(VideoPlayer.this);
mVideoView.setMediaController(mc);
mVideoView.setVideoURI(Uri.parse(_videoUri));
mVideoView.requestFocus();
mVideoView.seekTo(position);
mVideoView.start();
}
////////////////////////Edit with new Code: as per suggestions/////////////////////
Now the dialog does not show at all.
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
mVideoView.start();
}
});
///////////////////////////////////////////////////////////////////////////////////
popupMenu.show();
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(final MenuItem item) {
//get current position of video to seek to
final int position = mVideoView.getCurrentPosition();
p_dialog = ProgressDialog.show(VideoPlayer.this, "", "Loading...");
try{
if(item.getTitle().equals("Catalog")){
backButtonVideo(new View(VideoPlayer.this));
}
else if(item.getTitle().equals("Hide menu")){
popupMenu.dismiss();
mVideoView.start();
}
else{
play(streamUrls.get(item.getItemId()),position);
}
}
catch (Exception e) {Log.e("tag", e.getMessage());}
return true;
}
});
}
public void play(String _videoUri, int position){
Log.i(TAG,"URI is : "+Uri.parse(_videoUri).toString());
MediaController mc = new MediaController(VideoPlayer.this);
mVideoView.setMediaController(mc);
mVideoView.setVideoURI(Uri.parse(_videoUri));
mVideoView.requestFocus();
mVideoView.seekTo(position);
p_dialog.dismiss();
}
Firstly, it makes no sense to create a separate thread only to run something back on the UI thread (why do you create the thread in the first place then?).
onMenuItemClick should already be called by the UI thread (callbacks for UI elements generally do this).
What you should look at is using a MediaPlayer.OnPreparedListener with VideoView.setOnPreparedListener. This will give you an indication of when the video is ready to be played.
I did some rearrangement as per Che Jami and got it to work. Thanks
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
p_dialog.dismiss();
mVideoView.start();
}
});
////////////////////////////////////////////////////////////////////////////////
popupMenu.show();
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(final MenuItem item) {
//get current position of video to seek to
final int position = mVideoView.getCurrentPosition();
try{
if(item.getTitle().equals("Catalog")){
backButtonVideo(new View(VideoPlayer.this));
}
else if(item.getTitle().equals("Hide menu")){
popupMenu.dismiss();
mVideoView.start();
}
else{
p_dialog = ProgressDialog.show(VideoPlayer.this, "", "Loading new stream");
play(streamUrls.get(item.getItemId()),position);
}
}
catch (Exception e) {Log.e("tag", e.getMessage());}
return true;
}
});
}
public void play(String _videoUri, int position){
Log.i(TAG,"URI is : "+Uri.parse(_videoUri).toString());
MediaController mc = new MediaController(VideoPlayer.this);
mVideoView.setMediaController(mc);
mVideoView.setVideoURI(Uri.parse(_videoUri));
mVideoView.requestFocus();
mVideoView.seekTo(position);
}
I have the same problem...i've tried you solution but my progress gets blocked...everything stops...and video doesn't show up...it seems that onPrepareListener is never called up...any ideas? Also it seems that setVideoURI has never passed in code...:/
I have different versions of code...with threads, with runOnUI, with AsyncTask, without everything...nothing seems to be working fully. something is always missing.
EDIT:
I have function that I'm calling in onClickListener
Here is a code like yours without any threads:
Version 1: Video starts after few seconds, progress dialog just show for a moment when video starts. Progress isn't showed from the beginning.
public void loadService(final int mode) {
//Version 1
// Show dialog
loadService = new ProgressDialog(activity);
loadService.setTitle("Loading service");
loadService.setMessage("Please wait...");
loadService.setCancelable(false);
loadService.show();
// Hide progress dialog after 15 seconds if nothing happens
DuoTVGuiHandler.progressBarProttection(loadService, 15);
// Setup video view for playback
if (mode == STAND_ALONE) {
setupVideoView(((DuoTVGuiActivity) activity).getVideoPlayback());
}
if (mode == WIDGET_MODE) {
setupVideoView(((MainActivity) activity).getVideoViewWidgetMode());
}
}
/** Setup video view for playback */
public void setupVideoView(final VideoView videoView) {
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
loadService.dismiss();
videoView.start();
}
});
videoView.setVideoURI(Uri.parse(channels.get(index).getFilePath()));
}
Version 2 with thread and handler...progress dialog is showed...its spinning...video shows for a second...and the app crashes...WrongThread for UI exception...but when i move setVideoURI in runOnUiThread...progress just stays forever...and setOnVideoURI is never finished.
public void loadService(final int mode) {
// //////////////////////////////
// Version 2
// //////////////////////////////
// Show dialog
loadService = new ProgressDialog(activity);
loadService.setTitle("Loading service");
loadService.setMessage("Please wait...");
loadService.setCancelable(false);
loadService.show();
final Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
loadService.dismiss();
super.handleMessage(msg);
}
};
Thread loadVideoPath = new Thread(new Runnable() {
public void run() {
if (mode == STAND_ALONE) {
((DuoTVGuiActivity) activity)
.getVideoPlayback()
.setVideoURI(
Uri.parse(channels.get(index).getFilePath()));
}
if (mode == WIDGET_MODE) {
((MainActivity) activity)
.getVideoViewWidgetMode()
.setVideoURI(
Uri.parse(channels.get(index).getFilePath()));
}
handler.sendEmptyMessage(0);
}
});
loadVideoPath.start();
}
Hope you have some idea...I've lost few days on this...and cant see solution...for this pretty basic thing...it drives me crazy :)
I wanted to display an Alert Dialogue inside thread or alternatively such a way that AlertDialogue opens up directly after finding some records in database negative, w/o clicking on any button..
Alert dialogue may contain Few lines and 2-3 buttons..
referred following link and tried on my own but not getting how to create an AlertDialogue without using onClickListener as its working with it(onClickListener) very smoothly:
http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog
Thanks in Advance.
public class jar_layut extends Activity {
/** Called when the activity is first created. */
boolean out=false;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new Thread(new Runnable()
{ public void run()
{
try {
out=first_check_pic_on_device();
mHandlerMySpace5.post(mUpdateResultsMySpace5);
} catch (Exception e) {
// TODO Auto-generated catch block
mHandlerMySpace5.post(mUpdateResultsMySpace5);
}
}
} ).start();
}
private boolean first_check_pic_on_device()
{
Context contextMySpace= this;;
try {
FileInputStream stream =contextMySpace.openFileInput("prf.png");
try {
return true;
} catch (Exception e) {
// TODO Auto-generated catch block
return false;
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
return false;
}
}
final Handler mHandlerMySpace5 = new Handler();
final Runnable mUpdateResultsMySpace5 = new Runnable() {
public void run() {
if(!out)
{
showDialog();
}
}
};
private void showDialog()
{
final CharSequence[] items = {"one", "two"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if(item==0)
{
}
if(item==1)
{
}
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
check out the below link
http://developer.android.com/guide/topics/ui/dialogs.html ---> expand(click) on "Example ProgressDialog with a second thread". This will solve your problem.