I am using the ToneGenarator in order to play a continuous beep.
The problem is that the volume is far too low!
We can't hear anything when the volume is lower than 8.
And with the maximum volume, it is not loud at all...
Is it a limitation of the DTMF sound or am I doing something wrong?
Here is the code:
private final int TONE_TYPE = ToneGenerator.TONE_DTMF_5;
private final int STREAM = AudioManager.STREAM_MUSIC;
private final int DOT_TIME = 3;
public SoundManager(Activity anActivity) {
audio = (AudioManager) anActivity
.getSystemService(Context.AUDIO_SERVICE);
generator = new ToneGenerator(STREAM,
audio.getStreamMaxVolume(STREAM));
}
private void playBeep() {
generator.startTone(TONE_TYPE, DOT_TIME);
}
Use this one, For using Current System Volume.
ToneGenerator toneG = new ToneGenerator(AudioManager.STREAM_SYSTEM, 100);
I found where the problem was:
ToneGenerator takes a volume between 0 and 100, while the stream volume is between 0 and 15.
Thus, audio.getStreamMaxVolume(STREAM) gives 15 out of 100, it is low...
Related
I`m trying to make an android app that can control Bluetooth volume.
till now i managed to change Bluetooth volume value for calls. using this piece of code :
audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
int streamMaxVolume = audioManager.getStreamMaxVolume(6);
int lVol = ((volumeValue / 100) * streamMaxVolume);
audioManager.setStreamVolume(6, lVol, 0);
now the problem that I have is I cannot find the int value for streamType which is allocated for Bluetooth Media volume.
well I found the answer.
looks like after a bluetooth device connect to the phone. in order to change the bluetooth media volume the app should wait for some amount of time. I`ve set my method to run its codes after 10sec. here it is :
public void strat(BtDevice btDevice){
new Timer().schedule(
new TimerTask() {
#Override
public void run() {
AudioManager audioManager = (AudioManager) myContex.getSystemService(Context.AUDIO_SERVICE);
int volumeValue = btDevice.getMusicVolume();
int streamMaxVolume = audioManager.getStreamMaxVolume(3);
int lVol = ((volumeValue / 100) * streamMaxVolume);
audioManager.setStreamVolume(3, lVol, AudioManager.FLAG_SHOW_UI);
}
},
10000
);
I am trying to develop an application which plays one of the predefined arrays of sounds continuously and also change the the tempo,pitch (individually) etc using soundpool class of android.
I know how to change the pitch of a sound.
But I do not know how to play the whole array and also the tempo of that whole array.
Please help!
Thank you in advance!
EDIT : I think I will have to use Handler in order to play the array of sounds, but i don't know how!
public class Sound {
private static int length = 22050 * 10; //10 seconds long
private static byte[] data = new byte[length];
static void fillRandom() {
new Random().nextBytes(data); //Create some random noise to listen to.
}
static void play() {
fillRandom();
final int TEST_SR = 22050; //This is from an example I found online.
final int TEST_CONF = AudioFormat.CHANNEL_OUT_MONO;
final int TEST_FORMAT = AudioFormat.ENCODING_PCM_16BIT;
final int TEST_MODE = AudioTrack.MODE_STATIC; //I need static mode.
final int TEST_STREAM_TYPE = AudioManager.STREAM_ALARM;
AudioTrack track = new AudioTrack(TEST_STREAM_TYPE, TEST_SR, TEST_CONF, TEST_FORMAT, length, TEST_MODE);
track.write(data, 0, length);
track.play();
}
}
This code play some random noises. All you have to do is fill the byte[]data array with the proper music files you intend to play...
I know that code below is the max volume
mAudioManager.getStreamMaxVolume
how can i get the minimum volume?
I used this code but not the minimum volume
mAudioManager.getStreamVolume(0);
This my code
int Minvolume=myAudioManager.getStreamVolume(0);
int current=myAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
int volume=(int) (current-1);
if (current>=Minvolume){
myAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, volume, 0);
handler.postDelayed(this, 10000);
System.out.println("ADJUST LOWER");
}
else if (current!=Minvolume){
System.out.println("volume is minimum");
}
Try the following to reduce the volume of the music stream...
for (int volume = myAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC) - 1; volume >= 0; volume--) {
//
// add your delay code here
//
// Call a method to set absolute volume
setVolume(volume);
}
private void setVolume(int volume) {
myAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, volume, 0);
}
A better approach to increase or decrease volume would be to use adjustVolume.
public void decreaseStreamVolume(int volType, AudioManager am){
am.adjustStreamVolume(volType, AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI + AudioManager.FLAG_PLAY_SOUND);
}
You can enter AudioManager.STREAM_MUSIC or any desirable stream and AudioManager.ADJUST_LOWER or AudioManager.ADJUST_HIGHER as per your requirement. I believe it's much cleaner way to execute.
Solved: I forgot the track.play(); at the end...
I want to play a sound on my Android Smartphone (4.0.4 Api level 15).
I tried to hear some random noise, but its not working:
public class Sound {
private static int length = 22050 * 10; //10 seconds long
private static byte[] data = new byte[length];
static void fillRandom() {
new Random().nextBytes(data); //Create some random noise to listen to.
}
static void play() {
fillRandom();
final int TEST_SR = 22050; //This is from an example I found online.
final int TEST_CONF = AudioFormat.CHANNEL_OUT_MONO;
final int TEST_FORMAT = AudioFormat.ENCODING_PCM_16BIT;
final int TEST_MODE = AudioTrack.MODE_STATIC; //I need static mode.
final int TEST_STREAM_TYPE = AudioManager.STREAM_ALARM;
AudioTrack track = new AudioTrack(TEST_STREAM_TYPE, TEST_SR, TEST_CONF, TEST_FORMAT, length, TEST_MODE);
track.write(data, 0, length);
}
}
I have played a little bit with the variabels, but could not get it to work.
All you have left to do is play it. Add this line to the end of your play() function:
track.play();
I am writing an app that dynamically creates a midi file which I then want to play. Each time I press a button in my app a new midi file is created and written to external storage. I then want the app to read that file and play it. I am using SoundPool to play the file. However, from the samples I have found, SoundPool is initialized during onCreate and therefore my file is only read once. Meaning the same file is played everytime I hit the button regardless of any changes I make to it. I have tried moving the SoundPool initialisation into my Audio1ButtonHandler method but then I get no sound or get a selection of “sample x not ready” errors. The relevant code from my app is here:
public void onCreate(Bundle savedInstanceState) {
…………
soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
soundPoolMap = new HashMap<Integer, Integer>();
soundPoolMap.put(SOUNDPOSITION, soundPool.load("/sdcard/test.mid", 1));
}
public void Audio1ButtonHandler(View target) {
ArrayList<Integer> chord = new ArrayList<Integer>();
chord = Chord.calculateChord(kSelection, cSelection);
for (int i = 0; i < chord.size(); i++) {
System.out.println("chord value is " + chord.get(i).toString());
}
Midi.createMidiFile(chord);
playSound();
releaseSound();
}
public void playSound() {
AudioManager mgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
float streamVolumeCurrent = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
float streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = streamVolumeCurrent / streamVolumeMax;
soundPool.play(soundPoolMap.get(SOUNDPOSITION), volume, volume, 1, 0, 1f);
}
public void releaseSound() {
soundPoolMap.clear();
soundPool.release();
}
As you can see I have tried to release the resources (within releaseSound) to try and force them to be re-read but to no avail.
Any pointers would be greatly received.
Thanks
First stop playing before you release! Then, after release, set soundPool to null.