Android loading animation before VideoView start - android

I don't find any solution to show a loading animation (or just an imageView) before a VideoView start to play : during the video is buffering.
Does anyone have a clue ?
Thanks.

Or you can just use Progress Dialog;
public class VideoEkrani extends Activity {
VideoView ekran;
String kaynakYolu = "http://commonsware.com/misc/test2.3gp";
ProgressDialog progDailog;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sayfa_video_goruntule);
ekran = (VideoView) findViewById(R.id.videoViewESPU);
ekran.setMediaController(new MediaController(this));
ekran.setVideoURI(Uri.parse(kaynakYolu));
ekran.requestFocus();
ekran.start();
progDailog = ProgressDialog.show(this, "Please wait ...", "Retrieving data ...", true);
ekran.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
progDailog.dismiss();
}
});
}
}

Finally I found the method setOnPreparedListener() on VideoView.
I added a ProgressBar inside my layout, and hide it with the OnPreparedListener

You cant detect the video buffer using OnBufferingUpdateListener on MediaPlayer. If you use VideView set that listener inside OnPreparedListener.
videoview.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mp.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
#Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
if(percent == 100){
//video have completed buffering
}
}
});
}
});

Related

Video Loop when it comes to the end [duplicate]

This question already has answers here:
Android: Video View: how to play a video on a loop
(2 answers)
Closed 5 years ago.
How to make the video to loop seamlessly as of now when it comes to an end of the clip and starts the clip again
`public class TvPlay extends Activity implements OnCompletionListener,
OnErrorListener, OnPreparedListener {
private VideoView mVideoView;
private String url;
private ProgressBar load;
private TextView empty;
private AdView mAdView;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// Vitamio.isInitialized(this);
Vitamio.isInitialized(getApplicationContext());
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.tvplay);
mAdView = (AdView) findViewById(R.id.adView);
mAdView.loadAd(new AdRequest.Builder().build());
Log.d("url=", getIntent().getStringExtra("url"));
url = getIntent().getStringExtra("url");
init();
}
public void init() {
load = (ProgressBar) this.findViewById(R.id.load);
empty = (TextView) this.findViewById(R.id.empty);
mVideoView = (VideoView) this.findViewById(R.id.surface_view);
mVideoView.setOnCompletionListener(this);
mVideoView.setOnPreparedListener(this);
mVideoView.setOnErrorListener(this);
Uri videoUri = Uri.parse(url);
mVideoView.setVideoURI(videoUri);
mVideoView.requestFocus();
loading();
}
private void loading() {
load.setVisibility(View.GONE);
empty.setVisibility(View.GONE);
}
private void loadComplete(MediaPlayer arg0) {
load.setVisibility(View.GONE);
// vv.setVisibility(View.VISIBLE);
empty.setVisibility(View.GONE);
mVideoView.start();
mVideoView.resume();
}
private void error(String msg) {
load.setVisibility(View.GONE);
mVideoView.setVisibility(View.GONE);
empty.setVisibility(View.VISIBLE);
if (msg != null)
empty.setText(msg);
}
#Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
Log.d("ONLINE TV", "Prepared");
loadComplete(mp);
}
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
// TODO Auto-generated method stub
Log.d("ONLINE TV", "Error");
error("Unable to play this channel.");
return false;
}
#Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
Log.d("ONLINE TV", "Complete");
}
}
`
this will make your video keep playing
mVideoView.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.setLooping(true);
}
});

Text Changes back to original when sound is finished playing?

I've created this simple soundboard app which plays a sound on click. I've also setup so that the text changes when the button is hit but is there anyway for the text to change back to its original when the sound file is finished playing
Thanks in advance!
MediaPlayer mediaPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button aussi = (Button) findViewById(R.id.aussi);
aussi.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(aussi.getText().toString().equals("Australian")){
try{
}catch (Exception e){
e.printStackTrace();
}
aussi.setText("Jay");
mediaPlayer = MediaPlayer.create(MainActivity.this,
R.raw.aussi);
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer play) {
// TODO Auto-generated method stub
play.release();
}
});
}
}
});
You got the solution by yourself, the completion listener tells you when the sound finishes playing. so :
#Override
public void onCompletion(MediaPlayer play) {
play.release();
// Now you can set you text
aussi.setText("your original text");
}
});

Android - Where to implement onBufferingUpdate(...) using VideoView and MediaController

I am confused by the MediaController and VideoView APIs in Android.
I am using it to stream videos through HTTP, which is working fine. Only problem is that it stops for buffering from time to time. I now want to get notified whenever the video starts to buffer to display a progress indicator.
I learned that I need to implement public abstract void onBufferingUpdate (MediaPlayer mp, int percent) from android.media.MediaPlayer.OnBufferingUpdateListener, my question is just where do I have to implement it? I followed the advice from another SO question and implemented:
videoView.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
Log.d(Constants.LOG, "onPrepared called");
mp.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
#Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
//do something
Log.d(Constants.LOG, "Buffering update: " + percent + "%");
}
});
}
But it didn't work... Do I have to subclass VideoView and implement it there??
Works:
VideoView myVideoView = (VideoView)this.findViewById(R.id.video_view);
myVideoView.setOnPreparedListener(new OnPreparedListener()
{
#Override
public void onPrepared(MediaPlayer mp)
{
myVideoView.start();
if ( dialog != null )
dialog.dismiss();
mp.setOnBufferingUpdateListener(new OnBufferingUpdateListener()
{
#Override
public void onBufferingUpdate(MediaPlayer mp, int percent)
{
Log.d("URL", "Buffering update: " + percent + "%");
}
});
}
});
Dialog dialog = new ProgressDialog(VideoActivity.this);
dialog.setMessage("Loading ...");
dialog.setCancelable(true);
dialog.setInverseBackgroundForced(false);
myVideoView.setVideoURI( Uri.parse( "http://..." ) );

