Android AudioPlayer volume extremly low - java

I am doing a little trivia game that plays a correct or wrong song when the user chooses an answer. This is my function:
public void playSound(boolean isRight) {
if(isRight){
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.correct);
mp.setVolume(VOLUME,VOLUME);
mp.start();
}else{
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.wrong);
mp.setVolume(VOLUME,VOLUME);
mp.start();
}
}
My problem is that even though my audio is properly played, it has an extremly low volume on my device(which has maximun volume).
I already tried many solutions found here on Stackoverflow but the only one that seems to work is this: Android MediaPlayer not playing sound on some devices but it doesnt respect the user's volume.
Any ideas?

Related

Is there a way to play different audio files without calling them each with their own function?

I have 8 audio files that I need to play when a button is pressed.
Currently, these files are played using a class. In this class, each audio file has it's own function that plays and pauses each audio file.
Is there a way to do this without repeating the same function for different audio files?
The button in question is connected to an Adafruit feather - essentially if the button is pressed the phone knows to play whichever audio is required.
Here's some code:
Here's what I have so far:
public class Audio {
MediaPlayer mp;
boolean paused = false;
public void playSound_1(Context context) {
mp = MediaPlayer.create(context, R.raw.Sound_1);
mp.start();
}
public void playSound_2(Context context) {
mp = MediaPlayer.create(context, R.raw.Sound_2);
mp.start();
}
public void playSound_3(Context context) {
mp = MediaPlayer.create(context, R.raw.Sound_3);
mp.start();
}
}
In my fragment I then call the function for each sound:
if (data_check.contains("Sound_1"){
audio.playSound_1(this.getContext());
}
else if (data_check.contains("Sound_2"){
audio.playSound_2(this.getContext());
}
The current results are that if the button is pressed (and data_check finds the name of the Sound) that sound plays.
What I'd like to do is have the same results but without having public void playSound_1, 2, 3, 4... etc.

MediaPlayer using more than once - Android

I have problem with using the same MediaPlayer more than once. I've tried mp.stop() but when i want again to start the same I'm using mp.start() but it wont want to play. Also I've tried mp.reset() and mp.prepare() ,than mp.start() but it wont want to play. So I I've done this:
MediaPlayer mp = MediaPlayer.create(this, R.raw.song);
mp.start(); // using for the first time
mp.release();
// then again I'm creating
mp = MediaPlayer.create(this, R.raw.song);
mp.start(); // second using, the same for more uses
I'm not sure if this is OK, but it works. Is this code good or I should use another solution?
Use the mp.pause() method instead of mp.release(). You can see the lifecycle of the mediaplayer here: http://developer.android.com/reference/android/media/MediaPlayer.html
Your code should look like this if you want to continue from where you paused:
MediaPlayer mp = MediaPlayer.create(this, R.raw.song);
mp.start(); // using for the first time
mp.pause();
mp.start(); // second using, the same for more uses
If you want to play the same song, but start the song again:
MediaPlayer mp = MediaPlayer.create(this, R.raw.song);
mp.start(); // using for the first time
mp.stop();
mp.prepare(); // second using, the same for more uses
mp.start();
If you wan to use another data source, the correct order is the following one:
mp.stop()
mp.reset();
mp.setDataSource(PATH);
mp.prepare();
mp.start();
Check the doc for more info about these calls.

Mediaplayer gap before playing another

I have a few media players setup to play one after another on a button click however there is a noticeable gap between the media players playing ie. once mediaplayer one has finished there is a half a second before mediaplayer two plays, for the purpose I'm using the mediaplayers for the gap in audio is very noticeable, so what I'm asking is, is there a way to remove this gap. Now I don't know if I'm going wrong using different mediaplayers each time however this is what I've come up with so far.
Basically I've got onCompleteionListeners for each mediaplayer and within those I have the next mediaplayer play until the last. Any pointers would be appreciated.
public void onClick(View v) {
mpButtonThree = MediaPlayer.create(MainActivity.this, R.raw.audioReplay);
if (mpButtonThree==null){
//display a Toast message here
return;
}
mpButtonThree.start();
mpButtonThree.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mpButtonThree) {
mpButtonThree.release();
mpButtonOne = MediaPlayer.create(MainActivity.this, audioReplayPrimary);
if (mpButtonOne==null){
//display a Toast message here
return;
}
mpButtonOne.start();
mpButtonOne.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mpButtonOne) {
mpButtonOne.release();
mpButtonTwo = MediaPlayer.create(MainActivity.this, audioReplaySecondary);
if (mpButtonTwo==null){
//display a Toast message here
return;
}
mpButtonTwo.start();
mpButtonTwo.setOnCompletionListener(new soundListener1()
{
});
}
If You play files in series you can use single media player object.
create function that plays next media file and put it into OnCompletion.
This should remove gap between playing, because You don't have to release and create media player every time

Java: Android: MediaPlayer sometimes returning null onResume

I'm trying to get music to play on my app but I've hit a problem which I can't figure out. When I first load the program, it works fine and the music loads and plays. If I let the phone idle and then come back to it, the app works again. But the third time I let it idle and return, the mediaplayer will return a null and crash the app on resuming. Here is the code for the mediaplayer:
public void startMusic(Context context)
{
if (music != null){
if (music.isPlaying()){
music.stop();
}
music.release();
music = null;
}
music = MediaPlayer.create(context, R.raw.song);
music.setLooping(true);
musicPlaying = true;
}
The app will crash when it hits the music.setLooping(true); line of code, saying that music is null.
Any ideas?
It turns out I was releasing the music in the wrong place. I was doing it in the view, when I should have been doing it in the activity.

Android MediaPlayer Clipping on Restart after seekTo

I am using an Android MediaPlayer to play from a local source. It is working well other than one bug when restarting the sound.
public void create() {
FileInputStream in = mApp.openFileInput(mMusicFile);
mp = new MediaPlayer();
mp.setDataSource(in.getFD());
mp.prepare();
mp.setLooping(true);
}
public void play() {
mp.start();
}
public void stop() {
mp.stop();
mp.prepare();
mp.seekTo(0);
}
If I call stop(), then a second or so later call play() I hear a brief clip of the sound where it had been stopped then it sound restarts.
I believe this is a known bug in Android 2.2 Froyo. The prescribed work around is to delete the MediaPlayer and make a new one or fade in to the playback.
The internal buffers are not being flushed after the seek.

Categories

Resources