简体   繁体   English

为什么我的Audio_Recorder可以在Samsung上运行而不能在Nexus手机上运行?

[英]Why my Audio_Recorder works in Samsung but Nexus phone not?

I'm trying to record audio and save it in a new folder. 我正在尝试录制音频并将其保存在新文件夹中。 In Samsung devices, it is working, but in a Nexus phone it gives some errors. 在Samsung设备中,它可以运行,但是在Nexus手机中,它会出现一些错误。 I get two errors: One is in the first line of my public class & the second one is in the startRecording() line. 我遇到两个错误:一个在我的公共类的第一行中,第二个在startRecording()行中。 Here is my code: 这是我的代码:

MainActivity.java: 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";
    ...
    }
...
}

Here is the errors from the logcat: 这是来自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 表现

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

It is not device issue. 这不是设备问题。 It is an API Version issue. 这是API版本问题。 You must find out which API Version is crashing, then you should be able to narrow out your problem with little research. 您必须找出哪个API版本崩溃了,然后您就可以通过很少的研究来缩小问题范围。

Given you are targetting api 24 and lets say you are supporting API 16 and greater, you will have to request permission at runtime for READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE (I think this last one covers both read and write but I am listing both to be on the safe side), and RECORD_AUDIO as these are considered dangerous permissions. 假设您以api 24为目标并且假设您正在支持API 16或更高版本,则必须在运行时请求READ_EXTERNAL_STORAGE,WRITE_EXTERNAL_STORAGE的许可(我认为这最后一个涵盖了读写,但是为了安全起见,我将两者都列出了)侧)和RECORD_AUDIO,因为它们被视为危险权限。

You have todo this before starting to record and the app has to handle if the user does't provede the permissions when the app asks for them, or revokes them after having provided them some time before. 您必须在开始录制之前执行此操作,并且如果用户未在应用程序询问权限时证明用户的权限,或者在提供权限一段时间后将其撤消,则该应用程序必须进行处理。

It is rather complex to implement it involves: 实现起来非常复杂,它涉及:

  1. Checking if you have permission grantes. 检查您是否具有权限授予。
  2. If not, check if the user has stated not to be asked again 如果不是,请检查用户是否已声明不再询问
  3. Check if you should provide some explanation about why the app needs those permissions. 检查您是否应该提供有关应用程序为何需要这些权限的一些说明。

If you find Android Dox are not helping checkout this tutorial Android 6.0 Runtime Permissions a CommonsWare Code Lab 如果您发现Android Dox不能帮助签出本教程,则Androids 6.0运行时权限将在CommonsWare代码实验室中提供

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM