Duvida sobre AudioTrack (emissão de Frequências sonoras)
Boa tarde, estou tentando criar um emissor de frequências sonoras audíveis pelo ser humano, desafio de um professor, mas não consigo fazer o som ficar continuo ao clicar no seekBar e ser parado ao clicar no botão stop, ou mudar de frequência ao mover o SeekBar, alguém poderia me ajudar? segue o código que estou utilizando.
public class MainActivity extends AppCompatActivity { Button Play,Stop; Boolean recording = false; TextView freque; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); freque = (TextView) findViewById(R.id.txtfreq); SeekBar Frequencia = (SeekBar) findViewById(R.id.seekFreq); Play = (Button)findViewById(R.id.Play); Stop = (Button)findViewById(R.id.Stop); Play.setOnClickListener(PlayOnClickListener); Stop.setOnClickListener(StopOnClickListener); Frequencia.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int i, boolean b) { freque.setText("Frequencia: " + i); while(recording){ playSound(i,44100); } } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }); } View.OnClickListener PlayOnClickListener = new View.OnClickListener(){ @Override public void onClick(View arg0) { Thread recordThread = new Thread(new Runnable(){ @Override public void run() { recording = true; } }); recordThread.start(); }}; View.OnClickListener StopOnClickListener = new View.OnClickListener(){ @Override public void onClick(View arg0) { recording = false; }}; private void playSound(double frequency, int duration) { // AudioTrack definition if (recording == true){ int mBufferSize = AudioTrack.getMinBufferSize(44100, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_8BIT); final AudioTrack mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, mBufferSize, AudioTrack.MODE_STREAM); // Sine wave final double[] mSound = new double[4410]; final short[] mBuffer = new short[duration]; for (int i = 0; i < mSound.length; i++) { mSound[i] = Math.sin((2.0*Math.PI * i/(44100/frequency))); mBuffer[i] = (short) (mSound[i]*Short.MAX_VALUE); } mAudioTrack.setStereoVolume(AudioTrack.getMaxVolume(), AudioTrack.getMaxVolume()); mAudioTrack.play(); mAudioTrack.write(mBuffer, 0, mSound.length); mAudioTrack.stop(); mAudioTrack.release(); }} }
Anatan
Curtidas 0