简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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