kkxiaoxian 2017-03-25 07:42 采纳率: 0%
浏览 2207
已结题

编写一个安卓入门小程序,程序闪退,求解

写一个安卓实现录音与播放功能的程序,但是apk运行时一按录音键就闪退,求大神帮忙看看代码,感谢
源文件:
package com.example.recorder;

import java.io.File;
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.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

private static final String LOG_TAG = "AudioRecordTest";
// 语音文件保存路径
private String FileName = null;

// 界面控件
private Button startRecord;
private Button startPlay;
private Button stopRecord;
private Button stopPlay;

// 语音操作对象
private MediaPlayer mPlayer = null;
private MediaRecorder mRecorder = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

//开始录音
startRecord = (Button) findViewById(R.id.startrecord);
// 绑定监听器
startRecord.setOnClickListener(new startRecordListener());

// 结束录音
stopRecord = (Button) findViewById(R.id.stoprecord);
stopRecord.setOnClickListener(new stopRecordListener());

// 开始播放
startPlay = (Button) findViewById(R.id.play);
// 绑定监听器
startPlay.setOnClickListener(new startPlayListener());

// 结束播放
stopPlay = (Button) findViewById(R.id.stop);
stopPlay.setOnClickListener(new stopPlayListener());


FileName = Environment.getExternalStorageDirectory().getAbsolutePath();
FileName = FileName + File.separator + "audiorecordtest.arm";
}

// 开始录音
    class startRecordListener implements OnClickListener {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            mRecorder = new MediaRecorder();
            mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
            mRecorder.setOutputFile(FileName);
            mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
            try {
                mRecorder.prepare();
            } catch (IOException e) {
                Log.e(LOG_TAG, "prepare() failed");
            }
            mRecorder.start();
        }

    }

    // 停止录音
    class stopRecordListener implements OnClickListener {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            mRecorder.stop();
            mRecorder.release();
            mRecorder = null;
        }

    }

    // 播放录音
    class startPlayListener implements OnClickListener {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            mPlayer = new MediaPlayer();
            try {
                mPlayer.setDataSource(FileName);
                mPlayer.prepare();
                mPlayer.start();
            } catch (IOException e) {
                Log.e(LOG_TAG, "播放失败");
            }
        }

    }

    // 停止播放录音
    class stopPlayListener implements OnClickListener {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            mPlayer.release();
            mPlayer = null;
        }

    }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

R文件
/* AUTO-GENERATED FILE. DO NOT MODIFY.
*

  • This class was automatically generated by the
  • aapt tool from the resource data it found. It
  • should not be modified by hand. */

package com.example.recorder;

public final class R {
public static final class attr {
}
public static final class dimen {
/** Default screen margins, per the Android Design guidelines.

     Example customization of dimensions originally defined in res/values/dimens.xml
     (such as screen margins) for screens with more than 820dp of available width. This
     would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively).

     */
    public static final int activity_horizontal_margin=0x7f040000;
    public static final int activity_vertical_margin=0x7f040001;
}
public static final class drawable {
    public static final int ic_launcher=0x7f020000;
}
public static final class id {
    public static final int action_settings=0x7f080005;
    public static final int play=0x7f080002;
    public static final int select=0x7f080004;
    public static final int startrecord=0x7f080000;
    public static final int stop=0x7f080003;
    public static final int stoprecord=0x7f080001;
}
public static final class layout {
    public static final int activity_main=0x7f030000;
}
public static final class menu {
    public static final int main=0x7f070000;
}
public static final class string {
    public static final int action_settings=0x7f050002;
    public static final int app_name=0x7f050000;
    public static final int hello_world=0x7f050001;
}
public static final class style {
    /** 
    Base application theme, dependent on API level. This theme is replaced
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.


        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.


    Base application theme for API 11+. This theme completely replaces
    AppBaseTheme from res/values/styles.xml on API 11+ devices.

API 11 theme customizations can go here.

    Base application theme for API 14+. This theme completely replaces
    AppBaseTheme from BOTH res/values/styles.xml and
    res/values-v11/styles.xml on API 14+ devices.

API 14 theme customizations can go here.
/
public static final int AppBaseTheme=0x7f060000;
/
* Application theme.
All customizations that are NOT specific to a particular API-level can go here.
*/
public static final int AppTheme=0x7f060001;
}
}

  • 写回答

5条回答

  • Android开发-CXY 2017-03-25 08:00
    关注

    需要添加权限,你试试吧

    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