I have got an error concerning MediaPlayer in combination with a surfaceview:
I am playing a video in a surfaceview by Streaming it from the Internet as follows:
mPreview = (SurfaceView) findViewById(R.id.surface);
videoFL = (FrameLayout) findViewById(R.id.videoFL);
SurfaceHolder holder = mPreview.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
Lateron I try to attach my first MediaPlayer to it:
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(videos.get(position));
mediaPlayer.prepareAsync();
mediaPlayer.setOnBufferingUpdateListener(this);
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.setOnPreparedListener(this);
mediaPlayer.setOnVideoSizeChangedListener(this);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDisplay(holder);
While Streaming the first video I am already buffering the second one to save my users time. To attach the second already prepared MediaPlayer instance to my SurfaceView I do the following an release the old one:
mediaPlayer.release();
mediaPlayer = null;
....
mediaPlayer2.setDisplay(holder);
On ICS and Jelly Bean this solution works fine but on Gingerbread it doesn't work as planned. The video is being played but not displayed (black screen and only once a picture is shown somewhere within the video) which I recognized via:
mediaPlayer2.getCurrentPosition();
where I always get the correct position on my SeekBar. I have tried it on an HTC Wildfire S.
Has anyone possibly a guess why this error could happen on Gingerbread?
With regards,
tsemann
I did reduce my version now to one MediaPlayer because as I stated in my question it is true that some 2.x devices do not have the capability to use a second MediaPlayer instance in the same Activity.
So I always reinstantiate my MediaPlayer by calling the prepare()-method for the upcoming video.
Related
In my Android Application I want to reproduce the streaming videos.
For now I currently use the video view but It might works better.. I have this problems :
some files it doesnt execute -> error (1 ,-1005)
video streaming buffering starts after long times
What do you suggest to use instead of videoview ?
I see more Applications that play streaming video inside the app without this problems, but I don't understand what they using for that result. Maybe webview with javascript player ???
you can try surfaceView :
SurfaceView surfaceView = (SurfaceView)findViewById(R.id.surfaceView);
SurfaceHolder surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(new SurfaceHolder.Callback() {
#Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
mediaPlayer.setDataSource(STREAM_URL);
mediaPlayer.setDisplay(surfaceHolder);
mediaPlayer.start();
}
If your min sdk is 16 and above use Exo player
You will find detailed instructions here.
My task - play video on SurfaceView, after stop show image in SurfaceView
My code for play
mPlayer.setOnCompletionListener(onCompletionListener);
mPlayer.setDisplay(view.getHolder());
mPlayer.reset();
mPlayer.setDataSource(file.getAbsolutePath());
mPlayer.prepare();
mPlayer.start();
all ok video playing
in onCompletion
mPlayer.reset();
mPlayer.setDisplay(null);
view.setBackgroundDrawable(Drawable.createFromPath(file.toString()));
but image is transparend and on background i see last video frame - its no good. How clear surface?
I try use
holder.lockCanvas()
but this return null, why?
I have a same problem and i fixed it by using these two lines after player release.
surfaceViewHolder.setFormat(PixelFormat.TRANSPARENT);
surfaceViewHolder.setFormat(PixelFormat.OPAQUE);
I tried to make android custom videoplayer, and its working fine then i play music.
But i got error (1,-38) on 2.3 then trying display video on surface.
// Mediaplayer
mp = new MediaPlayer();
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
{
public void onPrepared(MediaPlayer mp)
{
isPrepared = true;
mp.start();
}
});
mp.setDataSource("http://commonsware.com/misc/test2.3gp");
mp.prepareAsync();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setDisplay(surfaceHolder);//if comment this string - player is working fine
How to fix it?
What is difference between android 2.3 and 4.0 mediaplayer?
Here is how you can do it :
mp.setDataSource(url);
mp.setOnPreparedListener(this);
mp.prepareAsync();
public void onPrepared(MediaPlayer player) {
mp.start();
}
EDIT :
I think you should set an error listener using setOnErrorListener to see if you get any error when calling setDataSource.
Mine issue turned to be that I provided wrong url for the video playback. I had two urls -one for the raw data and one for progress streaming-comapatible format. I had supplied the first one by mistake. Swapping them fixed my issue. Older Android versions have poorer support for HLS video streaming (that's why the differnce on the different Android versions):
Android 2.3 (Gingerbread)
No Support, despite being the most popular version of Android
Android 3.0 (Honeycomb)
Streams cause tablet devices to crash
Android 4.0 (Ice Cream Sandwich)
VOD streams do not seek
Aspect ratios are not detected and cause image deformation
Fullscreen causes videos to restart from the beginning
Android 4.1+ (Jelly Bean)
Aspect ratio issue is fixed, but seek is still unavailable
Chrome does not understand HLS leading to broken mimetype detection
Taking video fullscreen causes devices to throw an error and stop.
This data is taken from here.
To solve the problem of MediaPlayer error (1,-38) after calling mediaPlayer.start() on Android 2.3 device, just add the following clause after setting up the surface holder.
...;
surfaceHolder_.addCallback(this);
surfaceHolder_.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mediaPlayer_ = new MediaPlayer();
...;
For devices equal to or higher than Honeycomb, we don't need to set the surface holder type, the framework will handle that automatically for the attached surface view to the media player, that's why the code works in Android 4.0 but not in 2.3.
I'm stumped.
I'm trying to play video with the Media Player but while the audio plays, all I get is a black window.
I've seen other posts about this problem but I haven't been to find a solution. I have tried to follow their suggestions.
The mediaplayer is prepared before playback. The surface holder was created and set to the media player's display before playback.
Tested on a Samsung Galaxy Tab and a Samsung Galaxy S.
I'm compiling against API level 7.
The video itself can be played in device's video application from the sdcard, so it should be compatible.
The surface view is not the same size as the video. So that might be an issue. Do I need to do something about that or is stretching handled automatically?
Here's what I have in my initialization:
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(width, height);
lp.leftMargin = x;
lp.topMargin = y;
mSurfaceView = new SurfaceView(mActivity);
mSurfaceView.requestFocus();
mSurfaceView.setZOrderOnTop(true);
mSurfaceView.getHolder().addCallback(player);
mSurfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mLayout.addView(mSurfaceView, lp);
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(assetDescriptor.getFileDescriptor(), assetDescriptor.getStartOffset(), assetDescriptor.getLength());
mMediaPlayer.setOnErrorListener(player);
mMediaPlayer.setOnPreparedListener(player);
mMediaPlayer.prepare();
and here are my callbacks:
public void onPrepared(MediaPlayer mp)
{
mMediaPlayer.start();
}
public void surfaceCreated (SurfaceHolder holder)
{
mMediaPlayer.setDisplay(holder);
}
What's frustrating is that a version of the code was working properly a while ago, but now it's not.
I had a similar problem, and was related to the video format (codec MP4, WMV, AVI, etc).
Try running the video on default player of the Android, see if that works. If not works, then it may be problem in video codec.
Do not try to run the video on players like VLC or Player MX, they have embedded codec.
I am working on video player in android.when i created surface holder in on create method ,it did not create.but when i created the surface holder in button onclick method,its created
My coding is,
preview=(VideoView)findViewById(R.id.surface);
preview.setEnabled(true);
preview.bringToFront();
holder=preview.getHolder();
holder.setFixedSize(400, 400);
mp=new MediaPlayer();
mp.setDataSource("path");
mp.setDisplay(holder);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setOnBufferingUpdateListener(playerActivity.this);
mp.setOnPreparedListener(playerActivity.this);
mp.prepare();
mp.start();
mp.prepare();
mp.start();
prepare() is asynchronous, which means, it might not be finished when you already call the mp.start. What do you mean by 'surface not created'? Do you just mean the video doesn't play?
Anyway, you should use an MediaPlayer.OnPreparedListener and start the media in onPrepared().