How to restart video on Exoplayer after ExoPlayer.STATE_ENDED - android

I'm using Exoplayer with a base of the DemoPlayer.
I want to restart/replay the video from beginning on some user action after that ExoPlayer.STATE_ENDED is dispatched.
I've tried to use seekTo(0) and mPlayer.setPlayWhenReady(true); after but it didn't do anything, at all.

I've updated the library to ExoPlayer r1.4.2 and it does the job...
mPlayer.seekTo(0);
mPlayer.setPlayWhenReady(true); // replay from start
// Pause video after restart
mPlayer.seekTo(0);
mPlayer.setPlayWhenReady(false);

A video can be seamlessly looped using a LoopingMediaSource. The following example loops a audio/video indefinitely. It’s also possible to specify a finite loop count when creating a LoopingMediaSource.
MediaSource source = new ExtractorMediaSource(audioUri, ...);
// Loops the audio indefinitely.
LoopingMediaSource loopingSource = new LoopingMediaSource(source);
or add a listener
playerExo.addListener(new ExoPlayer.Listener() {
#Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
switch(playbackState) {
case ExoPlayer.STATE_BUFFERING:
break;
case ExoPlayer.STATE_ENDED:
playerExo.seekTo(0);
break;
case ExoPlayer.STATE_IDLE:
break;
case ExoPlayer.STATE_PREPARING:
break;
case ExoPlayer.STATE_READY:
break;
default:
break;
}
}
#Override
public void onPlayWhenReadyCommitted() {
}
#Override
public void onPlayerError(ExoPlaybackException error) {
}
});
playerExo.seekTo(0);
playerExo.setPlayWhenReady(true);//replay from start
/* If you want to Pause audio/video and restart
mPlayer.seekTo(0);
mPlayer.setPlayWhenReady(false);*/
You can check the Exoplayer latest release to be up to date:
Exoplayer Releases

This code works for me
#OnClick(R.id.image_button_play)
public void play(){
Log.d(TAG, "play: clicked");
//If video has finished then set Exoplayer to 0
if (simpleExoPlayer.getPlaybackState() == Player.STATE_ENDED){
simpleExoPlayer.seekTo(0);
}
simpleExoPlayer.setPlayWhenReady(true);
playButton.setVisibility(View.INVISIBLE);
}

Related

How to show progressbar for streaming video on surfaceView with libvlc?

I used libvlc (de.mrmaffen:vlc-android-sdk:1.0.6) library for http video streaming. Everything works fine just one issue.
I used progress-bar before my playmovie() function call,and stop the progress-bar with using libvlc.isPlaying() boolean function so at this time my guess is video is loaded and we will stop the progressbar.
How to get exact time for buffering the video and streaming start to stop the progressbar?
You need to implement EventListener.Like this way
LibVLC vlcInstance = new LibVLC(context, new VlcOptions().getDefaultOptions());
org.videolan.libvlc.MediaPlayer player = new org.videolan.libvlc.MediaPlayer(vlcInstance);
player.setEventListener(new org.videolan.libvlc.MediaPlayer.EventListener() {
#Override
public void onEvent(org.videolan.libvlc.MediaPlayer.Event event) {
switch (event.type) {
case org.videolan.libvlc.MediaPlayer.Event.Opening:
//Video Opening
break;
case org.videolan.libvlc.MediaPlayer.Event.Playing:
//Video Playing
break;
case org.videolan.libvlc.MediaPlayer.Event.Buffering:
//Video Buffering
break;
case org.videolan.libvlc.MediaPlayer.Event.Stopped:
//Video Stopped
break;
case org.videolan.libvlc.MediaPlayer.Event.EndReached:
//Video EndReached/Completed
break;
case org.videolan.libvlc.MediaPlayer.Event.EncounteredError:
//Video EncounteredError/Failed
break;
default:
break;
}
}
});
Media media = new Media(vlcInstance, videoUri);
media.addOption(":fullscreen");
media.setHWDecoderEnabled(true, false);
player.setMedia(media);
IVLCVout vlcOut = player.getVLCVout();

VLC Compile. Add Button and play video

