Launching Media Player using Intent

Eray picture Eray · Jun 12, 2013 · Viewed 10k times · Source

I'm developing my first Android app . It's voice recording app. I'm recording voice with MediaRecord like this :

mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mRecorder.setOutputFile(mFileName);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

        try {
            mRecorder.prepare();
        } catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed");
        }

        mRecorder.start();

And i have another activity for playback these recorded voices (.3gpp files) . In this activity, there is a ListView containing my recorded voices. I want to play sound with any music player installed on phone. Here my code : (Source for this code : https://stackoverflow.com/a/3367231/556169)

Intent intent = new Intent();  
intent.setAction(android.content.Intent.ACTION_VIEW);  
File file = new File((String) ((TextView) item).getText());  
intent.setDataAndType(Uri.fromFile(file), "audio/*");  
startActivity(intent);

But i'm getting "Music player can't play this media type" error.

When i browse this audio files and playback them through my file explorer, it's working perfectly (It's mean, i'm recording voices, successfully). But when i use Intent inside my application, i'm getting error.

ADDITIONAL I can't use MediaStore.INTENT_ACTION_MUSIC_PLAYER because it's deprecated. I can't use Intent.CATEGORY_APP_MUSICbecause it's requires min API lvl 15 . My project's min API level should be 8.

Answer

mdelolmo picture mdelolmo · Jun 12, 2013

I think the best option is to use a chooser activity, where the user can choose his favorite media player.

Intent viewIntent = new Intent(Intent.ACTION_VIEW);
File file = new File((String) ((TextView) item).getText());
viewIntent.setDataAndType(Uri.fromFile(file), "audio/*");
startActivity(Intent.createChooser(viewIntent, null));

By the way, that way of passing the file name seems a bit weird to me. I would consider changing it.

I am not sure, but you should be able to see your own player in the chooser if you declare the corresponding intent-filters.