How to show progress bar until video play in live stream Android? - java

i am working on an application where i need to play video from a remote server as live stream.
which is done by me successfully.
i managed every thing in my app.
but when video is loading i need to show a progress dialog over VideoView.
i tried using OnPreparedListener as "how to show the progress bar before playing the video"
#Override
public void onPrepared(MediaPlayer mp) {
progressbar.setVisibility(View.GONE);
mp.start();
}
but video play after 5-7 Sec of progressbar gone.
i searched a lot on Google but not found any solution for it.
Could anyone help me.
Thanks in Advance.

have a look this one solved my problem hope help you also..
http://www.quicktips.in/how-to-show-progressbar-while-loading-a-video-in-android-videoview/
progressbar.setVisibility(View.VISIBLE);
videoView.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
mp.setOnVideoSizeChangedListener(new OnVideoSizeChangedListener() {
#Override
public void onVideoSizeChanged(MediaPlayer mp, int arg1, int arg2) {
// TODO Auto-generated method stub
Log.e(TAG, "Changed");
progressbar.setVisibility(View.GONE);
mp.start();
}
});
}
});
:)

Unforgettably there is no OnStart Listener expose by Android. After Prepared State, the MediaPlayer goes into Started State once playback started.
But in Prepared State only you should have sufficient buffer to play. Can you use setOnBufferingUpdateListener (MediaPlayer.OnBufferingUpdateListener listener) and print percentage of buffer. So that way you can see, if in Prepared State you have enough buffer or not.

Related

mediaPlayer.release() crashes my app java

I have a project where I added a MediaPlayer in a custom Array Adapter and I make it take the Resource ID of the audio file from the Array List the problem is that I want to release the media player after the audio completes but every time I try to add release in OnCompletion and try to press a view while the media player plays a sound the app crashes .. I searched online and found a code but it gives me an error and I can't find the actual reason for such a crash.
that's the code for the on Click Listener and the Media player:
final MediaPlayer mediaPlayer = MediaPlayer.create(getContext(), currentWord.getVoiceResourceID());
// Wait for user's input by his click on the list's item to play the sound.
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
// Storing the return value of getVoiceResourceID() into mVoice.
// I think this line can be ignored so we call the method inside the creation of the Media Player.
// Creating our media player and put our track on it.
mediaPlayer.selectTrack(currentWord.getVoiceResourceID());
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mediaPlayer.release();
}
});
}
};
I hope I could fix that problem.

Pause Media Player in case of phone call or user open a video or an audio in another program

I want to pause media player in case the user use another program to watch video
or the user receive a phone call like any multimedia app (YouTube ,...)
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.song);
mediaPlayer.start();
thanks
You need to override android methods which relate to the lifecycle of an android application
#Override
protected void onStop() {
super.onStop();
mediaPlayer.stop(); //put your stop criteria here
}
#Override
public void onPause() {
super.onPause();
mediaPlayer.pause(); //put your pause criteria here
}
#Override
public void onResume() {
super.onResume();
mediaPlayer.play(); //put your resume criteria here
}
you can simply override these methods to do your job.
MediaPlayer can be implemented as a service. Pause the service when the event happens using the BroadcastReceiver and you can again resume it using that. It would work in this manner.
You have to use AudioFocus (https://developer.android.com/guide/topics/media-apps/audio-focus). In this way you will receive an Event when another App is taking "the ownership" of Audio Device to play its content/audio and then another Event will be raised when that App stops to play it and the "ownership" return to your App.

freeze android app when playing music of server

I have an online music player and my problem is when user press on play button
my app freeze for 1 or 2 second(network speed).
I play music of a link of my server and put it in Voice variable and play stream that.
whats the problem of my code?
Thanks.
My code:
detail_voice.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar snackbar = Snackbar.make(detail_date, " " + "دارم میگیرررررررررمش :| :)))", Snackbar.LENGTH_SHORT);
snackbar.show();
try {
mp.setDataSource(Voice);
mp.prepare();
mp.start();
detail_voice.setVisibility(View.GONE);
detail_voice_stop.setVisibility(View.VISIBLE);
} catch (IOException e) {
e.printStackTrace();
}
}
});
detail_voice_stop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
detail_voice.setVisibility(View.VISIBLE);
detail_voice_stop.setVisibility(View.GONE);
mp.stop();
mp.reset();
detail_voice_stop.setVisibility(View.GONE);
}
});
The code you've provided is not extensive, but my guess is your problem might indeed have to do with loading your data over the network. Usually when loading data over a network, you'll want to do that asynchronously to avoid the program needlessly waiting for the data, and freeze during that time.
The MediaPlayer class has a prepareAsync() method which will do this for you. But, you cannot put the start() method below that anymore as it is now asynchronous. Try replacing this code:
mp.setDataSource(Voice);
mp.prepare();
mp.start();
With this:
mp.setDataSource(Voice);
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer player) {
player.start();
}
});
mp.prepareAsync();
If that code solves the problem, your connection was indeed the problem. You should not however use the setOnPreparedListener() method every time you click. I'd suggest moving it outside the onClickListener() method so it is only executed once.
This Stackoverflow question has a good implementation too. It might be worth a read as well.

