我正在尝试通过AudioTrack播放bytebeat。但它输出的只是沉默。 IDK为什么会这样,因为我是音频开发的新手。 PS。我已将未捕获的异常处理程序添加到bcontext,因此任何异常都将导致消息。
MainActivity。
package hg.melange.bytebeat;
import android.app.*;
import android.os.*;
import android.widget.*;
import android.view.*;
import android.content.*;
public class MainActivity extends Activity
implements AdapterView.OnItemSelectedListener,
CompoundButton.OnCheckedChangeListener,
YesNoQuestion.Predicate,
Thread.UncaughtExceptionHandler
{
@Override
public void uncaughtException(Thread p1, final Throwable p2)
{
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this)
.setTitle(p2.getClass().getName())
.setMessage(p2.getMessage())
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface p1, int p2)
{
p1.dismiss();
}
});
builder.create().show();
}});
}
@Override
public void yes()
{
try { this.bcontext.stop(); } catch(Throwable t) {}
finally { this.finish(); }
}
@Override
public void no()
{
// TODO: Implement this method
}
@Override
public void onCheckedChanged(CompoundButton p1, boolean p2)
{
if (p1.getId() == R.id.playState)
{
bcontext.setİsPlaying(p2);
}
}
@Override
public void onItemSelected(AdapterView<?> p1, View p2, int p3, long p4)
{
String selected = ((ArrayAdapter<String>) p1.getAdapter()).getItem(p3);
if (p1.getId() == R.id.samplingFreq)
{
bcontext.setSampleRate(Integer.parseInt(selected));
}
else if (p1.getId() == R.id.samplingType)
{
bcontext.setFloatbeat(selected.equalsIgnoreCase("floatbeat"));
}
}
@Override
public void onNothingSelected(AdapterView<?> p1)
{
// TODO: Implement this method
}
Spinner samplingFreq;
Spinner samplingType;
Switch playState;
EditText scriptCode;
TextView errorText;
BytebeatContext bcontext;
@Override
public void onBackPressed()
{
YesNoQuestion ynq = new YesNoQuestion("Are you sure?", "Are you sure you want to exit?");
ynq.setPredicate(this);
ynq.query(this);
if (ynq.getResult()) super.onBackPressed();
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
samplingFreq = (Spinner) findViewById(R.id.samplingFreq);
samplingFreq.setOnItemSelectedListener(this);
ArrayAdapter<String> aptb = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1);
aptb.add("8000");
aptb.add("11025");
aptb.add("22050");
aptb.add("32000");
aptb.add("44100");
samplingFreq.setAdapter((SpinnerAdapter) aptb);
samplingType = (Spinner) findViewById(R.id.samplingType);
samplingType.setOnItemSelectedListener(this);
ArrayAdapter<String> beats = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1);
beats.add("bytebeat");
beats.add("floatbeat");
samplingType.setAdapter((SpinnerAdapter) beats);
playState = (Switch) findViewById(R.id.playState);
playState.setOnCheckedChangeListener(this);
scriptCode = (EditText) findViewById(R.id.scriptCode);
//scriptCode.setOnKeyListener(this);
errorText = (TextView) findViewById(R.id.errorText);
bcontext = new BytebeatContext(8000);
bcontext.setRunning(true);
bcontext.setUncaughtExceptionHandler(this);
bcontext.start();
}
}
BytebeatContext。
package hg.melange.bytebeat;
import android.media.*;
public class BytebeatContext extends Thread
{
private int sampleRate;
private boolean floatbeat;
private boolean isPlaying;
private String code;
public BytebeatContext(int sampleRate)
{
this(sampleRate, false);
}
public BytebeatContext(int sampleRate, boolean floatbeat)
{
this.sampleRate = sampleRate;
this.floatbeat = floatbeat;
}
public void setRunning(boolean running)
{
this.running = running;
}
public boolean isRunning()
{
return running;
}
public void setSampleRate(int sampleRate)
{
this.sampleRate = sampleRate;
}
public int getSampleRate()
{
return sampleRate;
}
public void setFloatbeat(boolean floatbeat)
{
this.floatbeat = floatbeat;
}
public boolean isFloatbeat()
{
return floatbeat;
}
public void setİsPlaying(boolean isPlaying)
{
this.isPlaying = isPlaying;
}
public boolean isİsPlaying()
{
return isPlaying;
}
public void setCode(String code)
{
this.code = code;
}
public String getCode()
{
return code;
}
private int bufsizbytes;
private AudioTrack reinitTrack(AudioTrack old)
{
if (old != null)
{
try
{
old.stop();
old.release();
}
catch (Throwable t)
{
}
}
bufsizbytes = AudioTrack.getMinBufferSize(getSampleRate(),
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT);
AudioTrack newt = new AudioTrack(
AudioManager.STREAM_MUSIC,
getSampleRate(),
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT,
bufsizbytes,
AudioTrack.MODE_STREAM);
//newt.setPlaybackHeadPosition(0);
newt.setStereoVolume(1.0f, 1.0f);
return newt;
}
private boolean running;
private long t;
@Override
public void run()
{
while (running)
{
AudioTrack at = reinitTrack(null);
while (this.isİsPlaying())
{
if (at.getSampleRate() != getSampleRate())
break;
// place output.
short[] out = new short[bufsizbytes];
for(int x=0; x<bufsizbytes; x++)
{
out[(int)(t++)] = (short)(((((t*5&t>>8)&255)/128)-1)*32767);
}
at.write(out, 0, bufsizbytes);
if(at.getPlayState() != AudioTrack.PLAYSTATE_PLAYING)
at.play();
}
t = 0;
at = reinitTrack(at);
}
}
}