我試圖做到這一點的應用程序:http://developer.android.com/guide/topics/media/audio-capture.html的setContentView麻煩
我的問題是,當我發表意見線的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月12日至27日:04:55.456:E/AndroidRuntime(411):致命異常:主要
12月12日至27日:04:55.456:E/AndroidRuntime(411):java.lang.IllegalStateException
12月12日至27日:04:55.456:E/AndroidRuntime(411):在android.media.MediaRecorder .start(Native Method)
12-27 12:04:55.456:E/A ndroidRuntime(411):在Radar.packageRadar.app.Radar2Activity.startRecording(Radar2Activity.java:60)
12月12日至27日:04:55.456:E/AndroidRuntime(411):在Radar.packageRadar.app.Radar2Activity .onRecord(Radar2Activity.java:30)
十二月12日至27日:04:55.456:E/AndroidRuntime(411):在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 a ndroid.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月12日至27日:04:55.456:E/AndroidRuntime(411):在android.os.Handler.handleCallback(Handler.java:587)
12月12日至27日:04:55.456:電子/ 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月12日至27日:04:55.456:E/AndroidRuntime(411):在java.lang.reflect.Method.invokeNative(本機方法)
12月12日至27日: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。 (ZygoteInit.java:868)
12-27 12:04:55.456:E/AndroidRuntime(411) .java:626)
12月12日至27日:04:55.456:E/AndroidRuntime(411):在dalvik.system.NativeStart.main(本機方法)
請在此處粘貼您的代碼,以便人們可以看到並觀察碰撞點,並在此處粘貼日誌。 – AAnkit 2011-12-26 18:37:25