android: onSeekCompleteListener with VideoView - android

I am using VideoView to play video files. I am using seekTo function in order to play the video from where it has been left off. However, I wanted to do some operations when the seek operation is finished. For that I need to use onSeekCompleteListener; however, onSeekCompleteListener is not supported with VideoView; it can be used with MediaPlayer. My Question is that "is there any way by which I can use onSeekCompleteListener" with VideoView?
Thanks alot

There is no need to create a custom VideoView.
You can access the MediaPlayer from the onPrepared method of the VideoView and then set the OnSeekCompleteListener, like this :
mVideoView.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.setOnSeekCompleteListener(new OnSeekCompleteListener() {
#Override
public void onSeekComplete(MediaPlayer mp) {
//TODO: Your code here
}
});
}
});

public class ObservableVideoView extends VideoView
{
private IVideoViewActionListener mVideoViewListener;
private boolean mIsOnPauseMode = false;
public interface IVideoViewActionListener
{
void onPause();
void onResume();
void onTimeBarSeekChanged(int currentTime);
}
public void setVideoViewListener(IVideoViewActionListener listener)
{
mVideoViewListener = listener;
}
#Override
public void pause()
{
super.pause();
if (mVideoViewListener != null)
{
mVideoViewListener.onPause();
}
mIsOnPauseMode = true;
}
#Override
public void start()
{
super.start();
if (mIsOnPauseMode)
{
if (mVideoViewListener != null)
{
mVideoViewListener.onResume();
}
mIsOnPauseMode = false;
}
}
#Override
public void seekTo(int msec)
{
super.seekTo(msec);
if (mVideoViewListener != null)
{
mVideoViewListener.onTimeBarSeekChanged(msec);
}
}
public ObservableVideoView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public ObservableVideoView(Context context)
{
super(context);
}
public ObservableVideoView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
}
you can listen for events like this:
public class VideoPlayerActivity extends Activity
{
public static final String VIDEO_URL = "VideoUrl";
private String path = "";
private ObservableVideoView mVideoView;
#Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.video_player_activity_layout);
mVideoView = (ObservableVideoView) findViewById(R.id.videoView1);
mVideoView.setMediaController(new MediaController(this));
mVideoView.setVideoViewListener(mVideoViewListener);
path = getIntent().getStringExtra(VIDEO_URL);
if (path == "")
{
Toast.makeText(this, "Please edit VideoViewDemo Activity, and set path" + " variable to your media file URL/path", Toast.LENGTH_LONG)
.show();
}
else
{
mVideoView.setVideoPath(path);
mVideoView.requestFocus();
mVideoView.start();
}
}
private IVideoViewActionListener mVideoViewListener = new IVideoViewActionListener()
{
#Override
public void onTimeBarSeekChanged(int currentTime)
{
//TODO what you want
}
#Override
public void onResume()
{
//TODO what you want
}
#Override
public void onPause()
{
//TODO what you want
}
};
}

Farhan, you are correct, onSeekCompleteListener is not supported by VideoView.
But you can copy and locally customize the VideoView class to add this support yourself.
I show how to do this in my answer to 7990784.

Related

Android MediaPlayer with SurfaceView Fails on Prepare() Method

I am trying to build a video player using surface view and media player. My data source is an online video and I have enabled internet permission in manifest.
However, the video does not pay but throws a warning about prepare method without any hint at what the problem is like below:
W/System.err: at android.media.MediaPlayer._prepare(Native Method)
at android.media.MediaPlayer.prepare(MediaPlayer.java:1188)
at in.sashi.androidvideoplayer.MainActivity.surfaceCreated(MainActivity.java:64)
here's my code for this purpose:
public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback, MediaPlayer.OnPreparedListener, View.OnClickListener {
private static final String TAG = MainActivity.class.getSimpleName();
private SurfaceView surfaceView;
private ImageView playIV, backIV;
private TextView videoNameTV;
private MediaPlayer mediaPlayer;
private SurfaceHolder holder;
// private static final String VIDEO_URL = "https://www.youtube.com/watch?v=vEVd0QMjCc8";
private static final String VIDEO_URL = "http://mic.duytan.edu.vn:83/FINAL.mp4";
private static final String VIDEO_NAME = "FALL HARDER | BASKETBALL MOTIVATION";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
surfaceView = findViewById(R.id.surfsceView);
playIV = findViewById(R.id.playIV);
backIV = findViewById(R.id.backIV);
videoNameTV = findViewById(R.id.videoNameTV);
videoNameTV.setText(VIDEO_NAME);
backIV.setOnClickListener(this);
playIV.setOnClickListener(this);
holder = surfaceView.getHolder();
holder.addCallback(this);
surfaceView.setKeepScreenOn(true);
mediaPlayer = new MediaPlayer();
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
mediaPlayer.setDisplay(holder);
try {
mediaPlayer.setDataSource(VIDEO_URL);
mediaPlayer.prepare();
mediaPlayer.setOnPreparedListener(this);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.backIV:
onBackPressed();
break;
case R.id.playIV:
if (mediaPlayer.isPlaying()){
Log.d(TAG, "Pause Clicked");
mediaPlayer.pause();
playIV.setImageResource(R.drawable.play);
} else {
Log.d(TAG, "Play Clicked");
mediaPlayer.start();
playIV.setImageResource(R.drawable.pause);
}
break;
}
}
#Override
protected void onPause() {
super.onPause();
collect();
}
private void collect() {
if (mediaPlayer != null){
mediaPlayer.release();
mediaPlayer = null;
}
}
}
Can anyone help me to figure this out? Thanks.
Use mediaplayer object which is being defined globally.
#Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
}
How about this?
#Override
public void surfaceCreated(SurfaceHolder holder) {
mediaPlayer.setDisplay(holder);
try {
mediaPlayer.setDataSource(VIDEO_URL);
mediaPlayer.setOnPreparedListener(this);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
}
}

