我正在尝试录制音频并将其保存在新文件夹中。在三星设备中,它可以工作,但在Nexus手机中它会出现一些错误。我得到两个错误:一个是我公共课的第一行&第二个是startRecording()
行。这是我的代码:
MainActivity.java:
public class MainActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
mRecordBtn.setOnTouchListener(new View.OnTouchListener()
{
@Override
public boolean onTouch(View view, MotionEvent motionEvent)
{
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
startRecording(); //<<<<<<<<<<<<<<<<HERE AN ERROR
mRecordLabel.setText("GRAVANDO");
adp.notifyDataSetChanged();
} else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
stopRecording();
mRecordLabel.setText("GRAVADO");
lv.setAdapter(adp);
adp.notifyDataSetChanged();
}
return false;
}
});
...
}
private void startRecording()
{
Long tsLong = System.currentTimeMillis()/1000;
String ts = tsLong.toString();
mRecorder = new MediaRecorder();
mRecorder.reset();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
mRecorder.setOutputFile(mFileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
String sep = File.separator; // Use this instead of hardcoding the "/"
String newFolder = "FolderName";
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File myNewFolder = new File(extStorageDirectory + sep + newFolder);
//myNewFolder.mkdir();
myNewFolder.mkdirs();
mFileName = Environment.getExternalStorageDirectory().toString()
+ sep + newFolder + sep + "Record."+ts+".mp3";
...
}
...
}
以下是logcat的错误:
java.lang.RuntimeException: setAudioSource failed.
at org.usr.usr.musicplayer.MainActivity.startRecording(MainActivity.java:222) = mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
at org.usr.usr.musicplayer.MainActivity.access$000(MainActivity.java:72) = public class MainActivity extends AppCompatActivity {
at org.usr.usr.musicplayer.MainActivity$3.onTouch(MainActivity.java:196) = startRecording();
清单
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.usr.usr.musicplayer">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<application
android:allowBackup="true"
android:icon="@drawable/llama10"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/PlayerTheme">
<activity android:name=".MainActivity"
android:launchMode="singleTop"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Player"
android:launchMode="singleTop"
android:screenOrientation="portrait">
</activity>
</application>
答案 0 :(得分:0)
这不是设备问题。这是API版本问题。你必须找出崩溃的API版本,然后你应该能够通过很少的研究来缩小你的问题。
答案 1 :(得分:0)
鉴于你正在瞄准api 24并且假设你支持API 16及更高版本,你必须在运行时请求READ_EXTERNAL_STORAGE,WRITE_EXTERNAL_STORAGE的权限(我认为这最后一个包括读和写,但我列出两者都是在安全方面)和RECORD_AUDIO因为这些被认为是危险的权限。
如果用户在应用程序要求时证明了权限,或者在提供了一段时间之后撤消了这些权限,那么在开始记录之前你已经知道这个并且应用必须处理。
实施它涉及相当复杂:
如果您发现Android Dox无法查看本教程 Android 6.0 Runtime Permissions a CommonsWare Code Lab