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

时间:2015-12-03 17:00:22

标签: java android

我正在尝试使用MediaRecorder和MediaPlayer创建一个简单的记录和播放音频应用程序当我尝试停止录制我在logcat中得到以下错误E / MediaRecorder:停止调用无效状态:4我正在测试在moto g 2013 16 gb内存版本5.1

主要活动

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>

清单

<?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>

1 个答案:

答案 0 :(得分:1)

你确定它开始正确吗?看看你的logcat是否存在其他错误/异常。

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

android.permission.WRITE_EXTERNAL_STORAGE