Change the volume of a specif player and get its current volume

I've this class (my media player class) and I need to create 2 methods on it, one to change the volume and other to get the current volume. (I'll create a seekbar to change the player volume), ok.
public class LoopMediaPlayer {
private Context ctx = null;
private int rawId = 0;
private MediaPlayer currentPlayer = null;
private MediaPlayer nextPlayer = null;
public static LoopMediaPlayer create(Context ctx, int rawId) {
return new LoopMediaPlayer(ctx, rawId);
}
private LoopMediaPlayer(Context ctx, int rawId) {
this.ctx = ctx;
this.rawId = rawId;
currentPlayer = MediaPlayer.create(this.ctx, this.rawId);
currentPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
currentPlayer.start();
}
});
createNextMediaPlayer();
}
private void createNextMediaPlayer() {
nextPlayer = MediaPlayer.create(ctx, rawId);
currentPlayer.setNextMediaPlayer(nextPlayer);
currentPlayer.setOnCompletionListener(onCompletionListener);
}
private MediaPlayer.OnCompletionListener onCompletionListener = new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
mediaPlayer.release();
currentPlayer = nextPlayer;
createNextMediaPlayer();
}
};
public void stopPlayer() {
if (currentPlayer != null && currentPlayer.isPlaying()) {
currentPlayer.stop();
currentPlayer.release();
}
if (nextPlayer != null && nextPlayer.isPlaying()) {
nextPlayer.stop();
nextPlayer.release();
}
}
public void setVolume(float vol) {
//todo
currentPlayer.setVolume(vol, vol);//don't work
}
public int getVolume(){
//todo
return 0;
}
}
and my seekBar progress and listener
seekBar.setProgress(player.getVolume());//use the method that retrieve the current volume
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, final int i, boolean b) {
runOnUiThread(new Runnable() {
#Override
public void run() {
player.setVolume(i);//use the method that change the volume
}
});
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
I tried mp.setVolume() but didn't change any thing. And I can't get the current volume to set like the seekbar progress before show it.
Details - All Audios are in raw file (not stream). There will be more than one player playing at same time, them will be stored so I can retrieve any of them any time, and I want to change and retrieve just the volume of a specific player not of all them at same time.
Anyone know how I should implement both this methods, 'cause I have no idea.
Thanks

Android MediaPlayer to play video stream in Service screen rotation

I would like to create Activity with video player to play online stream using MediaPlayer class and SurfaceView to display. I'm creating MediaPlayer in separate Service so after screen rotation player don't have to be created again and don't have to connect to stream. My problem is that I don't know how to write Activity so my service wouldn't start every time after screen rotation.
My code below, in onStart() I start service but I don't know how to change it so it didn't start every time.
public class VideoPlayerActivity extends Activity implements SurfaceHolder.Callback {
private String path;
private SurfaceHolder vidHolder;
private SurfaceView vidSurface;
private VideoService videoService;
private Intent playIntent;
private boolean videoBound = false;
private ServiceConnection musicConnection = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName name, IBinder service) {
VideoService.VideoBinder binder = (VideoService.VideoBinder) service;
videoService = binder.getService();
videoService.setUrl(path);
videoBound = true;
if (vidHolder != null && videoService.getMediaPlayer() != null) {
videoService.getMediaPlayer().setDisplay(vidHolder);
videoService.playVideo();
}
}
#Override
public void onServiceDisconnected(ComponentName name) {
videoBound = false;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
path = "https://archive.org/download/ksnn_compilation_master_the_internet/ksnn_compilation_master_the_internet_512kb.mp4"; //TODO tmp
playIntent = new Intent(this, VideoService.class);
bindService(playIntent, musicConnection, Context.BIND_AUTO_CREATE);
startService(playIntent);
vidSurface = (SurfaceView) findViewById(R.id.surfView);
vidHolder = vidSurface.getHolder();
vidHolder.addCallback(VideoPlayerActivity.this);
}
#Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
if (videoService != null && videoService.getMediaPlayer() != null) {
videoService.getMediaPlayer().setDisplay(vidHolder);
}
}
#Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
Log.d("ServiceConnection", "surfaceChanged " + i + " " + i1 + " " + i2);
}
#Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
Log.d("ServiceConnection", "surfaceDestroyed");
}
#Override
protected void onDestroy() {
super.onDestroy();
unbindService(musicConnection);
stopService(playIntent);
videoService = null;
}
}
Service class:
public class VideoService extends Service implements OnPreparedListener {
private MediaPlayer player;
private String path;
private final IBinder musicBind = new VideoBinder();
#Override
public void onCreate() {
Log.d("VideoService", "onCreate");
super.onCreate();
player = new MediaPlayer();
initMusicPlayer();
}
#Override
public IBinder onBind(Intent intent) {
return musicBind;
}
#Override
public boolean onUnbind(Intent intent){
player.stop();
player.release();
return false;
}
public void initMusicPlayer() {
player.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setOnPreparedListener(this);
}
public void playVideo() {
try {
player.setDataSource(path);
player.prepareAsync();
} catch (IOException e) {}
}
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
}
public void setUrl(String url) {
path = url;
}
public MediaPlayer getMediaPlayer() {
return player;
}
public class VideoBinder extends Binder {
public VideoService getService() {
return VideoService.this;
}
}
}
You could either check for savedInstanceState being null to only start the service if the Activity is freshly created
if (savedInstanceState == null) {
startService...
}
or handle screen rotation yourself by adding
android:configChanges="orientation|keyboardHidden|screenSize"
to your Activity in manifest. Like this onCreate will not be called on rotation any more.

