java - Android studio E/MediaRecorder:无效状态调用停止 : 4

标签 java android

我正在尝试使用 MediaRecorder 和 MediaPlayer 为 android 创建一个简单的录音和播放音频应用程序,当我尝试停止录音时,我在 logcat 中收到以下错误 E/MediaRecorder: stop called in an invalid state: 4 i am testing在 moto g 2013 16 gb 内存版本 5.1 上

主要 Activity

package com.example.sebastin.myapplication;

import android.content.DialogInterface;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.GpsStatus;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FilterWriter;
import java.io.IOException;


public class MainBina extends AppCompatActivity {
    TextView texto;
    Button boton ,boton2, boton3, boton4;
    MediaPlayer Play;
    MediaRecorder Record;
    String grabacion;
    String filePath;
    String grabarState;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_bina);
        filePath  = Environment.getExternalStorageDirectory()+ "/audiorecordtest.3gp";
        texto = (TextView) findViewById(R.id.textView);
        //boton.setText("Grabar");
        boton = (Button) findViewById(R.id.button);
        boton2 = (Button) findViewById(R.id.button2);
        boton3 = (Button) findViewById(R.id.button3);
        texto.setText(boton.getText().toString());
        boton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                switch ( boton.getText().toString())
                {
                    case "grabar":
                        try {
                            startRecord();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        boton.setText("grabando");
                        break;
                    case "grabando":
                        try {
                            stopRecord();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        boton.setText("Grabar");
                        break;
                }


            }
        });
        boton3.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                stopPlay();
            }
        });
    boton2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            try {
                startPlay();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });}
    public void startRecord ()throws Exception{
        if (Record!=null)
        {
            Record.release();
        }
        File fileOut = new File(filePath);
        if (fileOut!=null)
        {
            fileOut.delete();
        }
        String fileName = "bina1.3gpp";
        FileOutputStream fileOutputStream = openFileOutput(fileName, MODE_PRIVATE);
        String filePathh = fileOutputStream.toString();

        texto.setText(filePath);
        Record = new MediaRecorder();
        Record.setAudioSource(MediaRecorder.AudioSource.MIC);
        Record.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        Record.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        Record.setOutputFile(filePath);
        fileOutputStream.close();
        Record.prepare();

        Record.start();

    }
    public void stopRecord () {
        Record.stop();
        Record.reset();
        Record.release();
        Record = null;
    }
    public void startPlay  ()throws Exception {
        Play = new MediaPlayer();
        Play.setDataSource(filePath);
        Play.prepare();
        Play.start();
        Play.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer Play) {
                Play.release();
            }
        });
    }
    public void stopPlay ()
    {
        if (Play != null) {
            Play.stop();
            Play.release();
            Play = 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.menu_main_bina, 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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}

XML

<LinearLayout 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=".MainBina"
    android:id="@+id/linear"
    android:orientation="vertical">


    <TextView android:text="@string/saludo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:id="@+id/textView"
        android:layout_gravity="center_horizontal" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Grabar"
        android:id="@+id/button"
        android:layout_marginTop="35dp" />


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Reproducir"
        android:id="@+id/button2" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Parar"
        android:id="@+id/button3" />


</LinearLayout>

list

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.sebastin.myapplication" >
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.write_external_storage" />
    <application

        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainBina"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

最佳答案

你确定它正确启动了吗?查看您的 logcat 是否有其他错误/异常。

您的写入外部存储的权限看起来不对,应该是(最后部分大写):

android.permission.WRITE_EXTERNAL_STORAGE

关于java - Android studio E/MediaRecorder:无效状态调用停止 : 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34071899/

相关文章:

java - XML 架构。处理命名空间感知属性值

android - 如何禁用 Logcat 中的自动滚动功能?

java - 使用谓词过滤

java - 如何使用属性中的 Spring @Value 来设置注释属性

java - 在回收器 View 中设置的单选复选框可添加和休息点

java - 链接列表的编码约定

java - 如何复用导航条码?

android - 使用 Integer.parseInt 将 EditText 转换为 int

java - 获取颜色值十六进制代码android

Android Java : Fatal exception main nullpointer. 我没有明确定义的线程。它说它无法实例化 Activity