Play song on default music player - android

My app shows a list of songs from an SD card. I want to play the song on the default player. I have all the data about the song: id, title, album, artist, path ...

Is there a way to start the default player to play a song?

What I tried:

  • Using Intent.CATEGORY_APP_MUSIC . I can start the default player, but I can’t install the song. Intent.makeMainSelectorActivity(Intent.ACTION_MAIN, Intent.CATEGORY_APP_MUSIC) open the default music application. However, intent.setData(Uri.withAppendedPath(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id)) throws an exception that was not found.

  • Using deprecated MediaStore.INTENT_ACTION_MUSIC_PLAYER . No activity found. Intent(MediaStore.INTENT_ACTION_MUSIC_PLAYER).setDataAndType(Uri.fromFile(songFile), "audio/*")

  • Using INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH . Running a search, but not a song, says that he cannot prepare the mix for Play music. However, now it works from Google, so this may be the key. Intent(MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH).putExtra(SearchManager.QUERY, name)
  • Using ACTION_VIEW . It works, but it launches the compact version of the player, and I want a full-fledged player.

compat view

Note. I want to run External Player , but not in my application.

Update . It looks like hardcoded Play Music and Youtube Google Now are now supported.

+6
source share
4 answers

If you want to run the default music player application on the device, try this:

 Intent intent = new Intent(); intent.setAction(android.content.Intent.ACTION_VIEW); File file = new File(YOUR_SONG_URI); intent.setDataAndType(Uri.fromFile(file), "audio/*"); startActivity(intent); 
+4
source

Try something simple:

 MediaPlayer mp = new MediaPlayer(); mp.setLooping(true); try { mp.setDataSource(mFile); //mFile is the path to your mp3 file } catch (Exception e) { e.printStackTrace(); } try { mp.prepare(); } catch (Exception e) { e.printStackTrace(); } mp.start(); 

And another example with MadiaPlayer and the file saved in the res / raw / directory:

 MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.sound_file_1); mediaPlayer.start(); // no need to call prepare(); create() does that for you 

Learn more about: http://developer.android.com/guide/topics/media/mediaplayer.html

0
source
  Intent musicIntent = new Intent(); //use Action VIEW to launch app musicIntent.setAction(Intent.ACTION_VIEW); String mimeType = getMimeTypeFromFile(); // in your case it is audio Uri uri = " yourfile uri"; musicIntent.setDataAndType(uri,mimeType); startActivity(intent); 

The default view application will be open if the mime type is changed, for the video mixer the mime type video will be opened, for the audio music player and for the image gallery.

If there are several players, then the Resolver action will be open.

Remember to catch an ActivityNotFoundException

In the music player ACTION_VIEW, the AudioPreview function is called with priority. there are other actions that handle this action, just check once with the action " com.android.music.PLAYBACK_VIEWER " instead of ACTION_VIEW or with it.

0
source

it launches the compact version for playback, since this application implemented it in this way, you can find many third-party applications that open the full application with

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

how have you tried this

0
source

Source: https://habr.com/ru/post/989396/


All Articles