Progress Bar while media player is preparing
Asked Answered
D

6

6

I am trying to figure out how to have a progress bar that says "Loading. Please Wait..." while my media player prepares a streaming file. What occurs now is that it displays after the song is prepared. how can i fix this?

        mediaPlayerLoadingBar =ProgressDialog.show(PlaylistActivity.this, "", "Loading. Please wait...", true);         
                    /*dubstep stream*/
                    try {
                        dubstepMediaPlayer.setDataSource(dubstepPlaylistString[0]);
                        dubstepMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                        dubstepMediaPlayer.prepare();

                    } catch (IllegalArgumentException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (SecurityException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IllegalStateException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }


                    dubstepMediaPlayer.start();
                    if(dubstepMediaPlayer.isPlaying()){
                        mediaPlayerLoadingBar.dismiss();
                    }`

EDIT: This is the code I have now:

`switch(pSelection){ case 1:

                    new AsyncTask<Void, Void, Void>(){

                    @Override
                    protected void onPreExecute(){
                        mediaPlayerLoadingBar =ProgressDialog.show(PlaylistActivity.this, "", "Loading. Please wait...", true);
                        try {
                            dubstepMediaPlayer.setDataSource(dubstepPlaylistString[0]);
                        } catch (IllegalArgumentException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (IllegalStateException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        dubstepMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                    }

                    @Override
                    protected Void doInBackground(Void... params) {
                        // TODO Auto-generated method stub
                        //mediaPlayerLoadingBar =ProgressDialog.show(PlaylistActivity.this, "", "Loading. Please wait...", true);
                        return null;
                    }

                    protected void onPostExecute(Void result){
                        //mediaPlayerLoadingBar =ProgressDialog.show(PlaylistActivity.this, "", "Loading. Please wait...", true)
                            dubstepMediaPlayer.prepareAsync();
                        dubstepMediaPlayer.start();
                        mediaPlayerLoadingBar.dismiss();

                }

                }.execute();`
Daleth answered 25/6, 2011 at 1:0 Comment(0)
L
4

The issue lies in that you are not doing anything asynchronously here, and you should be. You should use an AsyncTask to do your work.

Take a look at 'the 4 steps', as detailed here:

The 4 steps

When an asynchronous task is executed, the task goes through 4 steps:

  1. onPreExecute(), invoked on the UI thread immediately after the task is executed. This step is normally used to setup the task, for instance by showing a progress bar in the user interface.
  2. doInBackground(Params...), invoked on the background thread immediately after onPreExecute() finishes executing. This step is used to perform background computation that can take a long time. The parameters of the asynchronous task are passed to this step. The result of the computation must be returned by this step and will be passed back to the last step. This step can also use publishProgress(Progress...) to publish one or more units of progress. These values are published on the UI thread, in the onProgressUpdate(Progress...) step.
  3. onProgressUpdate(Progress...), invoked on the UI thread after a call to publishProgress(Progress...). The timing of the execution is undefined. This method is used to display any form of progress in the user interface while the background computation is still executing. For instance, it can be used to animate a progress bar or show logs in a text field.
  4. onPostExecute(Result), invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter.

EDIT:

You can create an anonymous inner class to do your bidding, which may be similar to how you are creating your onClick handler. In your onClick do something like this:

//pseudo-code...
onClick(View v, ...) {
    new AsyncTask<Generic1, Generic2, Generic3>() {
        protected void onPreExecute() {
            // do pre execute stuff...
        }

        protected Generic3 doInBackground(Generic1... params) {
            // do background stuff...
        }

        protected void onPostExecute(Generic3 result) {
            // do post execute stuff...
        }
    }.execute();
}

Don't forget to keep an eye on your generics here!

Leukoderma answered 25/6, 2011 at 1:17 Comment(10)
ok i seem to understand, or at least i think i do, what you are saying. So where should i create this class. Because this code is inside an onCLick. does that extend AsyncTask or my main class?Daleth
so inside my on click just write MediaPlayerAsync onPreExecute etc. ??Daleth
yes and it is displaying the progress bar, but once the song starts playing. in other words it is still doing the same thing as before.Daleth
my on generic's currently say Void. what should i replace the generics with?Daleth
on average it takes about 3-5 seconds before the song starts to play. but the user should know that the song is loadingDaleth
Well, it is going to be difficult to diagnose the problem from 10,000 feet. Perhaps you should update your question with the new details since you have made updates...Leukoderma
I have updated the code. it displays the progress bar before the song starts playing at cancels once the song is playing (which is exactly what should happen). Now, when i press the button to pause it, the application force closes. any ideas? and thanks for your help by the way. This is my sixth week of android programming so i am still learningDaleth
Since it sounds like your question has been answered, perhaps you should close this one and ask another...Leukoderma
well it seems like the .execute is messing up the rest of my code. anyway i can cancel the async task after it runs once?Daleth
will i play my song in on background ?Neeley
P
5

If someone Still facing this problem here is the code below

      AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {

            @Override
            protected void onPreExecute() {
                if(translation.equals("NIV"))
                {
                    if(AudioPlaying==false)
                    {
                        mediaPlayer = new MediaPlayer();
                        mediaPlayer.setOnPreparedListener(Main.this);
                        mediaController = new MediaController(Main.this);
                    }
                    else
                        mediaController.show();
                }
                else
                    Toast.makeText(getBaseContext(), "عفوا, جاري تحميل ملفات الصوت الخاصة بترجمة الفانديك ", Toast.LENGTH_LONG).show();
                pd = new ProgressDialog(Main.this);
                pd.setTitle("Processing...");
                pd.setMessage("Please wait.");
                pd.setCancelable(false);
                pd.setIndeterminate(true);
                pd.show();

            }

            @Override
            protected Void doInBackground(Void... arg0) {
                try {
                    //Do something...
                    //Thread.sleep(5000);
                    try 
                    {
                        mediaPlayer.setDataSource(AudioUrlPath);
                        mediaPlayer.prepare();
                        mediaPlayer.start();
                        AudioPlaying=true;
                    } 
                    catch (IOException e) {
                        Log.e("AudioFileError", "Could not open file " + AudioUrlPath + " for playback.", e);
                    }

                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(Void result) {
                if (pd!=null) {
                    pd.dismiss();
                    //b.setEnabled(true);
                }
            }

        };
        task.execute((Void[])null);
Predigestion answered 10/1, 2014 at 16:55 Comment(1)
You made me earn respect Brother (For me Freelancing task) :pPfosi
L
4

The issue lies in that you are not doing anything asynchronously here, and you should be. You should use an AsyncTask to do your work.

Take a look at 'the 4 steps', as detailed here:

The 4 steps

When an asynchronous task is executed, the task goes through 4 steps:

  1. onPreExecute(), invoked on the UI thread immediately after the task is executed. This step is normally used to setup the task, for instance by showing a progress bar in the user interface.
  2. doInBackground(Params...), invoked on the background thread immediately after onPreExecute() finishes executing. This step is used to perform background computation that can take a long time. The parameters of the asynchronous task are passed to this step. The result of the computation must be returned by this step and will be passed back to the last step. This step can also use publishProgress(Progress...) to publish one or more units of progress. These values are published on the UI thread, in the onProgressUpdate(Progress...) step.
  3. onProgressUpdate(Progress...), invoked on the UI thread after a call to publishProgress(Progress...). The timing of the execution is undefined. This method is used to display any form of progress in the user interface while the background computation is still executing. For instance, it can be used to animate a progress bar or show logs in a text field.
  4. onPostExecute(Result), invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter.

EDIT:

You can create an anonymous inner class to do your bidding, which may be similar to how you are creating your onClick handler. In your onClick do something like this:

//pseudo-code...
onClick(View v, ...) {
    new AsyncTask<Generic1, Generic2, Generic3>() {
        protected void onPreExecute() {
            // do pre execute stuff...
        }

        protected Generic3 doInBackground(Generic1... params) {
            // do background stuff...
        }

        protected void onPostExecute(Generic3 result) {
            // do post execute stuff...
        }
    }.execute();
}

Don't forget to keep an eye on your generics here!

Leukoderma answered 25/6, 2011 at 1:17 Comment(10)
ok i seem to understand, or at least i think i do, what you are saying. So where should i create this class. Because this code is inside an onCLick. does that extend AsyncTask or my main class?Daleth
so inside my on click just write MediaPlayerAsync onPreExecute etc. ??Daleth
yes and it is displaying the progress bar, but once the song starts playing. in other words it is still doing the same thing as before.Daleth
my on generic's currently say Void. what should i replace the generics with?Daleth
on average it takes about 3-5 seconds before the song starts to play. but the user should know that the song is loadingDaleth
Well, it is going to be difficult to diagnose the problem from 10,000 feet. Perhaps you should update your question with the new details since you have made updates...Leukoderma
I have updated the code. it displays the progress bar before the song starts playing at cancels once the song is playing (which is exactly what should happen). Now, when i press the button to pause it, the application force closes. any ideas? and thanks for your help by the way. This is my sixth week of android programming so i am still learningDaleth
Since it sounds like your question has been answered, perhaps you should close this one and ask another...Leukoderma
well it seems like the .execute is messing up the rest of my code. anyway i can cancel the async task after it runs once?Daleth
will i play my song in on background ?Neeley
S
1

Here is the activity class.Here i am showing the way only.

package com.android.mediaactivity;

import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

    public class MediaActivity extends Activity 
    {
        public LinearLayout mainLayout;
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            mainLayout=(LinearLayout)findViewById(R.id.mainlinear);
            MediaPlayer media=new MediaPlayer(this);
            media.startPlayer();
        }
    }

Here is mediaplayerclass.

package com.android.mediaactivity;

import java.io.IOException;

import android.media.MediaPlayer.OnPreparedListener;

public class MediaPlayer implements OnPreparedListener {
    MediaActivity mediaActivity;
    android.media.MediaPlayer mediaPlayer;
    public MediaPlayer(MediaActivity mediaActivity) {
        this.mediaActivity = mediaActivity;
    }
    public void startPlayer() {
        mediaPlayer = new android.media.MediaPlayer();
        mediaPlayer.setOnPreparedListener(this);
        mediaPlayer.reset();
        try {
            mediaPlayer.setDataSource("");
            mediaPlayer.prepareAsync();
            toggleProgress(true);
        } catch (IllegalArgumentException e) { // TODO Auto-generated catch block          e.printStackTrace();        } catch (IllegalStateException e) {             // TODO Auto-generated catch block          e.printStackTrace();        } catch (IOException e) {           // TODO Auto-generated catch block          e.printStackTrace();        }   }   public void onPrepared(android.media.MediaPlayer mp)    {       toggleProgress(false);      mediaPlayer.start();    }
            public void toggleProgress(final boolean show) {
                mediaActivity.runOnUiThread(new Runnable() {
                    public void run() {
                        if (show) mediaActivity.mainLayout.setVisibility(mediaActivity.mainLayout.VISIBLE);
                        else mediaActivity.mainLayout.setVisibility(mediaActivity.mainLayout.INVISIBLE);
                    }
                });
            }

        }
    }
}

And here is the main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:id="@+id/mainlinear"
    android:visibility="invisible">

    <ProgressBar android:id="@+id/ProgressBar01"
        android:layout_width="wrap_content" android:layout_height="wrap_content"></ProgressBar>
</LinearLayout>
Sweated answered 25/6, 2011 at 4:54 Comment(2)
Ok. I will try again, and let you knowDaleth
Ok, so the code you provided me does work. Now will I be able to put this onto my original code above and still work?Daleth
E
0

When you say prepare on the underlying mediaplayer object, internally it really does some preparation like - setting up the extractor for the file, setting up the audio decoder to decode the encoded audio file and setting up the audio sink to play the raw audio data that was decoded from the decoder. Now all this will take time, it is not instantaneous.

So in your original code, you check if the mediaplayer isPlaying and then dismiss it but the problem is at that point of time the mediaplayer is not playing the audio yet and thus your dismiss is never called so it always visible.

What you need to do is implement the listener MediaPlayer.OnPreparedListener and when the method onPrepared is called in your application call the dismiss mediaPlayerLoadingBar.dismiss(); in that method.

Ebneter answered 27/6, 2011 at 6:54 Comment(0)
I
0

Here is my solution : The prepareAsync function used to prepare the audio and it is a non blocking operation (it is not blocking the main thread of the app).

Then I used the callback setOnPreparedListener to get notified when the prepareAsync return, and the audio is ready

public void playAudio(String audioFile){


//init the progress dialog
  final ProgressDialog progressDialog = new ProgressDialog(SubjectActivity.this);

    try {

         progressDialog.setCancelable(false);
         progressDialog.setMessage(" Waiting to Prepare ...");
         progressDialog.show();

// pass the url file to the media player
        mediaplayer.setDataSource(audioFile);
        mediaplayer.prepareAsync();


    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
//the callback gets called when prepareAsync audio file become ready, 
    mediaplayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            mediaplayer.start();
// cancel the dialog
            progressDialog.cancel();
        }
    });


}
Igor answered 6/3, 2019 at 17:42 Comment(1)
Welcome to SO, this answer is low quality, you have to provide explanation with your code, how you managed to solve this problem.Cicisbeo
N
0

Finally found out: Guys very Imp point: After setting url

  try {
            mediaPlayer.setDataSource(url);
        } catch (IOException e) {
            e.printStackTrace();
        }

I have added prepare_sync() and added handler cause it was crashing for big files

new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        mediaPlayer.prepareAsync();
        ProgressDialog progressDialog = ProgressDialog.show(Player.this,
                "Loading Title", "Loading Message");
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

            @Override
            public void onPrepared(MediaPlayer mp) {
                if (progressDialog != null && progressDialog.isShowing()){
                    progressDialog.dismiss();

                }
            }
        });
        // might take long! (for buffering, etc)
        mediaPlayer.start();

    }
},500);

This code will add progressbar too.

Points:

  1. set url
  2. prepareasync
  3. OnPreparedlistener Progressbar

mp.start-done

This is my way of doing btw I have heard there is alternative Exoplayer have a look at that too !

New answered 16/7, 2020 at 12:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.