im trying to make it so when the user opens the app it sets the volume of the music to whatever they have their phones ringer volume at. This is my code so far but im not exactly sure what the paramters on setVolume(float, float) are. The android documentation doesn't explain it well. What is my code doing wrong here?
AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int currentVolume = audio.getStreamVolume(AudioManager.STREAM_RING);
mPlayer = MediaPlayer.create(this, R.raw.song);
mPlayer.setOnErrorListener(this);
if(mPlayer!= null)
{
mPlayer.setLooping(true);
mPlayer.setVolume(currentVolume,1);
}
Looks like audio.setStreamVolume is what you want, but pass in STREAM_MUSIC instead of STREAM_RING.
Note: the music volume and ringer volume are likely to have different maximum values, so you will need to normalize them. Use getStreamMaxVolume for that.
I have not done this before, and I haven't compiled this, but the code should look something like this
AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
// Get the current ringer volume as a percentage of the max ringer volume.
int currentVolume = audio.getStreamVolume(AudioManager.STREAM_RING);
int maxRingerVolume = audio.getStreamMaxVolume(AudioManager.STREAM_RING);
double proportion = currentVolume/(double)maxRingerVolume;
// Calculate a desired music volume as that same percentage of the max music volume.
int maxMusicVolume = audio.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int desiredMusicVolume = (int)(proportion * maxMusicVolume);
// Set the music stream volume.
audio.setStreamVolume(AudioManager.STREAM_MUSIC, desiredMusicVolume, 0 /*flags*/);
Related
I'm developing an app that plays some sound according to specific intervals. And I'm letting the user control those sounds volume levels. Tell now it's OK and the sound volume level is as the user selected before. But the problem here that the device volume levels changed too.
The QUESTION is: How to play my sounds at my volume level without affecting the device sound levels?
defaultSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int streamMaxVolume = mAudioManager.getStreamMaxVolume(3);
vol = defaultSharedPreferences.getInt("vol_one", streamMaxVolume);
mAudioManager.setStreamVolume(3, vol, 0);
playsound();
Update: according to Biraj solution
to get the max allowed volume for each device use the int streamMaxVolume instead of the MAX_VOLUME variable.
so the full answer is:
AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int streamMaxVolume = mAudioManager.getStreamMaxVolume(3);
vol = defaultSharedPreferences.getInt("vol_one", streamMaxVolume);
setVolume(vol);
*
*
*
public void setVolume(int soundVolume){
final float volume = (float) (1 - (Math.log(streamMaxVolume- soundVolume) / Math.log(streamMaxVolume)));
mediaPlayer.setVolume(volume, volume);
}
Don't use AudioManager to setVolume. Use MediaPlayer.setVoume()
private final static int MAX_VOLUME = 100;
public void setVolume(int soundVolume){
final float volume = (float) (1 - (Math.log(MAX_VOLUME - soundVolume) / Math.log(MAX_VOLUME)));
mediaPlayer.setVolume(volume, volume);
}
EXPLANATION :
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
For more information visit HERE
I'm recording male voice, when I click playback button how to convert male voice in to female voice and play it in female voice. I refer some links I tried sound pool to change voice by changing the float rate but I'm not getting female voice and change frequency for audio track.
Here is my code:
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
#Override
public void onLoadComplete(SoundPool soundPool, int sampleId,
int status) {
loaded = true;
}
});
soundID = soundPool.load(mFileName, 1);
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
float actualVolume = (float) audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC);
float maxVolume = (float) audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = actualVolume / maxVolume;
/* if (loaded) {
iv.setImageDrawable(getResources().getDrawable(R.drawable.palyrec));
soundPool.play(soundID, volume, volume, 1, 0, 1.8f);
Log.e("Test", "Played sound");
}*/
int streamID = -1;
do {
iv.setImageDrawable(getResources().getDrawable(R.drawable.palyrec));
streamID = soundPool.play(soundID, volume, volume, 1, 0, 1.7f);
Log.e("Test", "Played sound");
} while(streamID==0);
Changing frequency Code:
Integer[] freqset = {11025, 16000, 22050, 44100};
File file = new File(Environment.getExternalStorageDirectory(), "test.pcm");
int shortSizeInBytes = Short.SIZE/Byte.SIZE;
int bufferSizeInBytes = (int)(file.length()/shortSizeInBytes);
short[] audioData = new short[bufferSizeInBytes];
try {
InputStream inputStream = new FileInputStream(file);
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
DataInputStream dataInputStream = new DataInputStream(bufferedInputStream);
int i = 0;
while(dataInputStream.available() > 0){
audioData[i] = dataInputStream.readShort();
i++;
}
dataInputStream.close();
int sampleFreq = (Integer)spFrequency.getSelectedItem();
AudioTrack audioTrack = new AudioTrack(
AudioManager.STREAM_MUSIC,
sampleFreq,
AudioFormat .CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT,
bufferSizeInBytes,
AudioTrack.MODE_STREAM);
audioTrack.play();
audioTrack.write(audioData, 0, bufferSizeInBytes);
How it is possible to convert to female voice in android ?
I believe there might be a misunderstanding on the term frequency: the sample rate frequency is not the same as the perceived pitch frequency.
Changing sample rate frequency will change both pitch and time, like an audio tape played faster (for those old enough to remember :-)).
Changing pitch frequency upwards will not make it sound faster, but you will rapidly notice the digital artefacts introduced (chipmunk voice).
Might sound a bit better when going downwards (like in Luka's video).
But, in both case, the pitch change you have to apply is quite drastic and will produce some weird side effects.
The closest would be looking at the results obtained by software specialized in harmonizing or tuning signing vocal tracks. These softs look at the formants and do not change them to change the pitch.
e.g.:
https://documentation.apple.com/en/logicstudio/effects/index.html#chapter=10%26section=3%26tasks=true
One of the most famous soft for this :
http://www.celemony.com/en/melodyne/what-is-melodyne
I think that in this case, you might want to slightly change some formants of the voice (which contain some of the characteristics our brain uses to recognize someone's voice) and the overall pitch but independently.
Also note the formants are numbered (F0, F1, F2, F3, ...) and F1 and F2 allow us to tell the difference between the vowel sounds. http://home.cc.umanitoba.ca/~krussll/phonetics/acoustic/formants.html
As Merlevede mentioned, I believe a one size-fits-all algorithm is quite difficult to come up.
Afaict, there are no libraries on android doing this...
You need to work with the frequencies. According to:http://en.wikipedia.org/wiki/Voice_frequency andhttp://www.axiomaudio.com/blog/audio-oddities-frequency-ranges-of-male-female-and-children%E2%80%99s-voices/
A male voice's frequency is 85 to 180 Hz while a female's 165 to 255 Hz. From this, if you simple increase the overall sound frequency, you might get a female-like voice.
The above will not get you a perfect result. To get a perfect result you need to research more. You can get this also by by trial and error. Get a male to say some words. Get a female to say some words. Now, compare the frequencies. Obviously, the frequency of each persons voice is different.
Finally check out this video: http://www.youtube.com/watch?v=hD0HAo2iHbE
I want to add a sound effect when a user clicks on a button.
I tried this:
case R.id.b_all_addresses_addAddress:
// Getting the user sound settings
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
float actualVolume = (float) audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC);
float maxVolume = (float) audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = actualVolume / maxVolume;
if (loaded)
soundPool.play(soundID, volume, volume, 1, 1, 1f);
else
Toast.makeText(this, "could I hate you God more?", Toast.LENGTH_SHORT).show();
in my on create I did this:
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
#Override
public void onLoadComplete(SoundPool soundPool, int sampleId,
int status) {
loaded = true;
}
});
soundID = soundPool.load(this, R.raw.thip, 1);
the sound is just 6 KB.
My Problem
I can't hear the voice
what values are you getting back for volume?
They should be between 0.1 and 0.99. Try using 0.5 as a hard-coded setting and see whether that works. I vaguely remember someone here once reported a "bug" in SoundPool which meant that 0 and 1 meant "mute" and your volume had to be between these two values in order to be heard.
Also: What format is your sound in? not all phones can play all the formats. Some need ogg, some are fine with WAV.
This here works for me on HTC Sensation XE, Android 4.03:
*WAV
*2 Channels
*16 bit PCM
*44.1kHz
This question already has answers here:
Android: how to play music at maximum possible volume?
(5 answers)
Closed 9 years ago.
In my app, I am trying to set the volume when playing an audio clip to the maximum level but it doesn't appear to have any affect. I have to manually adjust the volume to the maximum level. Here's my code:
MediaPlayer mp = new MediaPlayer();
mp.setVolume(1, 1);
AudioManager
int origionalVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);
Use This
it s for total volume
AudioManager mgr = (AudioManager) appContext.getSystemService(Context.AUDIO_SERVICE);
int valuess = 9;//range(0-15)
mgr.setStreamVolume(AudioManager.STREAM_MUSIC, valuess, 0);
it is for left right while current song is playing...
AudioTrack m = (AudioTrack) appContext.getSystemService(Context.AUDIO_SERVICE);
m.setStereoVolume(leftVolume, rightVolume);
it works for me.
Using AudioManager you can control the volume of media player.
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 20, 0);
also from MediaPlayer
public void setVolume (float leftVolume, float rightVolume)
for example you can use this method as,
int maxVolume = 100;
float log1=(float)(Math.log(maxVolume-currVolume)/Math.log(maxVolume));
mp.setVolume(1-log1);
Now I can play a music in android. But I want to play this sound in a random amount of time between 2 to 8 seconds.How Can I randomly play it that for example the first time, it plays for 2 seconds, the next time 7 sec and so on? Can anybody help me?
Go through these links:
Random number Generation
Media Player
Timer
you will get an idea.
Sound in android is played like this :
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
int mSoundID = mSoundPool.load(this, R.raw.sound1, 1);
float lActualVolume = (float) audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC);
float lMaxVolume = (float) audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float lVolume = lActualVolume / lMaxVolume;
// Is the sound loaded already?
if (mSoundIsLoaded) {
mSoundPool.play(mSoundID, lVolume, lVolume, 1, 0, 1f);
I think you have been given plenty of help to figure the random number part out.
You will have to put the sound file in your assets/raw directory.
edit:
I forgot to mention where the mSoundIsLoaded parameter came from.
I set it when my sound has been loaded. I do this in my onCreate method. when the sound is loaded I set the boolean field called mSoundIsLoaded. I do this to prevent NullPointerExceptions when playing the sound
the loading of the sound looks like this:
mSoundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
mSoundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
#Override
public void onLoadComplete(SoundPool soundPool, int sampleId,
int status) {
mSoundIsLoaded = true;
}
});
mSoundID = mSoundPool.load(this, R.raw.sound1, 1);