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.
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.
I m developing an Android app which can play streaming videos and navigate through the app with the video playing in background.
The video is playing on a Service and to watch the video i set the MediaPlayer's surface to a SurfaceTexture of a TextureView. No problems here, this works fine.
The problem comes up when the user minimizes the player (without problem) and wants to re-open it. Here, when i re-set the Surface to the MediaPlayer the video jumps one or two seconds back and continues reproducing fine. Here is the code when i set the surface to the MediaPlayer:
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
surfaceTexture = new Surface(surface);
if (myMusicService != null) {
myMusicService.getMediaPlayer().setSurface(surfaceTexture);
}
}
I tried with a SurfaceView too (instead a TextureView) with the same result.
Any ideas?
Thanks,
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.
I am trying to find a video player library that I can add to my Android App source that will allow the user to play the video at a slower speed, ideally adjustable by the user.
Also, I need the player to allow for two videos to be on screen at once, with separate controls.
I have looked at a couple of players available http://www.vitamio.org/ (can't play two videos), and http://wiki.videolan.org/AndroidCompile (I don't have access/experience on Linux machine to compile source for Android).
Android 6.0 added PlaybackParams class to control playback behavior. -
Use setPlaybackParams method of MediaPlayer as given below -
videoview = (VideoView)findViewById(R.id.videoview);
videoview.setVideoURI("Your Video URI");
videoview.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
//works only from api 23
PlaybackParams myPlayBackParams = new PlaybackParams();
myPlayBackParams.setSpeed(0.5f); //here set speed eg. 0.5 for slow 2 for fast mode
mp.setPlaybackParams(myPlayBackParams);
videoview.start();//start your video.
}
});
You can adjust speed of video by using setSpeed method of PlaybackParams .
Such Problem: I have video file recorded with two sound channels. I tried to switch off left sound channel by this code:
MediaPlayer mp;
....
mp.setVolume(0.f, 1f);
... and on Tablet this work good (right volume channel sounds well). But then I tried it on googleTv which I connect to Samsung UE46ES6307U and this code did not work, sound swichs off.
Maybe it is bounds to Dolby Digital Plus / Dolby Pulse audio? Can I somehow programmatically discover how sound channels device has, and what volume in each chanels setuped?
Update:
On this forum http://www.googletvforum.org/forum/logitech-revue/375-audio-problems-logitech-revue.html in one of replies such message: "Logitech has not yet figured out how to pipe multichannel audio thru hdmi.you have to use the optical output. Which is ok."
"How are you constructing the MediaPlayer?"
Videoview vv;
...............
vv.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.setVolume(0.f, 1f);
}
});
Update:
public class MainActivity extends Activity {
MediaPlayer mp = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (mp != null) {
mp.reset();
mp.release();
}
mp = MediaPlayer.create(this, R.raw.test);
mp.start();
}
public void onTurnOffLeft(View v){
mp.setVolume(0.f, 1.f);
}
public void onTurnOffRight(View v){
mp.setVolume(1.f, 0.f);
}
}
Method onTurnOffLeft switchs off all sound, and onTurnOffRight method has no effect.
Update2
I tried to play .ogg audio file codded with Vorbis codec - channels turns off well. But I tried to play video files codded with mp3, ac3, pcm, aac - and problem with turning off channels is still there... I need to turn off audio channels in video, but how to solve that problem, I do not know yet.
The MediaPlayer object is backed by different libraries across the devices (not the same between a tablet and a Google TV). How are you constructing the MediaPlayer?
One thing you may want to try is calling #reset() on the MediaPlayer right after it is constructed. By default when you use a "new" operator to construct a MediaPlayer instance it is in an IDLE state (at least on Google TV). By calling reset you allow your own OnErrorListener.onError() handler to be invoked. This will let you see if there is some underlying error that is not visible otherwise.
You may also want to look at AudioManager#setStreamVolume(int, int, int) which sets the volume of ALL streams of a particular type.
Edit 1:
Since you are just grabbing the VideoView from layout (I'm guessing since that code was omitted) after you setup the listener you should call reset on the video view.