Have the source code VLC.
Compiled under android.
Added activity.
It has three buttons.
at the touch of a button should switch the channel stream , video from sd card or video from internet.
public void onClick(View v)
{
switch (v.getId()) {
case R.id.b1:
break;
case R.id.b2:
break;
case R.id.b3:
Context context = this;
VideoPlayerActivity.start(context, "http://www.youtube.com/watch?v=9Xjw2PtKjJE",
null, false, false);
break;
}
All compiled. By clicking the button, a player, but the video does not start to play.
Blank screen and the player does not see the video.
The same problem with the video playback from the card.
Tell me how to handle the press of a button.
Sorry for my English.
Instead of VideoPlayerActivity.start
VLCCallbackTask task = new VLCCallbackTask(MainActivity.this)
{
#Override
public void run() {
AudioServiceController c = AudioServiceController.getInstance();
c.append("http://www.youtube.com/watch?v=9Xjw2PtKjJE");
}
};
task.execute();

Android - Pause music player when a video is started

I have and app with an audio player and video player. I want when the audio player is playing in background and I start the video, the audio player to stop itself.
I could achieve this easily with a broadcast receiver by sending an intent when opening the Video, and listen for that intent in Audio player and stop it in onReceive().
However, I would like to extend this feature not only to my audio player, but to all audio players. So I thought if there's a standard intent to send when the video player opens?
You should use audio focus for this. Cf the RandomMusicPlayer sample android project:
public class AudioFocusHelper {
AudioManager mAM;
MusicFocusable mFocusable;
private final AudioManager.OnAudioFocusChangeListener listener = new AudioManager.OnAudioFocusChangeListener() {
#Override
/**
* Called by AudioManager on audio focus changes. We implement this by calling our
* MusicFocusable appropriately to relay the message.
*/
public void onAudioFocusChange(int focusChange) {
if (mFocusable == null) return;
switch (focusChange) {
case AudioManager.AUDIOFOCUS_GAIN:
mFocusable.onGainedAudioFocus();
break;
case AudioManager.AUDIOFOCUS_LOSS:
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
mFocusable.onLostAudioFocus(false);
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
mFocusable.onLostAudioFocus(true);
break;
default:
}
}
};;
public AudioFocusHelper(Context ctx, MusicFocusable focusable) {
mAM = (AudioManager) ctx.getSystemService(Context.AUDIO_SERVICE);
mFocusable = focusable;
}
/** Requests audio focus. Returns whether request was successful or not. */
public boolean requestFocus() {
return AudioManager.AUDIOFOCUS_REQUEST_GRANTED ==
mAM.requestAudioFocus(listener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
}
/** Abandons audio focus. Returns whether request was successful or not. */
public boolean abandonFocus() {
return AudioManager.AUDIOFOCUS_REQUEST_GRANTED == mAM.abandonAudioFocus(listener);
}
}

How to turn off my media service for an incoming call? My AudioManager is not working

I'm trying to setup my audio playing app to stop playback if there is an interruption. I followed directions in Android SDK Developer notes about setting up an AudioFocusHelper like so:
public class AudioFocusHelper implements AudioManager.OnAudioFocusChangeListener {
AudioManager mAudioManager;
Media_Service mService;
Context mContext;
public AudioFocusHelper(Context ctx, Media_Service svc) {
mAudioManager = (AudioManager) ctx.getSystemService(Context.AUDIO_SERVICE);
mService = svc;
}
public boolean requestFocus() {
return AudioManager.AUDIOFOCUS_REQUEST_GRANTED ==
mAudioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN);
}
public boolean abandonFocus() {
return AudioManager.AUDIOFOCUS_REQUEST_GRANTED ==
mAudioManager.abandonAudioFocus(this);
}
#Override
public void onAudioFocusChange(int focusChange) {
// let your service know about the focus change
mService.AudioFocus(focusChange);
}
}
and I have my audio player running in a service as they suggest. I have this method in my audio service to respond to Audio Focus changes to pause the playback but its not working -- I don't know how to test this in the vm debugger so I can't really see what is happening on an incoming call. It doesn't appear to get called since I told it to popup toasts:
public void AudioFocus(int focusChange) {
switch (focusChange) {
case AudioManager.AUDIOFOCUS_GAIN: // resume playback
if (mMediaPlayer == null)
initMediaPlayer();
else if (!mMediaPlayer.isPlaying())
mMediaPlayer.start();
//mMediaPlayer.setVolume(1.0f, 1.0f);
break;
case AudioManager.AUDIOFOCUS_LOSS: // Lost focus for an unbounded amount of time: stop playback and release media player
if (mMediaPlayer.isPlaying()) {
Toast.makeText(this,
"Playback interrupted by focus loss", Toast.LENGTH_SHORT).show();
mMediaPlayer.stop();
}
mMediaPlayer.release();
mMediaPlayer = null;
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: // Lost focus for a short time, but we have to stop
// playback. We don't release the media player because playback
// is likely to resume
if (mMediaPlayer.isPlaying()) {
Toast.makeText(this,
"Playback paused by focus loss (transient)", Toast.LENGTH_SHORT).show();
mMediaPlayer.pause();
}
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: // Lost focus for a short time, but it's ok to keep playing
// at an attenuated level
if (mMediaPlayer.isPlaying()) {
Toast.makeText(this,
"Playback paused by focus loss (duck)", Toast.LENGTH_SHORT).show();
mMediaPlayer.pause();
//mMediaPlayer.setVolume(0.1f, 0.1f);
}
break;
}
}
I can post more of the code if necessary, it seems like I'm posting a lot of code already and I don't want to post an excessive amount. It looks to me like my onAudioFocusChange just isn't getting called. I am running this on Android 2.2 (minSDK 8) since these feature is not supported before 2.2. Searched hi and low for tips and I find very little about this topic at all so I'm hoping somebody out there can give me some clues.
I had this same issue, remember you need to call the requestFocus() method when you start playback and abandonFocus() when you are done.
I think this may help with generating incoming calls through DDMS: Fake Incoming Call Android
Hopefully, you can debug your application with this.

How to detect when VideoView starts playing (Android)?

Here's the problem, I want to change play button to pause button when the video stream starts playing in videoview but I don't know how to detect that event?
There is a great article about MediaPlayer in here - http://www.malmstein.com/blog/2014/08/09/how-to-use-a-textureview-to-display-a-video-with-custom-media-player-controls/
You can set infoListener on your VideoView
setOnInfoListener(new MediaPlayer.OnInfoListener() {
#Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
if (what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START) {
// Here the video starts
return true;
}
return false;
}
I ended up using VideoView.setOnPreparedListener. This was enough to cover my problem (play button drawable change to pause)
accepted answer here is not 100% accurate.
sometimes onprepared is call 3 seconds before first frame is being rendered. i suggest having a callback on that event (MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START)
mMediaPlayer.setOnInfoListener(new MediaPlayer.OnInfoListener() {
#Override
public boolean onInfo(MediaPlayer mediaPlayer, int i, int i1) {
if (i == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START){
//first frame was bufered - do your stuff here
}
return false;
}
});
see media info documantaion for more callbacks of info/warning:
https://developer.android.com/reference/android/media/MediaPlayer.html#MEDIA_INFO_VIDEO_RENDERING_START
As far as I know, there is no event sent when video start playing in VideoView, but I can think of two options:
Create a version of VideoView by yourself, that sends event in those cases.
Use MediaController (which is default in VideoView)
If you want to follow option one - you can get the source of VideoView from here
isPlaying() can be called to test whether the MediaPlayer object is in the Started
Android MediaPlayer.isPlaying()
Another way to detect if videoview started is using videoView.getCurrentPosition(). getCurrentPosition() returns 0 if streaming not started.
protected Runnable playingCheck = new Runnable() {
public void run() {
while (true) {
if (vw.getCurrentPosition() != 0) {
// do what you want when playing started
break;
} else {
try {
Thread.sleep(250);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
};
Then call:
new Thread(playingCheck).start();
Please try this :
final Handler h = new Handler();
h.postDelayed( new Runnable() {
public void run() {
if (videoView.getCurrentPosition() != 0) {
((ProgressBar) rootView.findViewById(R.id.pgStreaming)).setVisibility(View.GONE);
} else {
h.postDelayed(this, 250);
}
}
}, 250);

Categories

Resources