I'm trying to implement a MediaPlayer on an Android app, but now I have two problems, which are not THE BIG SHOWSTOPPER but they are more then annoying and i have to fix it, just for me.
I implemented a async MediaPlayer+Controller to a Activity, which works fine.
My plan was to show also the percentage of the buffering on the MediaControl. This also works.
But now, after I can see the percentage, I saw a strange behaviour: if I seek to a position which is already in the buffer, the buffering will start from this position again. Is this a known and/or normal behavior/problem/feature ?
Here are more details:
I'm using the 2.2 SDK
This is how I implement it
public class Details extends Activity implements MediaPlayer.OnPreparedListener, MediaController.MediaPlayerControl {
[...]
private void setPosition(int currentPos ){
position = currentPos;
}
[...]
public void onCreate(Bundle savedInstanceState) {
[...]
mediaPlayer.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
#Override
public void onBufferingUpdate(MediaPlayer mp, int progress) {
setPosition(progress);
}
});
[...]
public int getBufferPercentage() {
return position;
}
[...]
public void seekTo(int i) {
General.mediaPlayer.seekTo(i);
}
}
What I expected after clicking on the seekbar
What I got
Is this normal?
This thread confirms that although a position is already buffered MediaPlayer sends a request to a server.
Related
hi I am using the sound class for my music so it will loop correctly (as advised here Libgdx: Lags in soundtrack looping) but when i close the app(it stills runs in the background) and open it again the sound starts to play over the previous sound, how can i avoid this?
This is my current music handler class
public class MusicHandler {
private Sound bgMusic;
public MusicHandler(Sound backgroundMusic){
bgMusic = backgroundMusic;
}
public void update(boolean play){
if(play){
bgMusic.loop();
}
else{
bgMusic.pause();
}
}
public void dispose(){
bgMusic.dispose();
}
}
I am using the Android YouTube API samples to create a chromeless YouTube player in my app. I am having an issue that the buffering / loading progress bar carries on displaying over my video even after it has loaded and started playing. I can reproduce this in the FragmentDemoActivity sample with a couple of small modifications:
public class FragmentDemoActivity extends AppCompatActivity implements YouTubePlayer.OnInitializedListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragments_demo);
YouTubePlayerFragment youTubePlayerFragment =
(YouTubePlayerFragment) getFragmentManager().findFragmentById(R.id.youtube_fragment);
youTubePlayerFragment.initialize(DeveloperKey.DEVELOPER_KEY, this);
}
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player,
boolean wasRestored) {
if (!wasRestored) {
player.setPlayerStyle(YouTubePlayer.PlayerStyle.CHROMELESS);
player.loadVideo("nCgQDjiotG0", 10);
}
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {}
}
I have changed FragmentDemoActivity to inherit from AppCompatActivity instead of YouTubeFailureRecoveryActivity, as the documentation says is fine to do. I have also changed the player style to be chromeless in onInitializationSuccess. Finally, I have changed cueVideo to loadVideo, just to trigger auto play.
This happens on multiple devices including Nexus 5X. I am using library version 1.2.2. No error is triggered in onInitializationFailure.
The video starts playing after buffering. The player is chromeless. However the buffering spinner never disappears. Is this a bug, or am I doing something I am not allowed to do?
I encountered this too, it really looks like a bug. Here is how I managed to work around it.
In your onInitializationSuccess, set a PlaybackEventListener on the player. Override the onBuffering and do something like this:
ViewGroup ytView = (ViewGroup)ytPlayerFragment.getView();
ProgressBar progressBar;
try {
// As of 2016-02-16, the ProgressBar is at position 0 -> 3 -> 2 in the view tree of the Youtube Player Fragment
ViewGroup child1 = (ViewGroup)ytView.getChildAt(0);
ViewGroup child2 = (ViewGroup)child1.getChildAt(3);
progressBar = (ProgressBar)child2.getChildAt(2);
} catch (Throwable t) {
// As its position may change, we fallback to looking for it
progressBar = findProgressBar(ytView);
// TODO I recommend reporting this problem so that you can update the code in the try branch: direct access is more efficient than searching for it
}
int visibility = isBuffering ? View.VISIBLE : View.INVISIBLE;
if (progressBar != null) {
progressBar.setVisibility(visibility);
// Note that you could store the ProgressBar instance somewhere from here, and use that later instead of accessing it again.
}
The findProgressBar method, used as a fallback just in case the YouTube code changes:
private ProgressBar findProgressBar(View view) {
if (view instanceof ProgressBar) {
return (ProgressBar)view;
} else if (view instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup)view;
for (int i = 0; i < viewGroup.getChildCount(); i++) {
ProgressBar res = findProgressBar(viewGroup.getChildAt(i));
if (res != null) return res;
}
}
return null;
}
This solution works perfectly fine for me, enabling the ProgressBar when the player is buffering and disabling it when it's not.
EDIT: If anyone using this solution discovers that this bug has been fixed or that the position of the ProgressBar has changed, please share so that I can edit my answer, thanks!
Using the Vitamio media player, I do not see a constant for when the video actually starts rendering (as there has been for the normal android MediaPlayer since api 17). onPreparedListeners do not detect when the rendering physically starts, and, as a result, the black screen prior to the video starting is seemingly unavoidable.
Is there any way to detect when the video has actually started rendering in Vitamio?
Though it's a bit of a hack, I found that this way works wonders:
Create a boolean, defaulted to false, which determines whether or not buffering has completed for the first time.
Set an onInfoListener on your Vitamio VideoView.
Look for MediaPlayer.MEDIA_INFO_BUFFERING_END
If the boolean you created is false, then set it to true and wait, with a while loop, until yourVideoView.getCurrentPosition() != 0. Note that 0 may be too low -- sometimes getCurrentPosition will return a number higher than zero before it has started, but typically the returned value will be no higher than 1000 / (the fps of your video). I used 40.
Execute desired code (remove other views to make the VideoView visible, or add the VideoView to the layout).
In an OnCompletionListener, set the created boolean back to false.
public class AnimationCanvas extends VideoView{
public static AnimationCanvas animationCanvas;
private static boolean bufferedOnce = false;
public AnimationCanvas(Context context, AttributeSet attrs){
super(context, attrs);
animationCanvas = this;
getHolder().addCallback(this);
this.setOnInfoListener(new MediaPlayer.OnInfoListener() {
#Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
if (!bufferedOnce && what == MediaPlayer.MEDIA_INFO_BUFFERING_END) {
bufferedOnce = true;
while (getCurrentPosition() < 40) {
try {
Thread.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
MainActivity.frameLayout.removeView(MainCanvas.mainCanvas);
return true;
}
return false;
}
});
this.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
bufferedOnce = false;
MainActivity.frameLayout.addView(MainCanvas.mainCanvas);
MainActivity.frameLayout.removeView(animationCanvas);
}
});
}
EDIT: Note that another option is to create a separate Runnable which does the waiting (while(vv.getCurrentPosition() < 40){}) and then, in the same Runnable, call runOnUIThread() to run a second runnable which alters / removes / adds views if needed (Views can only be touched by the thread in which they were created.) This way, there is no need for an onInfoListener -- just start the first Runnable in an onPreparedListener.
I am playing a music in Libgdx game. I wish to have a listener like this to play the music and do a specific process after that. But the program control never reaches the onCompletionListerner part.
private void playMusic() {
gameMusic = Gdx.audio.newMusic(Gdx.files.internal("data/bgm.mp3"));
gameMusic.play();
gameMusic.setOnCompletionListener(Music.OnCompletionListener(){
#Override
public void onCompletion(Music aMusic){
actionResolver.getLeaderboardGPGS();
}
}
);
Just add new before Music.OnCompletionListener() and it should work.
gameMusic.setOnCompletionListener(new Music.OnCompletionListener() {
#Override
public void onCompletion(Music aMusic) {
actionResolver.getLeaderboardGPGS();
}
});
Since you're passing an object (that implements OnCompletionListener), it has to be created with new as always.
i am using VideoView and seek bar but when i seekTo(..) on desired position through seekBar it play video from starting.
i trying this code:
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
mVideoView.seekTo(progress);
}
As Reno mentioned, you have to wait for the seeking to complete.
VideoView does not have a OnSeekCompleteListener() but 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
}
});
}
});
The call to VideoView.seekTo() is a wrapper around MediaPlayer.seekTo(). This function returns almost immediately even though the actual seeking is still being performed. Therefore you want to wait for seeking to complete via MediaPlayer.OnSeekCompleteListener.
However, as Reno mentioned, the standard VideoView does not support OnSeekCompleteListener.
But you can copy and locally customize the VideoView class to add this support yourself.
First, start with a copy of VideoView.java. Or you can clone the entire frameworks/base repo but warning it is over 1 gig of data to download.
Copy VideoView.java into your Eclipse Android project and it will start building but fail. Here's what I did to get it to compile:
Update the package statement to match your project.
Comment out the references to MetaData. The fix for this is on my todo list. These need to be replaced with calls to MediaMetadataRetriever.
Replace mContext with calls to getBaseContext()
Now you are ready to add the code for OnSeekCompleteListener. The implementation is similar to the other listeners, i.e OnCompletionListener.
public class VideoView extends SurfaceView
implements MediaPlayerControl {
// The client's listener which is the notification callback.
private OnSeekCompleteListener mOnSeekCompleteListener;
// Set up MediaPlayer to forward notifications to client.
private MediaPlayer.OnSeekCompleteListener mSeekCompleteListener =
new MediaPlayer.OnSeekCompleteListener() {
public void onSeekComplete(MediaPlayer mp) {
if (mOnCompletionListener != null) {
mOnCompletionListener.onCompletion(mMediaPlayer);
}
}
};
// API for client to set their listener.
public void setOnSeekCompleteListener(OnSeekCompleteListener l)
{
mOnSeekCompleteListener = l;
}
}
Finally, update your own code:
Update references to android.widget.VideoView to use your customized VideoView.
Implement a listener and set it via by calling setOnSeekCompleteListener().
Your code now receives notifications when the seek has really completed and it can then perform subsequent seeks.
You have to wait for the seeking to complete, unfortunately VideoView does not have a OnSeekCompleteListener() (why Google? -_-)
use my method works like charm
Take mediaPlayer
MediaPlayer mediaPlayer;
init mediaPlayer
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer = mp;
}
});
use seekTo method like this
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mediaPlayer.seekTo((int) mStartPosition, MediaPlayer.SEEK_CLOSEST);
} else {
mediaPlayer.seekTo((int) mStartPosition);
}
You should use
mVideoView.seekTo(progress, SEEK_CLOSEST)