java Android studio E/MediaRecorder﹕ 在无效状态下调用stop:4
我正在尝试使用MediaRecorder和MediaPlayer为安卓创建一个简单的录制和播放音频应用程序,当我尝试停止录制时,我在logcat中遇到以下错误E/MediaRecorder﹕ 在无效状态下调用stop:4我正在moto g 2013 16 gb内存版本5.1上测试
主要活动
package com.example.sebastin.myapplication;
import 安卓.content.DialogInterface;
import 安卓.graphics.Color;
import 安卓.hardware.Sensor;
import 安卓.hardware.SensorEvent;
import 安卓.hardware.SensorEventListener;
import 安卓.hardware.SensorManager;
import 安卓.location.GpsStatus;
import 安卓.media.MediaPlayer;
import 安卓.media.MediaRecorder;
import 安卓.os.Environment;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.os.Bundle;
import 安卓.view.Menu;
import 安卓.view.MenuItem;
import 安卓.view.View;
import 安卓.widget.Button;
import 安卓.widget.EditText;
import 安卓.widget.LinearLayout;
import 安卓.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:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:paddingLeft="@dimen/activity_horizontal_margin"
安卓:paddingRight="@dimen/activity_horizontal_margin"
安卓:paddingTop="@dimen/activity_vertical_margin"
安卓:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainBina"
安卓:id="@+id/linear"
安卓:orientation="vertical">
<TextView 安卓:text="@string/saludo"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:gravity="center_vertical"
安卓:id="@+id/textView"
安卓:layout_gravity="center_horizontal" />
<Button
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="@string/Grabar"
安卓:id="@+id/button"
安卓:layout_marginTop="35dp" />
<Button
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="@string/Reproducir"
安卓:id="@+id/button2" />
<Button
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="@string/Parar"
安卓:id="@+id/button3" />
</LinearLayout>
显示
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
package="com.example.sebastin.myapplication" >
<uses-permission 安卓:name="安卓.permission.RECORD_AUDIO" />
<uses-permission 安卓:name="安卓.permission.write_external_storage" />
<application
安卓:allowBackup="true"
安卓:icon="@mipmap/ic_launcher"
安卓:label="@string/app_name"
安卓:theme="@style/AppTheme" >
<activity
安卓:name=".MainBina"
安卓:label="@string/app_name" >
<intent-filter>
<action 安卓:name="安卓.intent.action.MAIN" />
<category 安卓:name="安卓.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
# 1 楼答案
你确定它启动正确吗?查看日志以了解其他错误/异常
您写入外部存储器的权限似乎有误,应该是(最后一部分大写):