我正在尝试执行此应用程序:http://developer.android.com/guide/topics/media/audio-capture.html
我的问题是,当我评论行setContentView(ll);一切都很好,但应用程序没有做任何事情。当我解除该行时,它会出现两个buttoms但是当我推动其中一个时,应用程序崩溃了。
我使用的是Android 2.1 AVD,API 7,我添加了sdcard,audioOutput和audioInput。
我添加了以下内容: 在清单文件中。
我做的另一个观察是:我不知道音频文件存储的确切位置,但我不确定这是否存在任何问题。
提前谢谢!
我的代码是:
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;
} }
}
我很抱歉因为这是一场小灾难!
这些是我的错误(我刚刚使用相同的代码创建了一个新的Activity):
12-27 12:04:55.456:E / AndroidRuntime(411):致命异常:主
12-27 12:04:55.456:E / AndroidRuntime(411):java.lang.IllegalStateException
12-27 12:04:55.456:E / AndroidRuntime(411):在android.media.MediaRecorder.start(原生方法)
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):在android.view.View.performClick(View.java:2408)
12-27 12:04:55.456:E / AndroidRuntime(411):在android.view.View $ PerformClick.run(View.java:8816)
12-27 12:04:55.456:E / AndroidRuntime(411):在android.os.Handler.handleCallback(Handler.java:587)
12-27 12:04:55.456:E / AndroidRuntime(411):在android.os.Handler.dispatchMessage(Handler.java:92)
12-27 12:04:55.456:E / AndroidRuntime(411):在android.os.Looper.loop(Looper.java:123)
12-27 12:04:55.456:E / AndroidRuntime(411):在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)
答案 0 :(得分:1)
在与setContentView()
合作之前,您必须致电Views
。在super.onCreate()
行之后立即拨打电话,您应该没事。