简体   繁体   English

Android AudioTrack错误

[英]Android AudioTrack Error

I'm currently trying to build a multi-waveform oscillator on Android using AudioTrack but however I try implementing the square wave I get the same fatal error, I cant seem to find any way around it, any help would be useful here's my code: 我目前正在尝试使用AudioTrack在Android上构建多波形振荡器,但是我尝试实现方波时遇到了同样的致命错误,我似乎找不到任何解决方法,任何帮助都对我有用,这是我的代码:

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();
   //}





}
}

And my Play wave class looks like this: 我的Play wave类如下所示:

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();

}
}

The error i get is this: 我得到的错误是这样的:

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)

Check this answer here 在这里检查这个答案

It recommends to call play() before using any write() methods. 建议在使用任何write()方法之前先调用play()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM