Could not find how to ask a question about "Playing an arbitrary tone with Android" post by Steve Pomeroy, so started one here.
Is there any code that needs to be added to an xml file? Could not get the sim to make sound.
public class PlaySound extends Activity {
// originally from http://marblemice.blogspot.com/2010/04/generate-and-play-tone-in-android.html
// and modified by Steve Pomeroy <steve@staticfree.info>
private final int duration = 3; // seconds
private final int sampleRate = 8000;
private final int numSamples = duration * sampleRate;
private final double sample[] = new double[numSamples];
private final double freqOfTone = 440; // hz
private final byte generatedSnd[] = new byte[2 * numSamples];
Handler handler = new Handler();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
protected void onResume() {
super.onResume();
// Use a new tread as this can take a while
final Thread thread = new Thread(new Runnable() {
public void run() {
genTone();
handler.post(new Runnable() {
public void run() {
playSound();
}
});
}
});
thread.start();
}
void genTone(){
// fill out the array
for (int i = 0; i < numSamples; ++i) {
sample[i] = Math.sin(2 * Math.PI * i / (sampleRate/freqOfTone));
}
// convert to 16 bit pcm sound array
// assumes the sample buffer is normalised.
int idx = 0;
for (final double dVal : sample) {
// scale to maximum amplitude
final short val = (short) ((dVal * 32767));
// in 16 bit wav PCM, first byte is the low order byte
generatedSnd[idx++] = (byte) (val & 0x00ff);
generatedSnd[idx++] = (byte) ((val & 0xff00) >>> 8);
}
}
void playSound(){
final AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
sampleRate, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT, generatedSnd.length,
AudioTrack.MODE_STATIC);
audioTrack.write(generatedSnd, 0, generatedSnd.length);
audioTrack.play();
}
}
is there something like:
activity android:soundEffectsEnabled="true"
or
uses-permission android:name="android.permission.WRITE_SETTINGS"
that needs to be added so the above code will make sound in a simulator such as eclipse?
i have added activity android:soundEffectsEnabled="true" and uses-permission android:name="android.permission.WRITE_SETTINGS"/, but still will not make sound.
thought it was duration of sound, because when duration was set to 10 instead of 1, it made a beep, but was very short. however, after the third time of running it, an inflateException is thrown.
duration over 500 causes an out of memory error, which is what through the exception. however, duration of 100 still only makes a very short beep, can barley hear it, the mouse click is louder.
duration of over 250 is a memory hug.
duration of 10 makes as long of a click as duration of 250. generatedSnd.length of 10 makes as long of a click as generatedSnd.length of 15k have changed the freqOfTone from 100 up to 55000.
still can not figure out how to make sound longer.