[英]Why my Audio_Recorder works in Samsung but Nexus phone not?
我正在嘗試錄制音頻並將其保存在新文件夾中。 在Samsung設備中,它可以運行,但是在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>
這不是設備問題。 這是API版本問題。 您必須找出哪個API版本崩潰了,然后您就可以通過很少的研究來縮小問題范圍。
假設您以api 24為目標並且假設您正在支持API 16或更高版本,則必須在運行時請求READ_EXTERNAL_STORAGE,WRITE_EXTERNAL_STORAGE的許可(我認為這最后一個涵蓋了讀寫,但是為了安全起見,我將兩者都列出了)側)和RECORD_AUDIO,因為它們被視為危險權限。
您必須在開始錄制之前執行此操作,並且如果用戶未在應用程序詢問權限時證明用戶的權限,或者在提供權限一段時間后將其撤消,則該應用程序必須進行處理。
實現起來非常復雜,它涉及:
如果您發現Android Dox不能幫助簽出本教程,則Androids 6.0運行時權限將在CommonsWare代碼實驗室中提供
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.