Domanda

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.

È stato utile?

Soluzione

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.

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top