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();
Related
I need to pause a player of a specific application - for example Spotify. But if some other player plays I would like to keep it playing and only pause the Spotify.
Is there any function how I can do it?
I ended up with possibility to pause all apps, but not the specific one.
Or other help would be if I could identify what app is currently playing a Media Player and if it is Spotify then I can pause all apps, but I will know that I stop only Spotify.
Cheers
There is no way to way extensively to stop particular app player audio
If they implement music api which is directly implemented with hardware
then if you play music in your app then its automatically stop
with AudioManager. You can also get callback if other application play audio.
private OnAudioFocusChangeListener focusChangeListener =
new OnAudioFocusChangeListener() {
public void onAudioFocusChange(int focusChange) {
AudioManager am =(AudioManager)getSystemService(Context.AUDIO_SERVICE);
switch (focusChange) {
case (AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) :
// Lower the volume while ducking.
mediaPlayer.setVolume(0.2f, 0.2f);
break;
case (AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) :
pause();
break;
case (AudioManager.AUDIOFOCUS_LOSS) :
stop();
ComponentName component =new ComponentName(AudioPlayerActivity.this,MediaControlReceiver.class);
am.unregisterMediaButtonEventReceiver(component);
break;
case (AudioManager.AUDIOFOCUS_GAIN) :
// Return the volume to normal and resume if paused.
mediaPlayer.setVolume(1f, 1f);
mediaPlayer.start();
break;
default: break;
}
}
};
I have created an application to play audio using media player but my player does not stops even if other app like youtube starts playing video.
How to stop audio if other app starts playing an audio.
I have used mediaPlayer.start(); to play an audio.
Any help would be appreciated.
You'll need to listen for Audio focus change.
Refer: https://developer.android.com/reference/android/media/AudioManager.OnAudioFocusChangeListener.html
OnAudioFocusChangeListener callback is called with a flag indicating audio focus status, which can be used to stop media player.
You can set a AudioFocusChangeListener to listen for Audio focus change.
private AudioManager.OnAudioFocusChangeListener mOnAudioFocusChangeListener;
audioFocusChangeListener =new AudioManager.OnAudioFocusChangeListener()
{
#Override
public void onAudioFocusChange ( int focusChange){
switch (focusChange) {
case AudioManager.AUDIOFOCUS_GAIN:
// Set volume level to desired levels
play();
break;
case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
// You have audio focus for a short time
play();
break;
case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK:
// Play over existing audio
play();
break;
case AudioManager.AUDIOFOCUS_LOSS:
stop();
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
// Temporary loss of audio focus expect to get it back you can keep your resources around
pause();
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
// Lower the volume
break;
}
}
}
;
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);
}
I am having trouble with MediaPlayer. In the enclosed code, when a button is touched, a voice cue is activated when the option to do so is selected. However, the voice works for approximately 31 times and then the sound goes off. Using the .release() method (which I have commented out for now) causes the sound to not work at all.
How can I correct this function?
Thank You
....
import android.media.MediaPlayer;
....
MediaPlayer mp = null;
........
// Causes the name of the appropriate cell to be announced
public void sayCellName() {
if(voiceChoice == false) return;
else{
switch(cellName){
case 1:
mp = MediaPlayer.create(this, R.raw.poly);
break;
case 2:
mp = MediaPlayer.create(this, R.raw.lymph);
break;
case 3:
mp = MediaPlayer.create(this, R.raw.mono);
break;
case 4:
mp = MediaPlayer.create(this, R.raw.eo);
break;
case 5:
mp = MediaPlayer.create(this, R.raw.baso);
break;
case 6:
mp = MediaPlayer.create(this, R.raw.band);
break;
default:
break;
} // end switch statement
} // end else statement;
mp.start();
// mp.release();
} // end of sayCellName
} // end of main
The Android OS has a hard-coded limit of audio track resources that may play back sound simultaneously. As it so happens that limit is 32 tracks that have to be shared by all running applications. What you are doing is wasting these tracks by creating fresh MediaPlayers on each button click without releasing them.
For your scenario you should probably create and reuse a single MediaPlayer instance and release it when your Activity stops. It is possible to reset a MediaPlayer and set a different stream data source afterwards.
You have to release the player OnPause or OnDestroy method.
#Override
public void onPause() {
if (mp!= null) mp.release();
}
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();