Reputation:
Thank you everyone for your help, it turns out that I had included a non existent directory in the file paths for the recording and play buttons.
Alright, here is the deal.
I used Google's example code for their AudioCapture class, here, for a project that I am doing for a class. I am creating a soundboard app where you should be able to record 4 different sounds, and play them back.
My issue surrounds my pClick and rClick functions, and I have reason to believe that it has to do with my Button tempButt line.
I appreciate you taking your time to read this, and possibly trying to help me with my issue. ☺
The error that I am receiving is this:
updated
04-14 03:16:29.639 22570-22570/com.example.oreoman.failuredude D/OpenGLRenderer: Enabling debug mode 0
04-14 03:16:31.358 22570-22570/com.example.oreoman.failuredude E/AudioRecordTest: prepare() failed
04-14 03:16:31.358 22570-22570/com.example.oreoman.failuredude E/MediaRecorder: start called in an invalid state: 4
04-14 03:16:31.359 22570-22570/com.example.oreoman.failuredude D/AndroidRuntime: Shutting down VM
04-14 03:16:31.359 22570-22570/com.example.oreoman.failuredude W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41702d40)
04-14 03:16:31.367 22570-22570/com.example.oreoman.failuredude E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.oreoman.failuredude, PID: 22570
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3851)
at android.view.View.performClick(View.java:4466)
at android.view.View$PerformClick.run(View.java:18537)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5102)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3846)
at android.view.View.performClick(View.java:4466)
at android.view.View$PerformClick.run(View.java:18537)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5102)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException
at android.media.MediaRecorder.start(Native Method)
at com.example.oreoman.failuredude.AudioRecordTest.startRecording(AudioRecordTest.java:120)
at com.example.oreoman.failuredude.AudioRecordTest.onRecord(AudioRecordTest.java:35)
at com.example.oreoman.failuredude.AudioRecordTest.rClick(AudioRecordTest.java:131)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3846)
at android.view.View.performClick(View.java:4466)
at android.view.View$PerformClick.run(View.java:18537)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5102)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Here is my java file:
package com.example.oreoman.failuredude;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.widget.Button;
import android.view.View;
import android.util.Log;
import android.media.MediaRecorder;
import android.media.MediaPlayer;
import java.io.IOException;
public class AudioRecordTest extends Activity
{
private static final String LOG_TAG = "AudioRecordTest";
private static String mFileA = null;
private static String mFileB = null;
private static String mFileC = null;
private static String mFileD = null;
boolean mStartPlaying = true;
boolean mStartRecording = true;
//private Button tempButt = null;
private MediaRecorder mRecorder = null;
private MediaPlayer mPlayer = null;
private void onRecord(boolean start, String theOne) {
if (start) {
startRecording(theOne);
} else {
stopRecording();
}
}
private void onPlay(boolean start, String theOne) {
if (start) {
startPlaying(theOne);
} else {
stopPlaying();
}
}
private void startPlaying(String theOne) {
mFileA = Environment.getExternalStorageDirectory().getAbsolutePath();
mPlayer = new MediaPlayer();
try {
switch(theOne){
case "mPlayA":
mFileA += "/dakotaApp/audio1.mp4";
mPlayer.setDataSource(mFileA);
break;
case "mPlayB":
mFileB = mFileA;
mFileB += "/dakotaApp/audio1.mp4";
mPlayer.setDataSource(mFileB);
break;
case "mPlayC":
mFileC = mFileA;
mFileC += "/dakotaApp/audio1.mp4";
mPlayer.setDataSource(mFileC);
break;
case "mPlayD":
mFileD = mFileA;
mFileD += "/dakotaApp/audio1.mp4";
mPlayer.setDataSource(mFileD);
break;
}
mPlayer.prepare();
mPlayer.start();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
}
private void stopPlaying() {
mPlayer.release();
mPlayer = null;
}
private void startRecording(String theOne) {
mFileA = Environment.getExternalStorageDirectory().getAbsolutePath();
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
switch(theOne){
case "mRecordA":
mFileA += "/dakotaApp/audio1.mp4";
mRecorder.setOutputFile(mFileA);
break;
case "mRecordB":
mFileB = mFileA;
mFileB += "/dakotaApp/audio1.mp4";
mRecorder.setOutputFile(mFileB);
break;
case "mRecordC":
mFileC = mFileA;
mFileC += "/dakotaApp/audio1.mp4";
mRecorder.setOutputFile(mFileC);
break;
case "mRecordD":
mFileD = mFileA;
mFileD += "/dakotaApp/audio1.mp4";
mRecorder.setOutputFile(mFileD);
break;
}
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
mRecorder.prepare();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
mRecorder.start();
}
private void stopRecording() {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
}
public void rClick(View v) {
String theOne = v.getResources().getResourceEntryName(v.getId());
onRecord(mStartRecording, theOne);
switch(theOne) {
case "mRecordA":
Button tempButt = (Button)findViewById(R.id.mRecordA);
if(mStartRecording) tempButt.setText("Stop Recording");
else tempButt.setText("Start Recording");
break;
case "mRecordB":
tempButt = (Button)findViewById(R.id.mRecordB);
if(mStartRecording) tempButt.setText("Stop Recording");
else tempButt.setText("Start Recording");
break;
case "mRecordC":
tempButt = (Button)findViewById(R.id.mRecordC);
if(mStartRecording) tempButt.setText("Stop Recording");
else tempButt.setText("Start Recording");
break;
case "mRecordD":
tempButt = (Button)findViewById(R.id.mRecordD);
if(mStartRecording) tempButt.setText("Stop Recording");
else tempButt.setText("Start Recording");
break;
}
mStartRecording = !mStartRecording;
}
public void pClick(View v) {
String theOne = v.getResources().getResourceEntryName(v.getId());
onPlay(mStartPlaying, theOne);
switch(theOne) {
case "mPlayA":
Button tempButt = (Button)findViewById(R.id.mPlayA);
if(mStartRecording) tempButt.setText("Stop Playing");
else tempButt.setText("Start Playing");
break;
case "mPlayB":
tempButt = (Button)findViewById(R.id.mPlayB);
if(mStartRecording) tempButt.setText("Stop Playing");
else tempButt.setText("Start Playing");
break;
case "mPlayC":
tempButt = (Button)findViewById(R.id.mPlayC);
if(mStartRecording) tempButt.setText("Stop Playing");
else tempButt.setText("Start Playing");
break;
case "mPlayD":
tempButt = (Button)findViewById(R.id.mPlayD);
if(mStartRecording) tempButt.setText("Stop Playing");
else tempButt.setText("Start Playing");
break;
}
mStartPlaying = !mStartPlaying;
}
@Override
public void onPause() {
super.onPause();
if (mRecorder != null) {
mRecorder.release();
mRecorder = null;
}
if (mPlayer != null) {
mPlayer.release();
mPlayer = null;
}
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_audio_record_test);
}
}
Activity file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".AudioRecordTest">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Dakota's Awesome\nSuper Fun App of Sound!"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textAlignment="center" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start recording"
android:id="@+id/mRecordA"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="70dp"
android:onClick="rClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start playing"
android:id="@+id/mPlayA"
android:layout_alignTop="@+id/mRecordA"
android:layout_marginLeft="220dp"
android:onClick="pClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start recording"
android:id="@+id/mRecordB"
android:layout_below="@+id/mRecordA"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="50dp"
android:onClick="rClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start playing"
android:id="@+id/mPlayB"
android:layout_alignTop="@+id/mRecordB"
android:layout_marginLeft="220dp"
android:onClick="pClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start recording"
android:id="@+id/mRecordC"
android:layout_below="@+id/mRecordB"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="50dp"
android:onClick="rClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start playing"
android:id="@+id/mPlayC"
android:layout_alignTop="@+id/mRecordC"
android:layout_marginLeft="220dp"
android:onClick="pClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start recording"
android:id="@+id/mRecordD"
android:layout_below="@+id/mRecordC"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="50dp"
android:onClick="rClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start playing"
android:id="@+id/mPlayD"
android:layout_alignTop="@+id/mRecordD"
android:layout_marginLeft="220dp"
android:onClick="pClick" />
</RelativeLayout>
Finally, the manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.oreoman.failuredude" >
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name=".AudioRecordTest" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Upvotes: 1
Views: 220
Reputation: 326
This is your exception not the one you mentioned:
Caused by: java.lang.IllegalStateException at android.media.MediaRecorder.start(Native Method) at com.example.oreoman.failuredude.AudioRecordTest.startRecording(AudioRecordTest.j ava:120)
Check if mRecorder.prepare(); throws an exception. If so fix the error.
Check if path in setOutputFile() point to existing files.
Check if you have this permission in manifest:
<uses-permission android:name="android.permission.RECORD_AUDIO" />
Upvotes: 1
Reputation: 2878
try below code
public void pClick(View v) {
//String theOne = v.getResources().getResourceEntryName(v.getId());
onPlay(mStartPlaying, theOne);
switch(v.getId()) {
case R.id.mPlayA:
Button tempButt = (Button)findViewById(R.id.mPlayA);
if(mStartRecording) tempButt.setText("Stop Playing");
else tempButt.setText("Start Playing");
break;
case R.id.mPlayB:
tempButt = (Button)findViewById(R.id.mPlayB);
if(mStartRecording) tempButt.setText("Stop Playing");
else tempButt.setText("Start Playing");
break;
case R.id.mPlayC:
tempButt = (Button)findViewById(R.id.mPlayC);
if(mStartRecording) tempButt.setText("Stop Playing");
else tempButt.setText("Start Playing");
break;
case R.id.mPlayD:
tempButt = (Button)findViewById(R.id.mPlayD);
if(mStartRecording) tempButt.setText("Stop Playing");
else tempButt.setText("Start Playing");
break;
}
mStartPlaying = !mStartPlaying;
}
Upvotes: 0