dole doug
dole doug

Reputation: 36038

android sound to speaker

On my dummy android app I try to replicate the functionality of an audio player. The files I have to deal with, are some ogg files which are not too long: 2-3 seconds.

The problem is that the volume of the files is too low. I'll like to augment the volume but I don't know how. In my app, the sounds are played on a volume equivalent to the one from phone call. I'll like to augment the volume, to the one from the voice call when I use the speaker mode.

    AssetFileDescriptor afd;

    try {
        afd = getAssets().openFd(strPlayFileName);
        mPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());

        mPlayer.prepare();
        afd.close();
        mPlayer.start();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

That is the code that plays my ogg files. Any feedback is welcome.

Upvotes: 3

Views: 2128

Answers (5)

Ajay Kumar Meher
Ajay Kumar Meher

Reputation: 1952

It's not a good idea to use media player for very short audios. Rather you will get another option to do so. Use SoundPool to play such files.

  1. Reasons why I'm suggesting is Almost all game uses this to play there effects. And we know how simple it is.

  2. You can use FX to play around your media.

Here is a great example. Just use it.

I'm sure you will get a great result.

If you still want to use MediaPlayer try playing media from RAW folder. It wont compress the files. I did not tested it.

Upvotes: 0

R KiranKumar
R KiranKumar

Reputation: 835

private AudioManager audio;

audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE); 


@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
        case KeyEvent.KEYCODE_VOLUME_UP:
            audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
                    AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
            return true;
        case KeyEvent.KEYCODE_VOLUME_DOWN:
            audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
                    AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
            return true;
        default:
            return false;


     }
}

try this..

Upvotes: 1

Ankit
Ankit

Reputation: 1148

@dole

I'll like to augment the volume, to the one from the voice call when I use the speaker mode.

You want to get full volume, here is the solution

AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
                audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, audioManager.getStreamMaxVolume(audioManager.STREAM_MUSIC), 0);

This will set volume to the max volume.

You can refer following link for audio manager workings here.

Upvotes: 0

Tobias Ritzau
Tobias Ritzau

Reputation: 3327

If I understand your question correctly you want to add gain to the media, i.e. play them at more than 100% volume. One way to do that is to use the new capabilities in JB. I have not played with this myself, but it should for sure be possible to get a buffer from the decoder and multiply the values in the buffer with your gain, and then pass it on to be played. You find more info here.

Upvotes: 0

Gan
Gan

Reputation: 1399

MediaPleyer allows for volume manipulation. You can try if the setVolume() method to see if it provides the desired result. You can get more info on this method here.

If this does not give you the desired result, I would suggest that you amplify the sound output of the source using some of the free audio editors such as audacity.

Upvotes: 1

Related Questions