Note that there are some explanatory texts on larger screens.

plurals
  1. POcode with thread crashes at startup (android beginner)
    primarykey
    data
    text
    <p>I am trying to make an application that passes through the audio samples obtained at the microphone to the speaker. This is the source code: </p> <pre><code>public class MainActivity extends Activity { AudioManager am = null; AudioRecord record =null; AudioTrack track =null; final int SAMPLE_FREQUENCY = 44100; final int SIZE_OF_RECORD_ARRAY = 1024; boolean isPlaying = false; class MyThread extends Thread{ @Override public void run(){ recordAndPlay(); } } MyThread newThread; private void init() { int min = AudioRecord.getMinBufferSize(SAMPLE_FREQUENCY, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT); record = new AudioRecord(MediaRecorder.AudioSource.VOICE_COMMUNICATION, SAMPLE_FREQUENCY, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, min); int maxJitter = AudioTrack.getMinBufferSize(SAMPLE_FREQUENCY, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT); track = new AudioTrack(AudioManager.MODE_IN_COMMUNICATION, SAMPLE_FREQUENCY, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, maxJitter, AudioTrack.MODE_STREAM); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setVolumeControlStream(AudioManager.STREAM_MUSIC); init(); newThread.start(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } private void recordAndPlay() { short[] lin = new short[SIZE_OF_RECORD_ARRAY]; int num = 0; am = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE); am.setMode(AudioManager.MODE_IN_COMMUNICATION); record.startRecording(); track.play(); while (true) { num = record.read(lin, 0, SIZE_OF_RECORD_ARRAY); track.write(lin, 0, num); } } public void passStop(View view){ Button playBtn = (Button) findViewById(R.id.playBtn); // /* if(!isPlaying){ record.startRecording(); track.play(); isPlaying = true; playBtn.setText("Pause"); } if(isPlaying){ record.stop(); track.pause(); isPlaying=false; playBtn.setText("Pass through"); } // */ } @SuppressWarnings("deprecation") @Override public void onDestroy(){ newThread.stop(); } } </code></pre> <p>Unfortunately, this program stops as soon as I try to run it through eclipse. This is wht I get in the logcat but I am not sure what it all means: </p> <pre><code>08-19 18:58:43.365: D/AndroidRuntime(27915): Shutting down VM 08-19 18:58:43.365: W/dalvikvm(27915): threadid=1: thread exiting with uncaught exception (group=0x4161f700) 08-19 18:58:43.365: E/AndroidRuntime(27915): FATAL EXCEPTION: main 08-19 18:58:43.365: E/AndroidRuntime(27915): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mypassthrough/com.example.mypassthrough.MainActivity}: java.lang.NullPointerException 08-19 18:58:43.365: E/AndroidRuntime(27915): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211) 08-19 18:58:43.365: E/AndroidRuntime(27915): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 08-19 18:58:43.365: E/AndroidRuntime(27915): at android.app.ActivityThread.access$600(ActivityThread.java:141) 08-19 18:58:43.365: E/AndroidRuntime(27915): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 08-19 18:58:43.365: E/AndroidRuntime(27915): at android.os.Handler.dispatchMessage(Handler.java:99) 08-19 18:58:43.365: E/AndroidRuntime(27915): at android.os.Looper.loop(Looper.java:137) 08-19 18:58:43.365: E/AndroidRuntime(27915): at android.app.ActivityThread.main(ActivityThread.java:5103) 08-19 18:58:43.365: E/AndroidRuntime(27915): at java.lang.reflect.Method.invokeNative(Native Method) 08-19 18:58:43.365: E/AndroidRuntime(27915): at java.lang.reflect.Method.invoke(Method.java:525) 08-19 18:58:43.365: E/AndroidRuntime(27915): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 08-19 18:58:43.365: E/AndroidRuntime(27915): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 08-19 18:58:43.365: E/AndroidRuntime(27915): at dalvik.system.NativeStart.main(Native Method) 08-19 18:58:43.365: E/AndroidRuntime(27915): Caused by: java.lang.NullPointerException 08-19 18:58:43.365: E/AndroidRuntime(27915): at com.example.mypassthrough.MainActivity.onCreate(MainActivity.java:46) 08-19 18:58:43.365: E/AndroidRuntime(27915): at android.app.Activity.performCreate(Activity.java:5133) 08-19 18:58:43.365: E/AndroidRuntime(27915): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 08-19 18:58:43.365: E/AndroidRuntime(27915): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175) 08-19 18:58:43.365: E/AndroidRuntime(27915): ... 11 more </code></pre> <p>What could be the reason for this code crashing, and how can it be debugged? I am pretty sure it has something to do with <code>Thread</code>, because my other versions of this code where I have not included Thread at all do not crash on startup.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload