Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

audio

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.

like image 380
Arun Badole Avatar asked May 05 '12 10:05

Arun Badole


1 Answers

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);
like image 119
user370305 Avatar answered Oct 29 '22 03:10

user370305