Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android select mp3 file

Sorry if this has already been answered, but I can't specify my question very well. I have an activity that needs a song file from your device, and I want it when I press a button to open a dialog to ask you how you want to open a file (like to choose a file explorer) and then the user will select an mp3 file (it should be possible to enter both internal memory and external, at least those available to users, like in my Xperia V which has 2 internal partitions, and an SD Card), and when a music file is selected (.wav, .mp3, .acc files) to load its name and file path in my app. How can I do it? I am not providing any code, because that's all I need

like image 564
Sartheris Stormhammer Avatar asked Jan 29 '14 10:01

Sartheris Stormhammer


People also ask

How do I choose an audio file on my Android?

So, to pick an audio file I'm using following code: Intent intent = new Intent(); intent. setType("audio/*"); intent. setAction(Intent.

How do I select only MP3 files?

type" without quotes and then click enter. In this case, just search ". mp3".

How do I select an audio file?

The easiest way to select a region of audio is to click the left mouse button anywhere inside of an audio track, then drag (in either direction) until the other edge of your selection is made, then release the mouse.


1 Answers

I use the following to select an mp3 file :

Intent intent;
intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("audio/mpeg");
startActivityForResult(Intent.createChooser(intent, getString(R.string.select_audio_file_title)), REQ_CODE_PICK_SOUNDFILE);

and then you can get the Uri back in onActivityResult :

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQ_CODE_PICK_SOUNDFILE && resultCode == Activity.RESULT_OK){
        if ((data != null) && (data.getData() != null)){
            Uri audioFileUri = data.getData();
            // Now you can use that Uri to get the file path, or upload it, ...
            }
        }
}

I believe selecting other types of audio files would be a matter of setting a wild card in the MIME type by doing intent.setType("audio/*) although I haven't tested that.

Please let me know if that works for you ;-)

like image 68
2Dee Avatar answered Oct 11 '22 03:10

2Dee