Java 无法执行onClick
谢谢大家的帮助,原来我在文件路径中为录制和播放按钮包含了一个不存在的目录。 好吧,就这么定了 我在他们的AudioCapture类中使用了Google的示例代码,我正在为一个类做一个项目。我正在创建一个音板应用程序,您可以在其中录制4种不同的声音,并播放它们 我的问题围绕着我的pClick和rClick功能,我有理由相信这与我的Button tempButt line有关。 我感谢你花时间阅读这篇文章,并可能试图帮助我解决我的问题。☺ 我收到的错误是:Java 无法执行onClick,java,android,Java,Android,谢谢大家的帮助,原来我在文件路径中为录制和播放按钮包含了一个不存在的目录。 好吧,就这么定了 我在他们的AudioCapture类中使用了Google的示例代码,我正在为一个类做一个项目。我正在创建一个音板应用程序,您可以在其中录制4种不同的声音,并播放它们 我的问题围绕着我的pClick和rClick功能,我有理由相信这与我的Button tempButt line有关。 我感谢你花时间阅读这篇文章,并可能试图帮助我解决我的问题。☺ 我收到的错误是: package com.example.o
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);
}
}
<?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>
<?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>
更新
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)
这是我的java文件:
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);
}
}
<?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>
<?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>
活动文件:
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);
}
}
<?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>
<?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>
最后,清单:
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);
}
}
<?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>
<?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>
尝试下面的代码
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;
}
这是您的例外,而不是您提到的例外: 原因:android.media.MediaRecorder.start上的java.lang.IllegalStateException(本机方法) 在com.example.oreoman.failuredude.AudioRecordTest.startRecording(AudioRecordTest.j 艾娃:120) 检查mRecorder.prepare()是否正确;抛出异常。如果是,请修复错误 检查setOutputFile()中的路径是否指向现有文件 检查您在清单中是否具有此权限:
<uses-permission android:name="android.permission.RECORD_AUDIO" />
你能发布整个堆栈跟踪吗?真正的原因可能写在你发布的部分下面?你为什么要将pClick
分配给4个按钮?@Berger添加了整个堆栈跟踪。@ShreeKrishna我将pClick分配给4个按钮,因为这是播放录制声音的按钮的方法。我尝试了你的建议,除了使我的其余代码与它兼容之外,我仍然收到了相同的错误。不过,我很感激,我有。任何录制按钮都会使应用程序崩溃。当按下播放按钮时,它们不会崩溃。请尝试在pClick方法中对Play方法调用进行注释,然后重试。问题似乎出现在onRecord内(或之后),而不是onPlay。一旦我发现更多,我会回来的。我的舱单上确实有这一行。与.WRITE\u EXTERNAL\u STORAGE一起使用。您的日志中是否有此异常上面的“prepare()failed”?如果是这样的话,添加如下内容:Log.e(Log_标记,“prepare()failed:+e.getMessage());在您的“尝试捕捉”中,查看错误是什么。还要检查音频文件的路径是否存在。这就是目前的情况。prepare()正在失败。这是因为我猜我没有正确地通过不同的方法传递我的按钮…更改您的Log.e(Log_标记,“prepare()failed”);对此:Log.e(Log_标记,“prepare()失败:”+e.getMessage());看看有什么例外,你是最棒的。事实证明,我有点傻,因为我认为它在录音时会在音频文件的正上方创建一个不存在的目录。谢谢你,好先生,现在很有魅力。我投你哪一部分票?