Android学习:java.lang.RuntimeException: setAudioSource failed异常处理




 

在实现钢琴软件的录音过程中,出现java.lang.RuntimeException: setAudioSource failed报错。

好多资料说需要在AndroidManifest.xml文件中增加如下配置以授予程序录制声音的权限:

     android:name="android.permission.RECORD_AUDIO">
< /uses-permission>

添加之后,依旧出现如上报错,考虑到可能跟SDcard有关系,于是又增加了以下语句:

     android:name="android.permission.WRITE_EXTERNAL_STORAGE">

Android学习:java.lang.RuntimeException: setAudioSource failed异常处理_第1张图片

问题解决,可以实现正常录音。

录音部分主要JAVA代码:

[java] view plain copy print ?
  1. recordStop = (ImageButton) findViewById(R.id.button1);  
  2.         recordStopText=(TextView) findViewById(R.id.textView2);  
  3.         recordStop.setOnClickListener(this);  
  4.           
  5.     }  
  6.     
  7.     @Override  
  8.     public void onClick(View v)  
  9.     {     
  10.         count++;  
  11.         int i=count%2;  
  12.         switch (i)  
  13.         {  
  14.             // 开始录音  
  15.             case 1:  
  16.                 recordStopText.setText("      Stop");  
  17.                 if (!Environment.getExternalStorageState().equals(  
  18.                     android.os.Environment.MEDIA_MOUNTED))  
  19.                 {  
  20.                     Toast.makeText(NewPianoActivity.this  
  21.                         , "SD卡不存在,请插入SD卡!"  
  22.                         , 5000)  
  23.                         .show();  
  24.                     return;  
  25.                 }  
  26.                 try  
  27.                 {  
  28.                     // 创建保存录音的音频文件  
  29.                     soundFile = new File(Environment  
  30.                         .getExternalStorageDirectory()  
  31.                         .getCanonicalFile() + "/Recondsound.amr");  
  32.                     mRecorder = new MediaRecorder();  
  33.                     // 设置录音的声音来源  
  34.                     mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);  
  35.                     // 设置录制的声音的输出格式(必须在设置声音编码格式之前设置)  
  36.                     mRecorder.setOutputFormat(MediaRecorder  
  37.                         .OutputFormat.THREE_GPP);  
  38.                     // 设置声音编码的格式  
  39.                     mRecorder.setAudioEncoder(MediaRecorder  
  40.                         .AudioEncoder.AMR_NB);  
  41.                     mRecorder.setOutputFile(soundFile.getAbsolutePath());  
  42.                     mRecorder.prepare();  
  43.                     // 开始录音  
  44.                     mRecorder.start();  
  45.                       
  46.                 }  
  47.                 catch (Exception e)  
  48.                 {  
  49.                     e.printStackTrace();  
  50.                 }  
  51.                 break;  
  52.             // 按两次停止录音          case 0:  
  53.                 recordStopText.setText("    Record");  
  54.                 try  
  55.                 {  
  56.                     // 停止录音  
  57.                     mRecorder.stop();  
  58.                     // 释放资源  
  59.                     mRecorder.release();  
  60.                     mRecorder = null;  
  61.                 }  
  62.                 catch(Exception e)  
  63.                 {  
  64.                     e.printStackTrace();  
  65.                 }  
  66.                   
  67.                 break;  
  68.                   
  69.         }  
recordStop = (ImageButton) findViewById(R.id.button1);
		recordStopText=(TextView) findViewById(R.id.textView2);
		recordStop.setOnClickListener(this);
		
	}
  
	@Override
	public void onClick(View v)
	{   
	    count++;
	    int i=count%2;
		switch (i)
		{
			// 开始录音
			case 1:
				recordStopText.setText("      Stop");
				if (!Environment.getExternalStorageState().equals(
					android.os.Environment.MEDIA_MOUNTED))
				{
					Toast.makeText(NewPianoActivity.this
						, "SD卡不存在,请插入SD卡!"
						, 5000)
						.show();
					return;
				}
				try
				{
					// 创建保存录音的音频文件
					soundFile = new File(Environment
						.getExternalStorageDirectory()
						.getCanonicalFile() + "/Recondsound.amr");
					mRecorder = new MediaRecorder();
					// 设置录音的声音来源
					mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
					// 设置录制的声音的输出格式(必须在设置声音编码格式之前设置)
					mRecorder.setOutputFormat(MediaRecorder
						.OutputFormat.THREE_GPP);
					// 设置声音编码的格式
					mRecorder.setAudioEncoder(MediaRecorder
						.AudioEncoder.AMR_NB);
					mRecorder.setOutputFile(soundFile.getAbsolutePath());
					mRecorder.prepare();
					// 开始录音
					mRecorder.start();
					
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
				break;
			// 按两次停止录音			case 0:
				recordStopText.setText("    Record");
				try
				{
					// 停止录音
					mRecorder.stop();
					// 释放资源
					mRecorder.release();
					mRecorder = null;
				}
				catch(Exception e)
				{
					e.printStackTrace();
				}
				
				break;
				
		}


初始按钮状态:   点击一次后:   再次点击(录音停止):

你可能感兴趣的:(Android学习:java.lang.RuntimeException: setAudioSource failed异常处理)