search

Home  >  Q&A  >  body text

java - android 调用系统录音机并能返回含所录音频的

PHPzPHPz2777 days ago711

reply all(1)I'll reply

  • 怪我咯

    怪我咯2017-04-17 14:38:39

    Audio is also obtained in onActivityResult
    Call system audio:

            Intent intent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
            startActivityForResult(intent, RECORD_AUDIO);
    

    Get Uri:

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            switch (requestCode) {
                case RECORD_AUDIO:
                    try {
                        Uri uri = data.getData();
                        String filePath = getAudioFilePathFromUri(uri);
                        Log.d(TAG, filePath);
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                    break;
            }
        }
    
        private String getAudioFilePathFromUri(Uri uri) {
            Cursor cursor = getContentResolver()
                    .query(uri, null, null, null, null);
            cursor.moveToFirst();
            int index = cursor.getColumnIndex(MediaStore.Audio.AudioColumns.DATA);
            return cursor.getString(index);
        }
    

    filePath is the audio file you recorded, you can copy it to any location.
    Don’t forget to add uses-permission related permissions

    reply
    0
  • Cancelreply