简体   繁体   English

如何在Android中从设备中拾取音频文件的路径或uri?

[英]How to pickup an audio file's path or uri from device in Android?

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();

And it works fine. 它工作正常。

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

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. 单击任何图像后,他或她将获得可用于在应用程序中显示图像的图像URI。 I would like to do the same, except for an audio file selection. 我想做同样的事情,除了音频文件选择。

Just use, these two lines, 只需使用这两行,

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

Or, 要么,

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

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

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