简单录音级播放

1:权限



2:布局

<Button
    android:id="@+id/startRecorder"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="开始录制" />

<Button
    android:id="@+id/stopRecorder"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="停止录制" />

<Button
    android:id="@+id/startPlay"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:text="播放录音" />

<Button
    android:id="@+id/stopPlay"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:text="停止播放" />

3:代码

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private Button startRecorder;
private Button stopRecorder;
private Button startPlay;
private Button stopPlay;
 private MediaPlayer mediaPlayer;
 private MediaRecorder recorder;
 private File audo;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initView();
}

private void initView() {
    startRecorder = (Button) findViewById(R.id.startRecorder);
    stopRecorder = (Button) findViewById(R.id.stopRecorder);
    startPlay = (Button) findViewById(R.id.startPlay);
    stopPlay = (Button) findViewById(R.id.stopPlay);

    startRecorder.setOnClickListener(this);
    stopRecorder.setOnClickListener(this);
    startPlay.setOnClickListener(this);
    stopPlay.setOnClickListener(this);

    mediaPlayer=new MediaPlayer();
    //录音机
    recorder=new MediaRecorder();
    //录制原音
    recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);

    //输出格式
    recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);

    //编码格式
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);


    //根目录下
    File file = Environment.getExternalStorageDirectory();
    audo=new File(file,"wangzeyun.amr");
    try {
        audo.createNewFile();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.startRecorder:
            try {

               recorder.setOutputFile(audo.getAbsolutePath());
                recorder.prepare();
                recorder.start();
            } catch (IOException e) {
                e.printStackTrace();
            }

            break;
        case R.id.stopRecorder:
               recorder.stop();
               recorder.release();
            break;
        case R.id.startPlay:
            try {
                startPlay();
            } catch (IOException e) {
                e.printStackTrace();
            }
            break;
        case R.id.stopPlay:
            mediaPlayer.stop();
            break;
    }
}

private void startPlay() throws IOException {
    //重置
    mediaPlayer.reset();
        mediaPlayer.setDataSource(audo.getAbsolutePath());

        if (!mediaPlayer.isPlaying()){
            mediaPlayer.prepare();
            mediaPlayer.start();
        }
        else {
            mediaPlayer.pause();
        }




}


// 注意在onDestory中销毁、回收资源
@Override
protected void onDestroy() {
    super.onDestroy();

    if (mediaPlayer.isPlaying()) {
        mediaPlayer.stop();
    }

    // 释放资源
    mediaPlayer.release();
    recorder.release();
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值