【问题标题】:How to play recorded audio file from 'onActivityResult()'?如何从“onActivityResult()”播放录制的音频文件?
【发布时间】:2017-10-17 17:22:57
【问题描述】:

我正在使用this 库在我的应用中录制音频。

这是我的代码:

recordDefectAudio.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if (ContextCompat.checkSelfPermission(getBaseContext(),
                android.Manifest.permission.RECORD_AUDIO) + ContextCompat.checkSelfPermission(getBaseContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {

            ActivityCompat.requestPermissions(MainActivity.this,
                    new String[]{android.Manifest.permission.RECORD_AUDIO, Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    MY_PERMISSIONS_REQUEST_LOCATION);

        }
        if (ContextCompat.checkSelfPermission(getBaseContext(), Manifest.permission.RECORD_AUDIO) +
                ContextCompat.checkSelfPermission(getBaseContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE)
                == PackageManager.PERMISSION_GRANTED) {

            String filePath = Environment.getExternalStorageDirectory() + "/recorded_audio.wav";
            int color = getResources().getColor(R.color.colorPrimaryDark);
            AndroidAudioRecorder.with(MainActivity.this)
                    // Required
                    .setFilePath(filePath)
                    .setColor(color)
                    .setRequestCode(RECORD_PRODUCT_DAMAGE)

                    // Optional
                    .setSource(AudioSource.MIC)
                    .setChannel(AudioChannel.STEREO)
                    .setSampleRate(AudioSampleRate.HZ_48000)
                    .setAutoStart(true)
                    .setKeepDisplayOn(true)

                    // Start recording
                    .record();
        }
    }
});

这是onActivityResult()中的代码:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == RECORD_PRODUCT_DAMAGE) {
            if (resultCode == RESULT_OK) {
                // Great! User has recorded and saved the audio file
                Toast.makeText(this, "Audio recorded successfully!", Toast.LENGTH_SHORT).show();
                playRecordedAudio.setVisibility(View.VISIBLE);
                recordAgain.setVisibility(View.VISIBLE);
                recordDefectAudio.setVisibility(View.INVISIBLE);
            } else if (resultCode == RESULT_CANCELED) {
                // Oops! User has canceled the recording
                Toast.makeText(this, "Audio was not recorded", Toast.LENGTH_SHORT).show();
            }

        }
    }

我只想知道如何从onActivityResult() 播放音频文件,或者有其他播放音频的方法吗?

请告诉我。

【问题讨论】:

  • 只使用MediaPlayer类播放onActivity结果中的音频文件
  • 一些代码真的很有帮助,@Redman

标签: android android-mediaplayer onactivityresult android-audiorecord


【解决方案1】:

使用MediaPlayer播放音频文件,在onActivityResult()中写入如下代码,if (resultCode == RESULT_OK)从intent data中获取音频文件的路径

MediaPlayer mp=new MediaPlayer();  
    try{  
        mp.setDataSource("/sdcard/Music/maine.mp3");//path to your audio file here  
        mp.prepare();  
        mp.start();  

    }catch(Exception e){e.printStackTrace();}  

}  

更多信息请见this

【讨论】:

    【解决方案2】:
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == RECORD_PRODUCT_DAMAGE) {
            if (resultCode == RESULT_OK) {
                // Great! User has recorded and saved the audio file
                Toast.makeText(this, "Audio recorded successfully!", Toast.LENGTH_SHORT).show();
                playRecordedAudio.setVisibility(View.VISIBLE);
                recordAgain.setVisibility(View.VISIBLE);
                recordDefectAudio.setVisibility(View.INVISIBLE);
    
        String realPath = null;
        Uri uriPath = null;
        realPath = getPathForAudio(YourActivity.this, data.getData());
        uriPath = Uri.fromFile(new File(realPath)); 
        try{
        MediaPlayer  mp = MediaPlayer.create(context, uriPath);
        mp.start();
        }catch(NullPointerException e) {
        // handle NullPointerException
        }
            } else if (resultCode == RESULT_CANCELED) {
                // Oops! User has canceled the recording
                Toast.makeText(this, "Audio was not recorded", Toast.LENGTH_SHORT).show();
            }
    
        }
    }
    

    然后获取音频文件路径

     public static String getPathForAudio(Context context, Uri uri)
     {
      String result = null;
      Cursor cursor = null;
    
      try {
        String[] proj = { MediaStore.Audio.Media.DATA };
        cursor = context.getContentResolver().query(uri, proj, null, null, null);
        if (cursor == null) {
            result = uri.getPath();
        } else {
            cursor.moveToFirst();
            int column_index = cursor.getColumnIndex(MediaStore.Audio.AudioColumns.DATA);
            result = cursor.getString(column_index);
            cursor.close();
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return result;
    }
    

    【讨论】:

    • 无法解析方法getPathForAudio()R.raw.closer是什么?
    • 我在下面添加了该方法
    • 获取java.lang.NullPointerException: Attempt to invoke virtual method 'android.net.Uri android.content.Intent.getData()' on a null object reference上线realPath = getPathForAudio(MainActivity.this, data.getData());
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多