如何皮卡音频文件的路径或URI从设备的Andr​​oid?音频文件、路径、皮卡、设备

2023-09-06 02:29:33 作者:Rational离伤

我创建一个简单的应用程序,我想打一个声音。在$ P $在App pssing一键播放,将播放声音。 对于播放声音我使用的:

I am creating an simple app where I want to play a sound. On pressing a button play on app it will play the sound. For playing sound I am using:

MediaPlayer mediaPlayer;
        mediaPlayer = MediaPlayer.create(this, R.raw.test_cbr);
        mediaPlayer.setOnCompletionListener(new OnCompletionListener()
        {
            @Override
            public void onCompletion(MediaPlayer mp)
            {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), "Completed",
                   Toast.LENGTH_LONG).show();

            }
        });
mediaPlayer.start();

和它工作正常。

但现在我想用户应该能够选择从设备中的音频文件,这样当他(她)presses的播放按钮,选定的声音应发挥。

But now I want that the user should be able to select a audio file from device, so that when he or she presses the play button, the selected sound should be played.

我没有收到类似图像的任何意图动作可以采摘。

I am not getting any intent action like image can be picked.

Intent photo_picker_intent = new Intent(Intent.ACTION_PICK);
photo_picker_intent.setType("image/*");
startActivityForResult(photo_picker_intent, PHOTOS_FROM_GALLERY);

在上面的code,用户只需去画廊。当点击在任何图像,他或她将得到的图像的URI可用于表示应用程序的图像。我想这样做,除了音频文件的选择。

In the above code, the user will simply go to gallery. Upon clicking on any image, he or she will get an image URI that can be used for showing image in app. I would like to do the same, except for an audio file selection.

推荐答案

只要使用,这两条线,

Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(Intent.createChooser(intent, "Gallery"), reqCode);

或者

 Intent intent = new Intent();
        intent.setType("audio/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent,"Select Audio "), reqCode);