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.
Related
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 .
I have followed Igor Khrupin's tutorial on streaming mp3 files in Android using the MediaPlayer class: http://www.hrupin.com/2011/02/example-of-streaming-mp3-mediafile-with-android-mediaplayer-class
This is all working fine except that the primary progress of the seek bar is not updating correctly. It stops just after the beginning. On debugging this, I can see that the MediaPlayer.getCurrentPosition() method is always returning 261 (milliseconds) even though the mp3 song is playing fine.
Has anyone come across this issue before or got any ideas on how to fix it?
Many thanks
I also had the same problem. I solved the problem by determining the type of stream using the setAudioStreamType(int) method:
Uri myUri = ....; // initialize Uri here
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(getApplicationContext(), myUri);
mediaPlayer.prepare();
mediaPlayer.start();
I found the problem here. Firstly, the emulator I was running on did not have audio turned on, so I needed to add the audio enabled boolean in the AVD manager in eclipse. Not having audio turned on is what was causing it to always return 261 milliseconds. There was also a second problem. See the 2 code snippets below. The first was my code and the second is the code from the tutorial. There is a subtle difference (number of brackets) which causes the first one to always evaluate to 0.
seekBarProgress.setProgress((int)((float) mediaPlayer.getCurrentPosition() / getMp3Duration()) * 100);
seekBarProgress.setProgress((int) (((float) mediaPlayer.getCurrentPosition() / getMp3Duration()) * 100));
I'm working on some music analysis using the Visualizer class on Android 2.3.1. I am finding that the FFT and waveform magnitudes are affected by the volume of the device. This means that if the user has the volume turned down I receive little or not FFT data.
I've tested this on a Motorola Xoom, Samsung Galaxy Tab and the emulator and it behaves this way.
I am using the code below:
mp = new MediaPlayer();
mp.setDataSource("/sdcard/sine1.wav");
mp.prepare();
mp.setLooping(true);
mp.start();
int audioSessionID = mp.getAudioSessionId();
v = new Visualizer(audioSessionID);
v.setEnabled(true);
Looking at the docs for the Visualizer class it seems that if we are passing in a valid audio session id then the visualizer should operate upon this audio session. It appears that the Visualizer is operating upon the output mix.
Has anyone else encountered this or found a way around it?
Thanks
I was also facing the same problem, but it is working when i am enabled the Eqaulizer and Visualizer for same seession id.I dont know the reason for it ,i checked it remove the equalizer from visualizer class in api demos it is working as you said.
Equalizer mEqualizer = new Equalizer(0, SessionId);
mEqualizer.setEnabled(true); // need to enable equalizer
Visualizer mVisualizer = new Visualizer(SessionId);
There are two options for the Visualizer scaling mode:
SCALING_MODE_AS_PLAYED and SCALING_MODE_NORMALIZED
If you want the Visualizer to be normalized, as in it's consistent no matter what the volume is, then use SCALING_MODE_NORMALIZED.
mVisualizer.scalingMode = Visualizer.SCALING_MODE_AS_PLAYED
Keep in mind though that this drastically changes the values being sent to the Visualizer, so other adjustments may be needed.
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'm using MediaPlayer to play back some videos in an Android application, and they are noticeably faster on my device as when viewed on a computer.
Is there any way to control the playback speed of these videos in order to slow them down?
Beginning API 23, MediaPlayer can set playback speed using this method.
Class MediaPlayer
public void setPlaybackParams (PlaybackParams params) Added in API
level 23
Sets playback rate using PlaybackParams. Parameters params
PlaybackParams: the playback params. Throws IllegalStateException if
the internal player engine has not been initialized.
IllegalArgumentException if params is not supported.
Sample code:
MediaPlayer mp = ...; //Whatever
float speed = 0.75f;
mp.setPlaybackParams(mp.getPlaybackParams().setSpeed(speed));
For API < 23, refer to this SO question.