0
В настоящее время я пытаюсь создать генератор многоволновых сигналов на Android с помощью AudioTrack, но, тем не менее, я пытаюсь реализовать квадратную волну, я получаю ту же фатальную ошибку, я не могу найти ее вокруг, любая помощь будет полезно вот мой код:Ошибка Android AudioTrack
package com.example.jack.synthesiser;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ToggleButton;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
EditText frequencyInput;
TextView displayFrequency;
ToggleButton startStop;
ToggleButton startStopSquare;
PlayWave wave = new PlayWave();
PlaySquare sWave = new PlaySquare();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeView();
}
private void initializeView() {
frequencyInput = (EditText) findViewById(R.id.editText);
startStop = (ToggleButton) findViewById(R.id.toggleButton);
startStopSquare = (ToggleButton) findViewById(R.id.toggleButton2);
displayFrequency = (TextView) findViewById(R.id.textView);
startStop.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int frequency = Integer.parseInt(frequencyInput.getText().toString());
displayFrequency.setText(String.valueOf(frequency));
wave.setWave(frequency);
sWave.setSquareWave(frequency);
boolean on = startStop.isChecked();
boolean square = startStopSquare.isChecked();
if (on) {
wave.start();
} else if (!on){
wave.stop();
}
//if (square) {
// sWave.start();
//} else if (!square) {
// sWave.stop();
//}
}
}
И мой класс Play волна выглядит следующим образом:
package com.example.jack.synthesiser;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
/**
* Created by Jack on 16/01/2017.
*/
public class PlayWave {
private final int SAMPLE_RATE = 44100; //maximum sample rate Audiotrack can use
private AudioTrack mAudio;
int buffsize = AudioTrack.getMinBufferSize(SAMPLE_RATE, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
private int sampleCount;
public PlayWave() {
mAudio = new AudioTrack(AudioManager.STREAM_MUSIC, SAMPLE_RATE, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT,
buffsize, AudioTrack.MODE_STATIC);
}
public void setWave (int frequency) {
sampleCount = (int) ((float) SAMPLE_RATE/frequency);
short samples[] = new short[sampleCount]; //array to hold samples
int amplitude = 32767; //max amplitude can reach
double twoPi = 8. * Math.atan(1.); //twoPi calculated as the arctangent of 1 multiplied by 8
double phase = 0.0;
for (int i = 0; i < sampleCount; i++) {
samples[i] = (short) (amplitude * Math.sin(phase));
phase += twoPi * frequency/SAMPLE_RATE;
}
mAudio.write(samples, 0, sampleCount);
}
public void setSquareWave (int frequency) {
sampleCount = (int) ((float) SAMPLE_RATE/frequency);
short samples[] = new short[sampleCount];
int amplitude = 32767;
double phase = 0.0;
double twoPi = 8. * Math.atan(1.);
for (int i = 0; i < sampleCount; i++) {
samples[i] = (short) (amplitude * Math.sin(phase));
if (samples[i] > (short) 0.0) {
samples[i] = 32767;
}
if (samples[i] < (short) 0.0) {
samples[i] = -32767;
}
phase += twoPi * frequency/SAMPLE_RATE;
}
}
public void start() {
mAudio.reloadStaticData();
mAudio.setLoopPoints(0, sampleCount, -1);
mAudio.play();
}
public void stop() {
mAudio.stop();
}
}
ошибка я получаю это:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.jack.synthesiser, PID: 18957
java.lang.IllegalStateException: play() called on uninitialized AudioTrack.
at android.media.AudioTrack.play(AudioTrack.java:1141)
at com.example.jack.synthesiser.PlayWave.start(PlayWave.java:61)
at com.example.jack.synthesiser.MainActivity.onClick(MainActivity.java:47)
at android.view.View.performClick(View.java:4780)
at android.widget.CompoundButton.performClick(CompoundButton.java:120)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)