Background Mode vs Turned Off Mode

I made a video chat app on android. When I'm in the middle of a video chat, and I move the app to background mode, the video chat pauses. When I move it back to foreground mode, the video chat resumes. This is the desired behavior. However, when I'm in the middle of a video chat, and I press the power button to turn off the screen, the video chat continues. I want turning off the screen to behave just like background mode. Any ideas?
You can pause the video chat when the app goes in onPause().
i.e add your video chat pause logic in the overridden onPause().
Try following code with your default videoview
#Override
public void onPause() {
Log.d(TAG, "onPause called");
super.onPause();
stopPosition = videoView.getCurrentPosition(); //stopPosition is an int
videoView.pause();
}
#Override
public void onResume() {
super.onResume();
Log.d(TAG, "onResume called");
videoView.seekTo(stopPosition);
videoView.start(); //Or use resume() if it doesn't work. I'm not sure
}

How to handle runtime exception on playing audio files?

I have a button that plays an audio file on its click listener. If the button is clicked again and again while the audio file is being played then the app crashes. What's the solution?
Here is some code for reference:
private OnClickListener btnMercyListener = new OnClickListener()
{
public void onClick(View v)
{
// Toast.makeText(getBaseContext(),
// "Mercy audio file is being played",
// Toast.LENGTH_LONG).show();
if (status==true)
{
mp.stop();
mp.release();
status = false;
}
else
{
mp = MediaPlayer.create(iMEvil.this,R.raw.mercy);
//mp.start();
try{
mp.start();
status= true;
//mp.release();
}catch(NullPointerException e)
{
Log.v("MP error",e.toString());
}
}
mp.setOnCompletionListener(new OnCompletionListener(){
// #Override
public void onCompletion(MediaPlayer arg0) {
mp.release();
status = false;
}
}
);
}
};
Two things:
1. Debug the crash and see where it's failing (which line).
2. Surround the whole statement with a try/catch and simply catch an Exception.
If you have an exception or a better idea where your code is failing, then it will be much easier to give you advice on how to fix it... as a matter of fact, you might not even need advice to fix it, you might end up solving the problem by yourself and then you will reap the fruits of your own success.
Update per comments:
The documentation for MediaPlayer indicates what might be the problem given the symptoms the OP is seeing:
To stop playback, call stop(). If you wish to later replay the media, then
you must reset() and prepare() the MediaPlayer object before calling
start() again. (create() calls prepare() the first time.)
It looks like if the play button is pressed too many times, then the media may end up not being in the prepared state and thus throw some exception. The idea of disabling the play button is valid and it should take care of this situation.
Here is some illustrative code on what you want your program to do:
private OnClickListener btnMercyListener = new OnClickListener()
{
public void onClick(View v)
{
if(isPressed)
{
return;
}
isPressed = true;
// create your media player
mp = MediaPlayer.create(iMEvil.this,R.raw.mercy);
// set your listener
mp.setOnCompletionListener(mp.setOnCompletionListener(new OnCompletionListener(){
// #Override
public void onCompletion(MediaPlayer arg0) {
if(!isPressed)
{
return;
}
isPressed = false;
// re-enable your play button
playButton.enable();
// disable the pause button
pauseButton.disable();
mp.release();
mp.prepare();
}
}
);
// disable the play button
playButton.disable();
// enable the pause button
pauseButton.enable();
// start playback
mp.start();
}
};
Of course you should have the appropriate try/catch statements in there so your app doesn't crash, but this code should give you a general idea of what to do.

Categories