How to show/hide play/pause Button in video view in android?

I am doing an Android project based on Video view. I want to show play button before the user clicks play, when user decides to pause video - pause button shows up. Clicking on pause button should trigger playing the video again from the same place where it was paused (like YouTube video).
im1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
vv1.setVideoURI(Uri.parse("android.resource://com.example.cm.filmfestival/" + R.raw.mission));
im1.setVisibility(View.INVISIBLE);
im2.setVisibility(View.INVISIBLE);
vv1.start();
}
});
im2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
vv1.stopPlayback();
im2.setVisibility(View.VISIBLE);
im1.setVisibility(View.INVISIBLE);
}
});
#Override
public boolean onTouch(View v, MotionEvent event) {
im1.setVisibility(View.VISIBLE);
vv1.start();
im2.setVisibility(View.VISIBLE);
vv1.stopPlayback();
return true;
}
Use the code below
<VideoView
android:id="#+id/video_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center" />
public VideoView myVideoView;
private int position = 0;
private MediaController mediaControls;
// set the media controller buttons
if (mediaControls == null)
{
mediaControls = new MediaController(MainActivity.this);
}
// initialize the VideoView
myVideoView = (VideoView) findViewById(R.id.video_view);
try
{
// set the media controller in the VideoView
myVideoView.setMediaController(mediaControls);
// set the uri of the video to be played
myVideoView.setVideoURI(Uri.parse("your UrI"));
} catch (Exception e)
{
Log.e("Error", e.getMessage());
e.printStackTrace();
}
myVideoView.requestFocus();
// we also set an setOnPreparedListener in order to know when the video
// file is ready for playback
myVideoView.setOnPreparedListener(new OnPreparedListener()
{
public void onPrepared(MediaPlayer mediaPlayer)
{
// if we have a position on savedInstanceState, the video
// playback should start from here
myVideoView.seekTo(position);
System.out.println("vidio is ready for playing");
if (position == 0)
{
myVideoView.start();
} else
{
// if we come from a resumed activity, video playback will
// be paused
myVideoView.pause();
}
}
});
Set an OntouchListener on your VideoView and then in Ontouch callback check if the video is playing or paused before pausing or playing it.
private boolean stopped = false;
private ImageView postVideoPlaypauseIcon;
//set it to any play/pause icon
postVideoPlaypauseIcon = mView.findViewById(R.id.playpause_icon);
private int stopPosition;
Add above code.
then, add CustomVideoView class, make VideoView extend it and create an object video and use findviewbyid
public class CustomVideoView extends VideoView {
private PlayPauseListener mListener;
public CustomVideoView(Context context) {
super(context);
}
public CustomVideoView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomVideoView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void setPlayPauseListener(PlayPauseListener listener) {
mListener = listener;
}
#Override
public void pause() {
super.pause();
if (mListener != null) {
mListener.onPause();
}
}
#Override
public void start() {
super.start();
if (mListener != null) {
mListener.onPlay();
}
}
public static interface PlayPauseListener {
void onPlay();
void onPause();
}
}
replace xml for VideoView with below code
`<package-name.CustomVideoView
android:id="#+id/custom_videoview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>`
Finally,
add setOnTouchListener for listening to touch.
video.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(stopped == false){
stopPosition = postVideo.getCurrentPosition();
video.pause();
stopped = true;
} else if(stopped == true){
video.seekTo(stopPosition);
video.start();
stopped = false;
}
Log.e("TAP","from video ");
return false;
}
});
add setPlayPauseListener on your videoView object
video.setPlayPauseListener(new CustomVideoView.PlayPauseListener() {
#Override
public void onPlay() {
System.out.println("Play!");
videoPlaypauseIcon.setVisibility(View.VISIBLE);
}
#Override
public void onPause() {
System.out.println("Pause!");
videoPlaypauseIcon.setVisibility(View.INVISIBLE);
}
});
used this for reference

