1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
int duration = 1; // seconds
int sampleRate = 44100;
int numSamples = duration * sampleRate;
double sample[] = new double[numSamples];
double freqOfTone = 3000; // hz
for (int i = 0; i < numSamples; ++i) {
sample[i] = Math.sin(2 * Math.PI * i / (sampleRate/freqOfTone));
}
byte generatedSnd[] = new byte[2 * numSamples];
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);
}
mgr = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
volumeActuel = mgr.getStreamVolume(AudioManager.STREAM_VOICE_CALL);
mgr.setStreamVolume(AudioManager.STREAM_VOICE_CALL, 15, 0);
//mgr.setRouting(AudioManager.MODE_NORMAL, AudioManager.ROUTE_EARPIECE, AudioManager.ROUTE_ALL);
mgr.setMode(AudioManager.MODE_IN_CALL);
mgr.setSpeakerphoneOn(false);
audioTrack = new AudioTrack(AudioManager.STREAM_VOICE_CALL,
sampleRate, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT, numSamples,
AudioTrack.MODE_STATIC);
audioTrack.write(generatedSnd, 0, generatedSnd.length);
audioTrack.play();
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
mgr.setStreamVolume(AudioManager.STREAM_VOICE_CALL, volumeActuel, 0);
audioTrack.stop();
audioTrack.release();
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start(); |
Partager