How do I prevent the initial black screen of a VideoView? I don't want to use a placeholder, as I therefore would have to create a previewimage for every VideoView (and I'm using several VideoViews...)
A common solution for this is to use videoView.seekto(100) once the video is prepared. But this doesn't work for me. It does set the seeker to the correct position, but the VideoView remains black until the user presses the play-button.
This is my code:
mVideoView.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mVideoView.start();
mVideoView.seekTo(100);
mp.setOnSeekCompleteListener(new OnSeekCompleteListener() {
#Override
public void onSeekComplete(MediaPlayer mp) {
mVideoView.pause();
}
});
}
I also tried this without the onSeekCompleteListener and without seperatly starting and pausing the video, but it didn't work either..
anyone a hint for solving this issue?
EDIT
I figured out that this is only an issue for streaming videos! It's working like a charm for locally stored ones...
and it actually seems to be an official issue...
https://code.google.com/p/android/issues/detail?id=4124
Related
I've been trying a few things with sounds in flutter lately . Using audioPlayer plugin.
But I'm facing a wall. I have both a background music, and small sounds effects.
The problem is that I can't play both at once. If I play the background music, then the sound effect won't play. And the opposite works too.
Any idea about how to solve that issue ?
This is an issue with the audioplayer plugin and there's an issue open for it already. The author has indicated that he's open to pull requests if you'd like to take a crack at implementing it.
Use media players. Use this code to set up the background music:
MediaPlayer backgroundMediaPlayer = MediaPlayer.create(getApplicationContext(), Uri.parse(PATH_TO_BACKGROUND_MUSIC));
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
}
});
Then replace PATH_TO_BACKGROUND_MUSIC with the background music path.
Then when you want your sound effect to happen, use this code:
MediaPlayer soundEffectMediaPlayer = MediaPlayer.create(getApplicationContext(), Uri.parse(PATH_TO_SOUND_EFFECT));
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
}
});
Then replace PATH_TO_SOUND_EFFECT with the background music path.
You can learn for about media players here.
Hope this helps!
I followed this tutorial http://code.tutsplus.com/tutorials/create-a-music-player-on-android-user-controls--mobile-22787 to build an audio player.
All works fine, but my MediaController behave oddly. In fact the controller's seekbar does not update until the user interacts with it.
I tested with API 16 to 19 without success, i have the same problem.
I tested many solution on topics i found on stackoverflow without success also.
Has anyone an idea?
It's odd, but I found that you should update start()/pause() methods of MediaPlayerControl in such way:
#Override
public void pause() {
musicSrv.pausePlayer();
controller.show();
}
#Override
public void start() {
musicSrv.go();
controller.show();
}
I am going to explain the problem i have exactly, in an app i am developing for android i have an activity where i am showing a video that comes from youtube, to show that video i am using a VideoView . Also i have a progressSeekBar to be able to change the current position of the video and to be able to see what is left.
To do that i have done the next:
new Thread(new Runnable() {
public void run() {
try {
Log.d("Voizee", "Vengo al runnable.");
vSeekBarProgress.setProgress(getMediaPlayer()
.getCurrentPosition());
if (getMediaPlayer().getCurrentPosition() < getMediaPlayer()
.getDuration()) {
vSeekBarProgress.postDelayed(this, 1000);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
This code it is working and it is moving perfectly the seekbar when the video plays, but the performance of the video is quite bad and it is shown slowly and jumping through the diferent scenes.
So please could you help me with this problem i have. One more thing to add, it is that if i remove this thread the video it is working well without any performance issue so it is something to do with the thread.
Thank you very much in advance for the help.
What's currently happening with my android application:
I mapped a simple image to a button and have it play a sound on click. On each click, I create a MediaPlayer object with a sound file in my raw folder, I set an OnClickListener for that MediaPlayer object which stops playing the file and releases it, and then I play the MediaPlayer object.
The code for the defined section:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
ImageButton start = (ImageButton) findViewById(R.id.imageButton1);
start.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
MediaPlayer play = MediaPlayer.create(MainActivity.this, R.raw.dvno);
play.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.stop();
mp.release();
mp = null;
}
});
play.start();
}
});
}
What's wrong with it:
It works fine and does not crash, but it's terrible for memory and very slow in general. I'd like for users to click the button many times in a row, as fast as they possibly can, and hear the sound play instantaneously over and over with overlap. Creating a new MediaPlayer object on each click and waiting for it to finish and be released consumes too many resources and the sound often lags behind the actual press of the button. I'd like to be able to create a single sound as MediaPlayer object which can be played while overlapping on itself.
Possible Solution:
Create a single final MediaPlayer object in the scope of onCreate rather than onClick and somehow use threading to start the MediaPlayer object on every click. I read that this might be a possible solution, but I haven't ever used threads before and I don't know if they're slow, if not slower, than my current code, so I'd like to know if there's a solution without using threads that might be simpler. Manipulating the state of a single MediaPlayer to overlap on itself seems impossible at this point, but maybe I'm wrong.
Would this crash the program because of illegal states? If not, is this going to be slower than I want it to be? And if not, can anyone suggest a fix to my code?
I suggest you use a SoundPool instead of MediaPlayer for this.
Tutorial is here
There is a problem in my application,I want to use the seekTo() function with VideoView like this:
videoView.seekTo(time);
videoView.start();
It works well in android 2.2 ,but doesn't work in android 2.3 or higher version...
Some body will tell me why? It troubles me for serval days.
The call to VideoView.start() should be made only after the seek has completed. The call to VideoView.seekTo() initiates a seek but unfortunately VideoView does not support OnSeekCompleteListener needed to notify the seek is actually done.
You can customize VideoView to support OnSeekCompleteListener as shown in my answer to 7990784.
Then you can register to receive onSeekComplete() by calling setOnSeekCompleteListener(). Your implementation of the listener should then call VideoView.start().
Have you tried VideoView class from Vitamio library?
Vitamio
For proper operation of the method seekTo(),the video state should be in PlaybackState.
Checkout the VideoView source here for get more information.
This solution should work.
The problem may be that the mediaplayer inside videoView has not been created.
It's easy to test, by changing the orientation of the device. That's how I tested it.
videoView.setOnPreparedListener(onPreparedListener);
private MediaPlayer.OnPreparedListener onPreparedListener = new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mp.seekTo(videoPosition);
}
};