0

I'm trying to do this application: http://developer.android.com/guide/topics/media/audio-capture.html

My problem is that when I comment the line setContentView(ll); everything is ok, but the app doesn't do anything. When I discomment that line, it appears the two buttoms but when I push one of them, the app crashes.

I'm using an Android 2.1 AVD, API 7 and I've added the sdcard, audioOutput and audioInput.

I've added the lines: in the manifest file.

Another observation that I've made is: I don't know exactly where the audio file is storaged but I'm not sure if there is any problem with this.

Thank you in advance!

My code is:

package com.example.helloandroid; import java.io.IOException; import android.app.Activity; import android.media.MediaPlayer; import android.media.MediaRecorder; import android.os.Bundle; import android.os.Environment; import android.widget.Button; import android.widget.LinearLayout; import android.view.View; import android.view.ViewGroup; import android.content.Context; import android.util.Log; /** * @author Natalia * */ public class HelloAndroidActivity extends Activity { private static final String LOG_TAG = "AudioRecordTest"; private static String mFileName = null; private RecordButton mRecordButton = null; private MediaRecorder mRecorder = null; private PlayButton mPlayButton = null; private MediaPlayer mPlayer = null; public static final int MODE_WORLD_WRITEABLE=0x00000002; //path = "/sdcard/hola.3gp"; private void onRecord(boolean start) { if (start) { startRecording(); } else { stopRecording(); } } private void onPlay(boolean start) { if (start) { startPlaying(); } else { stopPlaying(); } } private void startPlaying() { mPlayer = new MediaPlayer(); try { mPlayer.setDataSource(mFileName); mPlayer.prepare(); mPlayer.start(); } catch (IOException e) { Log.e(LOG_TAG, "prepare() failed"); } } private void stopPlaying() { mPlayer.release(); mPlayer = null; } private void startRecording() { mRecorder = new MediaRecorder(); mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); mRecorder.setOutputFile(mFileName); try { mRecorder.prepare(); } catch (IOException e) { Log.e(LOG_TAG, "prepare() failed"); } mRecorder.start(); } private void stopRecording() { mRecorder.stop(); mRecorder.release(); mRecorder = null; } class RecordButton extends Button { boolean mStartRecording = true; OnClickListener clicker = new OnClickListener() { public void onClick(View v) { onRecord(mStartRecording); if (mStartRecording) { setText("Stop recording"); } else { setText("Start recording"); } mStartRecording = !mStartRecording; } }; public RecordButton(Context ctx) { super(ctx); setText("Start recording"); setOnClickListener(clicker); } } class PlayButton extends Button { boolean mStartPlaying = true; OnClickListener clicker = new OnClickListener() { public void onClick(View v) { onPlay(mStartPlaying); if (mStartPlaying) { setText("Stop playing"); } else { setText("Start playing"); } mStartPlaying = !mStartPlaying; } }; public PlayButton(Context ctx) { super(ctx); setText("Start playing"); setOnClickListener(clicker); } } public void HelloAndroidActivity() { mFileName = Environment.getExternalStorageDirectory().getAbsolutePath(); mFileName += "/sdcard/hola.3gp"; } @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); LinearLayout ll = new LinearLayout(this); mRecordButton = new RecordButton(this); ll.addView(mRecordButton, new LinearLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 0)); mPlayButton = new PlayButton(this); ll.addView(mPlayButton, new LinearLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 0)); //setContentView(ll); } @Override public void onPause() { super.onPause(); if (mRecorder != null) { mRecorder.release(); mRecorder = null; } if (mPlayer != null) { mPlayer.release(); mPlayer = null; } } } 

I'm sorry because it is a little disaster!

These are my errors (I've just created a new Activity with the same code):

12-27 12:04:55.456: E/AndroidRuntime(411): FATAL EXCEPTION: main

12-27 12:04:55.456: E/AndroidRuntime(411): java.lang.IllegalStateException

12-27 12:04:55.456: E/AndroidRuntime(411): at android.media.MediaRecorder.start(Native Method)

12-27 12:04:55.456: E/AndroidRuntime(411): at Radar.packageRadar.app.Radar2Activity.startRecording(Radar2Activity.java:60)

12-27 12:04:55.456: E/AndroidRuntime(411): at Radar.packageRadar.app.Radar2Activity.onRecord(Radar2Activity.java:30)

12-27 12:04:55.456: E/AndroidRuntime(411): at Radar.packageRadar.app.Radar2Activity.access$0(Radar2Activity.java:28)

12-27 12:04:55.456: E/AndroidRuntime(411): at Radar.packageRadar.app.Radar2Activity$RecordButton$1.onClick(Radar2Activity.java:71)

12-27 12:04:55.456: E/AndroidRuntime(411): at android.view.View.performClick(View.java:2408)

12-27 12:04:55.456: E/AndroidRuntime(411): at android.view.View$PerformClick.run(View.java:8816)

12-27 12:04:55.456: E/AndroidRuntime(411): at android.os.Handler.handleCallback(Handler.java:587)

12-27 12:04:55.456: E/AndroidRuntime(411): at android.os.Handler.dispatchMessage(Handler.java:92)

12-27 12:04:55.456: E/AndroidRuntime(411): at android.os.Looper.loop(Looper.java:123)

12-27 12:04:55.456: E/AndroidRuntime(411): at android.app.ActivityThread.main(ActivityThread.java:4627)

12-27 12:04:55.456: E/AndroidRuntime(411): at java.lang.reflect.Method.invokeNative(Native Method)

12-27 12:04:55.456: E/AndroidRuntime(411): at java.lang.reflect.Method.invoke(Method.java:521)

12-27 12:04:55.456: E/AndroidRuntime(411): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)

12-27 12:04:55.456: E/AndroidRuntime(411): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)

12-27 12:04:55.456: E/AndroidRuntime(411): at dalvik.system.NativeStart.main(Native Method)

1
  • Please paste your code here, so one can see and observe the crash point, as well as paste logs here. Commented Dec 26, 2011 at 18:37

1 Answer 1

1

You must call setContentView() before doing any work with your Views. Place the call right after the super.onCreate() line and you should be fine.

Sign up to request clarification or add additional context in comments.

2 Comments

Do you mean call setContentView() without any argumets after super.OnCreate() and don't change the rest of the code? Because I've tried and I have to pass some argument in setContentView(), I've passed null and 0, but the app crashes again! =(
@NataliaMolineroMingorance, sorry for misleading you, of course the argument to setContentView will be your layout. Thing is that it should be called before you inflate your views.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.