停止录音机后应用程序崩溃
Posted
技术标签:
【中文标题】停止录音机后应用程序崩溃【英文标题】:Application is getting crashed after stop the voice recorder 【发布时间】:2019-09-13 09:24:53 【问题描述】:当我停止录制时,应用程序崩溃了。我收到这样的错误
E/androidRuntime: FATAL EXCEPTION: main invoice recorder
。 我检查了这些链接,但没有找到任何解决方案。
This Activity already has an action bar supplied by the window decor
How to Record Voice in android?
我的活动:
public class MainActivity extends AppCompatActivity
MediaRecorder recorder;
Button btnstart;
Button btnstop;
ImageView micrecord,imgrecord;
TextView txttime;
boolean isrecording=false;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
micrecord=(ImageView)findViewById(R.id.micrecord);
imgrecord=(ImageView) findViewById(R.id.imgrecord);
txttime=(TextView)findViewById(R.id.txttime);
imgrecord.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
if(!isrecording)
isrecording=true;
imgrecord.setImageResource(R.drawable.stop);
micrecord.setImageResource(R.drawable.mic);
MediaRecorder recorder;
recorder=new MediaRecorder();
recorder.setAudiosource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
recorder.setOutputFile(G.address+"/"+System.currentTimeMillis()+".amr");
try
recorder.prepare();
recorder.start();
catch (IOException e)
e.printStackTrace();
else
isrecording=false;
imgrecord.setImageResource(R.drawable.stop);
micrecord.setImageResource(R.drawable.micc);
recorder.release();
);
清单
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.video69">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.STORAGE"/>
G类:
public class G extends Application
public static Context context;
public static String address;
@Override
public void onCreate()
super.onCreate();
context=getApplicationContext();
address= Environment.getExternalStorageDirectory().getAbsolutePath()+"/clicksite";
new File(address).mkdirs();
风格
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
【问题讨论】:
【参考方案1】:只需删除 if 循环中的注释行。
MediaRecorder 记录器;
你已经在全局和本地创建了 MediaRecorder 引用。所以在你调用的 else 循环中
recorder.release();
在空引用上,所以您会收到此错误。
if(!isrecording)
isrecording=true;
imgrecord.setImageResource(R.drawable.stop);
micrecord.setImageResource(R.drawable.mic);
// MediaRecorder recorder;
recorder=new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
recorder.setOutputFile(G.address+"/"+System.currentTimeMillis()+".amr");
try
recorder.prepare();
recorder.start();
catch (IOException e)
e.printStackTrace();
else
isrecording=false;
imgrecord.setImageResource(R.drawable.stop);
micrecord.setImageResource(R.drawable.micc);
recorder.release();
【讨论】:
以上是关于停止录音机后应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章