I am displaying the videos in a list. After playing the videos one after another it is displaying video can't play error in android naught only in Remaining all the versions are working fine.Please help me to resolve this issue. Below is the my code for playing the video in video view.
videoview.setVideoURI(Uri.parse(streamInfo.video_streams.get(0).url));
viewHolder.large_videoview.seekTo(starttime * 1000);
viewHolder.large_videoview.start();
videoview.setOnPreparedListener(new OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
mMediaPlayer = mp;
mp.setOnInfoListener(new MediaPlayer.OnInfoListener() {
#Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
Log.e("large_videoview", "<><>onInfo");
if (what == MediaPlayer.MEDIA_INFO_BUFFERING_START)
//Log.e("buffering starts", "buffering starts");
if (what == MediaPlayer.MEDIA_INFO_BUFFERING_END)
Log.e("buffering ends", "<><>buffering ends");
return false;
}
});
}
});
getting this error in my log cat
E/MediaPlayer: invoke failed: wrong state 0, mPlayer(0x7a30b0e0)
E/MediaPlayer: Error (1,-19)
E/MediaPlayer: Error (1,-1010)
Are you sure you are doing like this?
videoview.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
videoview.start();
}
});
i.e wait for VideoView to be prepared & once it's prepared, you call start() on it?
Related
I have some problem with seeking video played in VideoView and resuming it.
Here's some part of my code:
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
mp.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
#Override
public void onSeekComplete(MediaPlayer mp) {
Log.d("V", "onSeekComplete1 " + videoView.getCurrentPosition());
videoView.start();
Log.d("V", "onSeekComplete1 " + videoView.getCurrentPosition());
}
});
}
});
videoView.setVideoURI('some local uri');
At some point I'm pausing video using:
videoView.pause();
After button click I'm seeking video to certain time, eg. 10 seconds (10 000 ms).
#OnClick(R2.id.ivMove)
void clickMove() {
videoView.seekTo(10000);
}
After that in log I see:
onSeekComplete1 10000
onSeekComplete2 0
So after calling videoView.start() it is starting video from beginning!
But when I call start() without seeking video (eg. one button calls pause() and second calls start() then video is playing correctly - from the moment when it was paused, not from beginning.
Could you tell me what I'm doing wrong?
The problem was caused by MediaPlayer - it is seeking to closest keyframe of the video and it turns out that my testing video has keyframes every ~30 seconds.
I changed to ExoPlayer https://github.com/google/ExoPlayer - it can seek to exact time.
There is a new overload for the MediaPlayer seekTo method. It has a second int parameter, in which you can pass a SEEK_CLOSEST constant. According to Google, "This mode is used with seekTo(long, int) to move media position to a frame (not necessarily a key frame) associated with a data source that is located closest to or at the given time." In testing my own app, it solves the problem for API level >= 26. I'm not sure if there are any good options prior to API level 26.
Try videoView.resume() instead of start()
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
mp.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
#Override
public void onSeekComplete(MediaPlayer mp) {
Log.d("V", "onSeekComplete1 " + videoView.getCurrentPosition());
videoView.resume();
Log.d("V", "onSeekComplete1 " + videoView.getCurrentPosition());
}
});
}
});
videoView.setVideoURI('some local uri');
Try this solution. It works like charm....
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
mp.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
#Override
public void onSeekComplete(MediaPlayer mp) {
mp.start();
}
});
}
});
I'm creating a simple soundboard to play sounds when a user clicks a button. Problem is, if the button is pressed enough ( usually around 10 times ) it will eventually stop playing and show the error E/MediaPlayer: error (1, -19)
what am I doing wrong? My code that plays the sound:
private void playSound(int soundID){
final MediaPlayer mp = MediaPlayer.create(this,soundID);
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mp.start();
}
});
}
You should use Soundpool. It's created exactly for playing short sound effects. And it's much simpler to use than MediaPlayer. MediaPlayer should only be used for playing regular/large music.
See here for example:
http://www.vogella.com/tutorials/AndroidMedia/article.html#tutorial-play-sounds-via-soundpool
Note: No need to use onTouch as in the example, you can just use onClick for simplicity.
Reason: "W/Choreographer: (let say) Frame time is 0.239384 ms in the future! Check that graphics HAL is generating vsync timestamps using the correct timebase."
i.e, there is a much gap between timestamps.
This problem can be removed by using setOnCompletionListener() within your OnClick() method like: `
#Overrid
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
// Get the {#link Word} object at the given position the user clicked on
Word word = words.get(position);
Log.v("NumbersActivity","Current word: "+word);
// Create and setup the {#link MediaPlayer} for the audio resource associated with the current word
MediaPlayer mMediaPlayer = MediaPlayer.create(NumbersActivity.this, word.getAudioResourceId());
// Start the audio file
mMediaPlayer.start();
// Keep timeStamp sync
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
mMediaPlayer.release();
}
});
}
`
private void playSound(int soundID){
final MediaPlayer mp = MediaPlayer.create(this,soundID);
mp.start();
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
mp.release();
}
});
}
I'm using VideoView to play remote videos using HLS
When video is VOD (video on demand - just a remote video file, no live stream) everything works fine.
Problems start when i try to play live streams.
If I'm on the fast network (wifi or 3g/4g) everything works as expected.
If i try to play video over edge network for example, video starts to load, and that's OK.
But if i click back button, whole application just freezes. The weird thing is, that ANR dialog is not shown at all, and the freeze sometimes last more that 20 seconds
Here is the code
...
MediaController mediaController = new MediaController(this);
videoView.setMediaController(mediaController);
videoView.setVideoURI(Uri.parse(Utility.sanitizeUrl(videoUrl)));
videoView.setOnErrorListener(new MediaPlayer.OnErrorListener()
{
#Override
public boolean onError(MediaPlayer mp, int what, int extra)
{
tvError.setVisibility(View.VISIBLE);
videoView.setVisibility(View.GONE);
pbLoading.setVisibility(View.GONE);
return true;
}
});
//we also set an setOnPreparedListener in order to know when the video file is ready for playback
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
{
public void onPrepared(MediaPlayer mediaPlayer)
{
// close the progress bar and play the video
pbLoading.setVisibility(View.GONE);
//if we have a position on savedInstanceState, the video playback should start from here
if(savedVideoPosition > 0)videoView.seekTo(savedVideoPosition);
if (savedVideoPosition == 0)
{
videoView.start();
}
else
{
//if we come from a resumed activity, video playback will be paused
videoView.pause();
}
}
});
...
#Override
protected void onDestroy()
{
super.onDestroy();
videoView.stopPlayback();
}
The only thing i could fine in the logs is this
07-09 10:00:57.665 10499-10499/{package_name_removed} I/Choreographer﹕ Skipped 9565 frames! The application may be doing too much work on its main thread.
If playing a video from the device, the media player knows its exact lenght and fast-forwarding through the video by moving the thumb in the mediacontroller seekbar is seemless.
If playing a streaming video from the internet, the length of the video is unknown and seeking using the thumb causes the video to stop responding.
I want to know how can I disable the thumb until the video is fully buffered? Then show it and enable fast-forwarding.
Following is my video player code:
setContentView(R.layout.layout_fragment_video_fullscreen);
vvVideoFullscreen = (VideoView) findViewById(R.id.vvVideoFullscreen);
rlVideoFullscreen = (RelativeLayout) findViewById(R.id.rlVideoFullscreen);
videoUri = Uri.parse(video.getUrl());
vvVideoFullscreen.setVideoURI(videoUri);
mediaController = new MediaController(this);
mediaController.setMediaPlayer(vvVideoFullscreen);
mediaController.setAnchorView(vvVideoFullscreen);
vvVideoFullscreen.setMediaController(mediaController);
vvVideoFullscreen.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
progressDialog.dismiss();
rlVideoFullscreen.setVisibility(View.VISIBLE);
vvVideoFullscreen.start();
mediaController.show(3000);
}
});
vVideoFullscreen.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
finish();
}
});
vvVideoFullscreen.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
if (vvVideoFullscreen.isPlaying()) {
vvVideoFullscreen.pause();
mediaController.hide();
} else {
vvVideoFullscreen.start();
mediaController.show();
}
}
return true;
}
}); // End of setOnTouchListener
Have you tried adding an onBufferingUpdateListener() to your mediaplayer?
From the documentation is says:
public abstract void onBufferingUpdate (MediaPlayer mp, int percent)
Called to update status in buffering a media stream received through
progressive HTTP download. The received buffering percentage indicates
how much of the content has been buffered or played. For example a
buffering update of 80 percent when half the content has already been
played indicates that the next 30 percent of the content to play has
been buffered.
So when this callback is invoked with 100%, you know your video is fully buffered.
To disable all playback controls, you can simply only call mediaController.setMediaPlayer(...) when buffering is completed.
If you want to only disable the seek thumb, I suggest to implement your own MediaController.MediaPlayerControlInterface and rout all calls to your MediaPlayer object. Seeking can then be disabled by having the canSeekForward() and canSeekBackward() methods return false and by making the seekTo(int pos) method don't do anything on your MediaPlayer.
I am working on customized video play in android. I am using following code for video play.
mp.reset();
mp.setDataSource(songsList.get(songIndex).get("songPath"));
mp.prepare();
mp.start();
videoview.setVideoPath(songsList.get(songIndex).get("songPath"));
videoview.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
//pDialog.dismiss();
videoview.start();
}
})
I am facing problem that sound repeats twice & so its mixing during video play. Can you please guide me what will be the issue. Kindly help.
You are starting the same file twice, try the following...
videoview.setVideoPath(songsList.get(songIndex).get("songPath"));
videoview.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
//pDialog.dismiss();
videoview.start();
}
})