I'm using Video view to stream an HLS stream from wowza media server. I want to show the overall buffering just as in YouTube player, what i did is this and I'm able to access seek-bar object and i debugged the code it's not null
videoView.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
int topContainerId = getResources().getIdentifier("mediacontroller_progress", "id", "android");
seekbar = (SeekBar) mediaController.findViewById(topContainerId);
mp.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
#Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
if (percent<seekbar.getMax()) {
seekbar.setSecondaryProgress(percent);
}
}
});
}
});
The problem is i can't see any secondary progress in Media controller seekbar. If any clarification required required please write in comments. Help is really appreciated.
Use below code and you're goog to go
videoView.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
int topContainerId = getResources().getIdentifier("mediacontroller_progress", "id", "android");
seekbar = (SeekBar) mediaController.findViewById(topContainerId);
mp.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
#Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
if (percent<seekbar.getMax()) {
seekbar.setSecondaryProgress(percent/100);
}
}
});
}
});
Related
I am trying to implement video player in Android using VideoView. I have video uploaded on server I am fetching video from the server and trying to play in VideoView. I have fetched video successfully but unable to play in player.
Below is my code:
MediaController mediacontroller = new MediaController(getActivity());
mediacontroller.setAnchorView(videoView);
videoView.setMediaController(mediacontroller);
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
videoView.setVideoURI(Uri.parse(video_url));
videoView.start();
}
});
videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Toast.makeText(getActivity(),"Error",Toast.LENGTH_SHORT).show();
return false;
}
});
What am I doing wrong?
You need to set video path url also before setting media controller to videoview like this
Uri uri = Uri.parse(path);
videoView.setVideoURI(uri);
videoView.start();
Example
videoView.setVideoURI(Uri.parse(video_url));
videoView.start();
MediaController mediacontroller = new MediaController(getActivity());
mediacontroller.setAnchorView(videoView);
videoView.setMediaController(mediacontroller);
ProgressBar progressBar =findViewById(R.id.progressbar);
progressBar.setVisibility(View.VISIBLE);
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
}
});
videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Toast.makeText(getActivity(),"Error",Toast.LENGTH_SHORT).show();
return false;
}
});
videoView.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.start();
mp.setOnVideoSizeChangedListener(new OnVideoSizeChangedListener() {
#Override
public void onVideoSizeChanged(MediaPlayer mp, int arg1,
int arg2) {
// TODO Auto-generated method stub
progressBar.setVisibility(View.GONE);
mp.start();
}
});
}
});
Add this progressbar widget in xml like this
<ProgressBar
android:id="#+id/progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="gone" />
Update:- Added progress bar while playing video
I'm trying to return the current state of the media player because I want to check that the media player has not loaded a song yet so I can provide a popup to choose an audio file.
Code in other class
public Sound(int buttonID, Activity _activity) {
super();
this.activity = _activity;
button = (Button) activity.findViewById(buttonID);
mp = new MediaPlayer();
}
Code to check if an audio file is loaded
sound.button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(//no audio file loaded){
Intent intent = new Intent(MainActivity.this, Pop.class);
startActivityForResult(intent, PICK_AUDIO_REQUEST);
}
else if(sound.mp.isPlaying()) {
sound.mp.pause();
}
else{
sound.mp.seekTo(0);
sound.mp.start();
}
}
});
Using the answer, this is the code I came up with:
sound.button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
sound.mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
isPrepared = true;
}
});
if(!isPrepared){
Intent intent = new Intent(MainActivity.this, Pop.class);
startActivityForResult(intent, PICK_AUDIO_REQUEST);
}
else if(sound.mp.isPlaying()) {
sound.mp.pause();
}
else{
sound.mp.seekTo(0);
sound.mp.start();
}
}
});
Perhaps the following could help...
MediaPlayer.setOnBufferingUpdateListener(...);
public void onBufferingUpdate(MediaPlayer mp, int percent)
{
//while MediaPlayer's buffer been update
}
MediaPlayer.setOnPreparedListener(...);
public void onPrepared(MediaPlayer mp)
{
//while MediaPlayer is ready to play
}
MediaPlayer.setOnCompletionListener(...);
public void onCompletion(MediaPlayer mp)
{
//while MediaPlayer played to the end
}
MediaPlayer.setOnErrorListener(...);
public boolean onError(MediaPlayer mp, int what, int extra)
{
//while MediaPlayer has error ocurred
}
Also, your class must implement OnAudioFocusChangeListener, and create the following method
public boolean requestAudioFocus()
{}
public boolean abandonAudioFocus()
{}
public void onAudioFocusChange(int focusChange)
{}
You may considered to put your MediaPlayer into a Service class.
I'm afraid that there is no such method. You have to check it yourself. It's not too hard to follow MeidaManager state with the MediaPlayer's StateDiagram. The easiest way is to declare a boolean flag isPrepared / isReadyToPlay and set it to true in onPrepared() callback of the OnPreparedListener.
i am displaying images and video in imageview and videoview but the issue is when video is
playing onpreparedlistener called but when video finish oncompletion listener not called
when videoview complete i increment the i for next video or images
also it gives me error in logcat like this but video is playing
10-29 20:12:47.770: E/MediaPlayer(3975): error (1, -2147483648)
private void nextVideo(String path){
mImageview.setVisibility(View.GONE);
if(mVideoview.getVisibility()==View.GONE){
mVideoview.setVisibility(View.VISIBLE);
}
controller = new MediaController(HomeActivityNewViewPager.this);
mVideoview.setVideoURI(Uri.parse(path));
mVideoview.setMediaController(null);
controller.setMediaPlayer(mVideoview);
mVideoview.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mVideoview.start();
long duration = mVideoview.getDuration();
second=duration;
//handler.removeCallbacks(runnable);
//handler.postDelayed(runnable,second);
}
});
mVideoview.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
Log.v("video view completed","---"+i);
mp.reset();
if(automode){
if(i==myplaylistlocal.size() || i>myplaylistlocal.size())
{
String checkcount=spreferences.getString("roundcount", "");
Log.v("roundcount==Before Integer.parseInt","---->"+roundcount);
if(roundcount>=Integer.parseInt(checkcount))
{
roundcount=0;
Log.v("roundcount==After Integer.parseInt","---->"+roundcount);
updateplaylist();
}
i=0;
indexplus();
imagesautomode();
i++;
}
else if(i==myplaylistlocal.size()-1)
{
imagesautomode();
i++;
}
else{
imagesautomode();
}
}
else{
i++;
images();
}
}
});
mVideoview.setOnErrorListener(new OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Log.v("Error in video playing","----->"+i);
return true;
}
});
}
Either way, the error referenced above is MEDIA_ERROR_UNKNOWN. If this video was made for this app, I would make sure that it is properly encoded for Android. Also make sure that is clearly defines its endpoint.
http://developer.android.com/reference/android/media/MediaPlayer.html#MEDIA_ERROR_UNKNOWN
this is a work around but could possbly work in your situation:
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
if(what == MediaPlayer.MEDIA_ERROR_UNKNOWN)
//ERROR UNKNOWN - COULD BE IMPROPERLY FORMATTED VIDEO {
//MOVE ON TO NEXT VIDEO
//DO LOGGING
}
}
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://..." ) );
i have the following code:
final SeekBar videoBar = (SeekBar) findViewById(R.id.videoBar);
final VideoView videoView = (VideoView) findViewById(R.id.videoViewPaint);
videoView.setVideoPath(videoPath);
videoView.start();
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
long duration = videoView.getDuration();
videoBar.setMax((int) duration);
LogService.log("in runnable", "videoView duration= " + videoView.getDuration());
}
});
videoBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (isVideo) {
videoView.seekTo(progress);
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
playBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if (isPlaying) {
// Button is ON
videoView.pause();
playBtn.setImageResource(R.drawable.play);// Do Something
isPlaying = false;
} else if (!isPlaying) {
// Button is OFF
videoView.start(); // Do Something
playBtn.setImageResource(R.drawable.pause);
isPlaying = true;
}
}
});
Now if i move the progressBar (videoBar), it takes the the video to that part of the video, but if the video plays, it does not move to progress bar, alongside. How can I make it, move the progress bar while the video is playing?
This helped me get it right, now it works. The first answer
How to use a Seekbar in android as a seekBar as well as a progressBar simultaneously?