4

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.

Community
  • 1
  • 1
Eray
  • 7,038
  • 16
  • 70
  • 120

2 Answers2

5

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.

mdelolmo
  • 6,417
  • 3
  • 40
  • 58
2

You can still use the deprecated intent to target API levels lower than 15, and then use the new intent to target newer API levels. To do this simply use a check in your code as follows:

if ( Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1 )  {
   // use the [MediaStore.INTENT_ACTION_MUSIC_PLAYER] intent
}
else  {
   // use the [Intent.CATEGORY_APP_MUSIC] intent
}

Just make sure that your TargetAPI is 15 or higher; your MinAPI can be 8.

free3dom
  • 18,729
  • 7
  • 52
  • 51
  • I've tried it yesterday, there are problems. On Galaxy Note 2 (Android 4.1.2) , i'm getting `No Activity found to handle Intent { act=android.intent.category.APP_MUSIC dat=file:///audiorecordtest20130612145343.3gp typ=audio/* }` . And on Galaxy S2 (Android 4.1.2) my music player launching and then suddenly shut down. – Eray Jun 13 '13 at 07:17
  • This seems to be a problem with some Samsung devices not registering their music player properly. See [here](http://stackoverflow.com/questions/12532207/open-music-player-on-galaxy-s3) for more info. – free3dom Jun 13 '13 at 07:41