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

本文解决了一款Android钢琴软件在录音过程中遇到的java.lang.RuntimeException:setAudioSourcefailed问题,通过在AndroidManifest.xml中添加权限配置,并确保SD卡存在,成功实现了正常的录音功能。
 

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

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

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

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

<uses-permission

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

</uses-permission>

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

录音部分主要JAVA代码:

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;
				
		}


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

### Java解压缩失败解决方案 当遇到`java.lang.RuntimeException: Unzip failed`错误时,通常意味着在尝试读取或处理ZIP文件的过程中出现了问题。此异常可能由多种原因引起,包括但不限于文件损坏、权限不足以及路径配置不正确。 #### 文件验证与修复 确保用于解压操作的目标ZIP文件未被破坏。可以通过其他工具(如WinRAR或7-Zip)打开该文件来初步判断其完整性。如果发现文件确实存在问题,则应重新获取一份完整的副本[^1]。 #### 权限设置检查 确认应用程序具有足够的访问权限去执行涉及磁盘I/O的操作。特别是在Linux系统上运行程序时,需注意当前用户的目录写入许可情况;而在Windows环境下则要留意UAC(User Account Control)的影响范围[^4]。 #### 路径字符串规范化 对于通过网络加载资源的情况,务必保证URL地址拼接无误,并且遵循标准格式。例如,在构建`jar:`协议开头的URI实例之前,应该先调用`toExternalForm()`方法转换原始链接对象。 ```java // 正确做法示范 String jarUrlStr = "file:/path/to/my.jar"; URL url = new URL(jarUrlStr); JarURLConnection connection = (JarURLConnection)new URL("jar", "", url + "!/"); connection.setUseCaches(false); // 关闭缓存机制以减少潜在冲突 InputStream inputStream = connection.getInputStream(); ``` #### 使用第三方库简化流程 考虑到内置API可能存在局限性,引入成熟的开源项目往往能带来更稳定的表现。像Apache Commons Compress这样的组件就提供了丰富的功能接口供开发者选用,从而降低开发难度并提高代码可维护性[^2]。 ```xml <!-- Maven POM片段 --> <dependencies> <!-- ... --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-compress</artifactId> <version>1.21</version> </dependency> <!-- ... --> </dependencies> ``` ```java import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipFile; public class ZipUtil { public static void extractZip(String zipFilePath, String outputDirPath){ try(ZipFile zf=new ZipFile(zipFilePath)){ Enumeration<? extends ZipArchiveEntry> entries=zf.getEntries(); while(entries.hasMoreElements()){ final var entry=(ZipArchiveEntry)entries.nextElement(); File outputFile=new File(outputDirPath,entry.getName()); if(entry.isDirectory()){ Files.createDirectories(outputFile.toPath()); }else{ Files.copy(zf.getInputStream(entry),outputFile.toPath(),StandardCopyOption.REPLACE_EXISTING); } } }catch(IOException e){ throw new RuntimeException(e.getMessage(),e); } } } ```
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值