Playing video in videoview android

I am trying to play a video in a video view. The xml code contains the video view as:
<VideoView
android:id="#+id/vvIntroVideo"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="38" />
and in the Java part of code I have:
public class LoginPage extends Activity {
private VideoView introVideoView;
private static String DEBUG = LoginPage.class.getSimpleName();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_page);
//initializing part
introVideoView = (VideoView) findViewById(R.id.vvIntroVideo);
try {
introPlayer();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.v(DEBUG, e.getMessage().toString());
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.login_page, menu);
return true;
}
public void introPlayer(){
Uri video = Uri.parse("android:resource://"+getPackageName()+"/"+R.raw.documentariesandyou);
introVideoView.setVideoURI(video);
introVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.setLooping(true);
}
});
introVideoView.start();
}
}
The application shows the error that the video cannot be played. The video is in MP4 so there might not be problem in that. Can anyone help me in this case.
First define ProgressDialog mProgressDialog;
Then try this code,it worked for me:
video = (VideoView) findViewById(R.id.VideoView1);
final MediaController mc = new MediaController(this);
mc.setAnchorView(video);
mc.setMediaPlayer(video);
Uri uri = Uri.parse(path1);
video.setMediaController(mc);
System.out.println("!-- uri: "+uri.toString());
video.setVideoURI(uri);
video.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mProgressDialog.dismiss();
}
});
video.start();
video.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
}
});
}
public void showLoaderProgress(){
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("Loading Video\nPlease wait...");
mProgressDialog.setIndeterminate(true);
mProgressDialog.setCancelable(false);
mProgressDialog.show();
}
where path1 is your video path.
As you are a beginer i am giving you code to play video in videoview,this will play both videos from raw folder as well from server url with live streaming. to play local just load URI to vieoview rest all thing are same.its very easy to understand.i hope it will help you.
globals in activity
private MediaController controller;
ProgressBar progressBar = null;
private String _url;
Oncreate
VideoView video = (VideoView) findViewById(R.id.videoView1);
controller = new MediaController(VideoScreen.this);
video.setMediaController(controller);
video.setVideoPath(_url);
video.requestFocus();
video.start();
progressBar.setVisibility(View.VISIBLE);
video.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.start();
progressBar.setVisibility(View.GONE);
mp.setOnVideoSizeChangedListener(new OnVideoSizeChangedListener() {
#Override
public void onVideoSizeChanged(MediaPlayer mp, int arg1,
int arg2) {
progressBar.setVisibility(View.GONE);
mp.start();
}
});
}
});
I found the solution. Actually the problem was in encoding of video. I was using gingerbread emulator to test application and the video was in mp4 h.264. But it seems gingerbread is capable in playing mp4 or 3gpp h.263 only in default.

How to display progress dialog with media player ?

There is a problem in the following media player that plays the audio stream when you press play if the stream is not available there is no any response from the program, How can I solve this issue, try to make progress dialog, but the solution does not work, maybe I did something wrong, but when you click on the progress bar appears first and then immediately disappears without waiting to start playback of music.
setDataSource filed in advance a broken link, nothing has changed.
Code media player with progress dialog:
private class ProgressTask extends AsyncTask <Void,Void,Void>{
#Override
protected void onPreExecute(){
progressbar.setVisibility(View.VISIBLE);
}
#Override
protected Void doInBackground(Void... arg0) {
preload();
return null;
}
#Override
protected void onPostExecute(Void result) {
MPplay();
progressbar.setVisibility(View.GONE);
}
}
public void preload() {
releaseMP();
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
if(pdaStream.isChecked()){
Main_stream = "http://145.255.233.204:58000/radio_pda";
}else{
Main_stream = "http://145.255.233.204:58000/radio";
}
mediaPlayer.setDataSource(Main_stream);
} catch (IOException e) {
e.printStackTrace();
}
Log.d(LOG_TAG, "prepareAsync");
mediaPlayer.prepareAsync();;
}
public void MPplay(){
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
}
If you want to see when a video is ready for playback, use this callback. http://developer.android.com/reference/android/media/MediaPlayer.OnPreparedListener.html
onPrepared(MediaPlayer mp)
//Called when the media file is ready for playback.
If you would like to see when a video is 100% buffered, use this callback: http://developer.android.com/reference/android/media/MediaPlayer.OnBufferingUpdateListener.html
public abstract void onBufferingUpdate (MediaPlayer mp, int percent)

Categories

Resources