Android MediaPlayer getCurrentPosition returns 0 - android

I am using a MediaPlayer to play several audio files. I have a custom MediaController based on androids MediaController. The problem I am having is that when I pause the player the controller shows the current position as 0 until I hit play again. I have traced it down to the media player returning 0 as its position while paused. Is this the intended behavior?
Thanks,
Nathan

A simple fix would be put up a static int that catches the currentposition of the media player before it is stopped or paused.
int currentPos = mediaPlayer.CurrentPosition;
mediaPlayer.Stop();
mediaPlayer.Release();
stopIsActive = true;
WriteSeekingToDataBase(currentPos, CurrentSongObject);

Related

Visualizer Returns all zeros when using queued MediaPlayer

I created a MediaPlayer then queued the next MediaPlayer using setNextMediaPlayer.
I then created a Visualizer based on the first player. All works well until the song changes over and then the visualizer just sends -128's or zeros.
I tried using OnInfoListener to catch the MEDIA_INFO_STARTED_AS_NEXT event and use the passed in MediaPlayer to recreate the visualization. I verified this player is playing, but it still doesn't receive any valid data.
This is how I create the Visualizer. NOTE I release() the old Visualizer right before calling this.:
public Visualizer createVisualizer(MediaPlayer _player) {
if (_player != null) {
Visualizer visualizer = new Visualizer(_player.getAudioSessionId());
visualizer.setScalingMode(Visualizer.SCALING_MODE_NORMALIZED);
visualizer.setCaptureSize(Visualizer.getCaptureSizeRange()[0]);
visualizer.setDataCaptureListener(this, Visualizer.getMaxCaptureRate() / 4, true, false);
visualizer.setEnabled(true);
return visualizer;
}
return null;
}
The only thing I can think of is it has something to do with reusing the listener, but I really have no idea at this point.
I was able to get this working by forcing the audioSessionId of the next player to be the same as the first player by using:
playerNext.setAudioSessionId(playerFirst.getAudioSessionId());

Android MediaPlayer or SoundPool?

When I use mediaplayer on click button, after several clicks the sound disappears. When I use soundpool, the time is too short to play the effect sound. Can anybody please help me with this?
As I understood your audio is 20 s. So you can use this code on your button click:
if (mediaPlayer != null){
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
mediaPlayer = MediaPlayer.create(this, R.raw.audio_file);
mediaPlayer.start();
The first if statement is to take care that it will keep playing how much ever you clicked the button.

Mediaplayer with two audio tracks

I have a problem with mediaplayer.
int inlevel=0;
int ingame=1;
MediaPlayer[] music = new MediaPlayer[2];
music[inlevel] = MediaPlayer.create(ctx, R.raw.jazz);
music[ingame] = MediaPlayer.create(ctx, R.raw.funky);
music[inlevel].start();
When the game begins I call a function for fadeout the music inlevel
fadeout();
music[inlevel].pause();
music[ingame].setVolume(volume, volume);
music[ingame].start();**
When the game finishs:
music[ingame].pause();
music[ingame].seekTo(0);
music[inlevel].start();
I don't need the music in level restart from the begin. I need that only for the music ingame.
Everything works good. I have only a problem the second time the game starts. Never happens at the first time.
At the begin of music in game(**), there is a very short piece of the music inlevel. Maybe it is the buffer of Mediaplayer.
I tried with two istance of Mediaplayer, but no sound.
Any solution? Thanks.

Reload data source in android media player

I'm having a hard time with media player in android. Basically I try to play a song from my sd card which is downloading (some sort of streaming). So after the song was downloaded (20%) the song starts to play and if I leave it like this it works fine until the end. But if I try to seek at the end (over 20%) obviously it won't work because my seek position is over EOF file so I made this code inside a method
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(sFilePath);
mp.prepare();
int offset = (progress * mp.getDuration()) / 100;
if (sCompleted) return;
sLastSeek = offset;
if (offset > sMediaPlayer.getDuration()) {
sMediaPlayer.reset();
sMediaPlayer.setDataSource(sFilePath);
sMediaPlayer.prepare();
} else {
sMediaPlayer.seekTo(offset);
}
Where sMediaPlayer is my mediaPlayer, and sFilePath is the file path. In theory the case I presented should be covered but when I seek over the file length the player is reseted (as the code says) but is not playing and this is very awkward and is over my powers.
Maybe the mediaPlayer stopt because it reaches the end of the song. Either start it again, refresh the file in steady intervals (say every 5 secs), or just use onCompletionListener. In the last case, when the file ends, you can reload the file and continue playing... mp.getCurrentPosition() to get where it left off playing.
I also noticed you refer to sMediaPlayer, but your MediaPlayer is defined as mp.

Android Media Player - Getting the number of times a video played in SetLooping

I am Playing the video in loop using set Looping(true) option and i will stop the media player after a particular event happened. It is working fine. But i want to know the number of times that my video got played in the loop.
MediaPlayer doesn't provide that information, you need to make it loop yourself and count how many times it has restarted. To do this, something in your app will need to extend OnCompletionListener and do something like
int count = 0;
public void onCompletion(MediaPlayer mediaPlayer) {
count++;
mediaplayer.seekTo(0);
mediaplayer.start();
}
And you need to set mediaPlayer.setLooping(false)

Categories

Resources