How to prepare mediaplayer in android while another playing?

I using MediaPlayerto play video from http stream is any solution to prepare second media player with another http url while first one still playing?
I want to play two videos without black screen between them, when first one end second start immediately.
Here is my code:
public class VideoActivity extends Activity implements SurfaceHolder.Callback {
private MediaPlayer mediaPlayer;
private SurfaceHolder surfaceHolder;
private SurfaceView playerSurfaceView;
private String videoSrc = "http://192.168.1.101:8090/api/video/";
private String android_id;
private MediaPlayer next;
private static boolean firstCall = true;
private String secondVideoSrc = "http://192.168.1.101:8090/api/secondvideo/";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
playerSurfaceView = (SurfaceView)findViewById(R.id.playersurface);
surfaceHolder = playerSurfaceView.getHolder();
surfaceHolder.addCallback(this);
GetDeviceId();
videoSrc += android_id;
secondVideoSrc += android_id;
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
prepareVideoPlayer();
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
private void GetDeviceId() {
android_id = Settings.Secure.getString(getBaseContext().getContentResolver(),
Settings.Secure.ANDROID_ID);
}
private void prepareVideoPlayer() {
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDisplay(surfaceHolder);
mediaPlayer.setDataSource(this, Uri.parse(videoSrc));
mediaPlayer.setScreenOnWhilePlaying(true);
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
if(firstCall) {
mediaPlayer.start();
firstCall = false;
}
new AsyncPrepareNext().execute();
}
});
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
next.start();
}
});
}
catch(Exception ex) {
}
}
private void prepareNext() {
try {
next = new MediaPlayer();
next.setDisplay(surfaceHolder);
next.setDataSource(this, Uri.parse(secondVideoSrc));
next.setScreenOnWhilePlaying(true);
next.prepareAsync();
next.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
}
});
next.setAudioStreamType(AudioManager.STREAM_MUSIC);
next.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
}
});
}
catch(Exception ex) {
}
}
private class AsyncPrepareNext extends AsyncTask<Void,Void,Void> {
#Override
protected Void doInBackground(Void... params) {
prepareNext();
return null;
}
}
}
When i run this first video starts for 2 or 3 seconds and then stop with some errors like media server died, audio flinger died, error (100,0), error (38,0)
You'll probably need to implement the OnPreparedListener() and OnCompletedListener() to accomplish this and you might need 2 media players for it as well.
#Override
public void onPrepared(MediaPlayer mp){
// Set Video Source for second video
mp.setDataSource(path);
// Prepare second video
mp.prepareAsync();
}
#Override
public void onCompletion(MediaPlayer mp){
// Start second video here
mp.start();
}
and set them like this:
mediaPlayer.setOnPreparedListener(new OnPreparedListener());
mediaPlayer.setOnCompletedListener(new OnCompletionListener());

Categories

Resources