Control the playback speed of Android MediaPlayer - android

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.

Related

Get media player to play first on right speaker and then on left speaker

I would like to play an audio file that starts on the left speaker and then switches to the right speaker.
I have tried doing something like this:
MediaPlayer mp = new MediaPlayer();
// Setup audio file
mp.start();
mp.setVolume(1.0F, 0F);
// Delay a second or two (I actually use a Handler and the postDelayed method)
mp.setVolume(0F, 1.0F);
but the sound comes through on both speakers the whole time.
How can I play audio in Android with either the left or right speaker muted (or at reduced volume)?
EDIT:
I got the correct behavior for a while while I was testing my app, but then it returned to what I described above with the exact same code base. Based on this, is there anything else I could check to find out what's going on?
One option would be
Start mediaplayer with setVolume(1.0F, 0F);
When you want to switch to other speaker, get current position of media player by using getCurrentPosition() method.
Then stop media player.
Then again start with setVolume(0F,1.0F);
Seek to the positin you got in 2nd step using seekTo() method
Done.
Overhead:This method may cause you some delay
It looks like you are doing it correctly according to the Android API http://developer.android.com/reference/android/media/MediaPlayer.html
public void setVolume (float leftVolume, float rightVolume)
Sets the volume on this player. This API is recommended for balancing the output of
audio streams within an application. Unless you are writing an application to control
user settings, this API should be used in preference to setStreamVolume(int, int, int)
which sets the volume of ALL streams of a particular type. Note that the passed volume
values are raw scalars in range 0.0 to 1.0. UI controls should be scaled logarithmically.
Parameters
leftVolume left volume scalar
rightVolume right volume scalar
My best advice is to try 0.0F instead of just 0F and then maybe trying to set the volume before you start playing the track then transition while it's playing.

What Android video player libraries allow for slow motion playback?

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 .

custom media player error on Android 2.3

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.

How can I setRate for Android MediaPlayer?

How can I implement
setRate(float f)
for my Android MediaPlayer, and secondly is it posible?
I believe this is the function you are looking for.
This sets the sampling rate at which the audio data will be consumed and played back, not the original sampling rate of the content. Setting it to half the sample rate of the content will cause the playback to last twice as long, but will also result in a negative pitch shift. The valid sample rate range is from 1Hz to twice the value returned by getNativeOutputSampleRate(int).
If you want to play mp3 directly using AudioTrack, you can either have a look at this example or convert your mp3 file to wav format, which enables AudioTrack to use it without hassle. This is the tradeoff you should account for if you want to adjust the playback rate easily.
Android 6.0 adds PlaybackParams for MediaPlayer, so you can now do this:
String recordingPath = recordingDirectory + File.separator + "music.mp3";
MediaPlayer audioPlayer = MediaPlayer.create(getApplicationContext(), Uri.parse(recordingPath));
audioPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
PlaybackParams params = new PlaybackParams();
params.setSpeed(0.75f);
audioPlayer.setPlaybackParams(params);
audioPlayer.start();
I don't have an Android 6 device yet, but this works for me in the emulator.
Based on the Android developer documentation, you may have to use SoundPool instead.
Android Developer: Media SoundPool-setRate
public final void setRate (int streamID, float rate)
Change playback rate. The playback rate allows the application to vary
the playback rate (pitch) of the sound. A value of 1.0 means playback
at the original frequency. A value of 2.0 means playback twice as
fast, and a value of 0.5 means playback at half speed. If the stream
does not exist, it will have no effect.
Parameters
streamID: a streamID returned by the play() function
rate: playback rate (1.0 = normal playback, range 0.5 to 2.0)

Android Visualizer FFT / waveform affected by device volume?

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.

Categories

Resources