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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
| package com.test.test;
import android.app.Activity;
import android.graphics.Color;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.OnGestureListener;
import android.widget.Button;
import android.widget.TextView;
public class test extends Activity {
public AudioTrack at;
public TextView tv;
public TextView Start;
public TextView Stop;
public int minbufsizbytes;
public int bufsizbytes;
public int bufsizsamps;
public final int samprate = 44100;
public short[] buffer=null;
public final float f=1000.0f; //beep freq
public final float MAXVOL=(float)1.0;
public final float MINVOL=(float)0.01;
public final int LOOPON= -1;
public final int LOOPOFF=0;
public int tmp;
int ok=0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Start = (Button)findViewById(R.id.Start);
Stop = (Button)findViewById(R.id.Stop);
Start.setOnClickListener(play);
Stop.setOnClickListener(stop_play);
tv= (TextView)findViewById(R.id.tv1);
minbufsizbytes = AudioTrack.getMinBufferSize(samprate,AudioFormat.CHANNEL_CONFIGURATION_MONO,AudioFormat.ENCODING_PCM_16BIT)*15; //870 at 8000, 4800 at 44100
bufsizbytes=minbufsizbytes; //
tv.setText("minbufsizebytes "+minbufsizbytes+"\n"); //gets to here
bufsizsamps=bufsizbytes/2;
buffer = new short[bufsizsamps];
fillbuf();
try{
at = new AudioTrack(AudioManager.STREAM_MUSIC,samprate,
AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT,bufsizbytes,AudioTrack.MODE_STREAM);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
tv.append("create audiotracksnafu "+e+"\n"); //gets to here
e.printStackTrace();
}
at.setStereoVolume(1.0f,1.0f);
}//oncreate
void fillbuf(){
double omega,t;
double dt=1.0/samprate; //sec per samp
t=0.0;
omega=(float)(2.0*Math.PI*f);
for(int i=0; i < bufsizsamps; i++){
buffer[i]=(short)(32000.0*Math.sin(omega*t));
t+=dt;
}
}
View.OnClickListener play = new View.OnClickListener()
{
//@Override
public void onClick(View view) {
tv.append("in playit!\n"); //
at.play();
ok=1;
while (ok==1){
tmp=at.write(buffer, 0, bufsizsamps);
//SystemClock.sleep(1000L);
tv.append("tmpp="+tmp+"\n"); //
}
}
};
View.OnClickListener stop_play = new View.OnClickListener()
{
//@Override
public void onClick(View view) {
tv.append("in stop!\n"); //
at.stop();
ok=0;
}
};
//----------lifecycle callbacks----------------
@Override
public void onPause(){
super.onPause();
at.pause();
tv.append("onpause!\n"); //gets to here
}
@Override
public void onResume(){
super.onResume();
tv.append("onresume!\n"); //gets to here
// playit();
}
@Override
public void onStop(){
super.onStop();
at.stop();
tv.append("onstop!\n"); //gets to here
}
}//activity |
Partager