Android open default mp3 player within application - android

Should be really simple. I just want the default mp3 player to open when a button is pressed as its own activity. I need it to work on api 3 and higher
This opens a media player but it is not the default:
//doesnt open default player
Intent viewMediaIntent = new Intent();
viewMediaIntent.setAction(android.content.Intent.ACTION_VIEW);
//File file = new File(objectFilePath);
viewMediaIntent.setDataAndType(null, "audio/*");
viewMediaIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
act.startActivity(viewMediaIntent);
this doesn't work:(no activity found to handle intent)
Intent intent = new Intent("android.intent.action.MUSIC_PLAYER");
act.startActivity(intent);
This also works but not for default:
Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
act.startActivity(it);
thank you for your time.

Related

Play music after selecting track from media player

I want to select a track from music player and then return the selected track to my app.
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_PICK);
intent.setData(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 1);
then in OnAcvtivityResult, i'm storing the result in a string.
String song=data.getData().toString();
then when user presses a button, I want to play the music
Intent intent1 = new Intent();
intent1.setAction(android.content.Intent.ACTION_VIEW);
File file = new File(song);
intent1.setDataAndType(Uri.fromFile(file), "audio/*");
startActivity(intent1);
but this does not work. It shows that cannot play files of this format.
How can I correct this??

How to start the Service of default music player

I just need to start the SERVICE of default Music player. I can play the song if the service is started but have no idea how to start the service of music player
Intent i = new Intent("com.android.music.musicservicecommand");
i.putExtra("command", "play");
context.sendBroadcast(i);
Try this
Intent it = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("file:///sdcard/song.mp3");
it.setDataAndType(uri, "audio/mp3");
startActivity(it);
or
Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
or You can simply pass the url to Media Player like this,
Uri myUri = Uri.parse("your url here");
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(myUri, "audio/*");
startActivity(intent);
Extracted from https://snipt.net/Martin/android-intent-usage/
I haven't tested myself.

Android: playing a song file using default music player

Is there a way to play media with the default media player? I can do this with the following code:
Intent intent = new Intent(Intent.ACTION_VIEW);
MimeTypeMap mime = MimeTypeMap.getSingleton();
String type = mime.getMimeTypeFromExtension("mp3");
intent.setDataAndType(Uri.fromFile(new File(songPath.toString())), type);
startActivity(intent);
But this launches a player with less controls and can't be pushed to the background.
Can I launch the player with the default media player?
Try the below code:::
Intent intent = new Intent(MediaStore.INTENT_ACTION_MUSIC_PLAYER);
File file = new File(songPath.toString());
intent.setDataAndType(Uri.fromFile(file), "audio/*");
startActivity(intent);
Updated:: Try this also
Intent intent = new Intent();
ComponentName comp = new ComponentName("com.android.music", "com.android.music.MediaPlaybackActivity");
intent.setComponent(comp);
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File(songPath.toString());
intent.setDataAndType(Uri.fromFile(file), "audio/*");
startActivity(intent);
I've been researching this for the last few days as I don't have the stock music player. It seems so tragic that it can't be done easily. After looking through various music app's AndroidManifest.xml for clues I stumbled upon MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH.
Using the below method I'm able to start the Samsung music player in the background as long as the song is in the Android MediaStore. You can specify Artist, Album or Title. This method also works for Google Play Music but unfortunately even the newest version of the stock Android player does not have this intent:
https://github.com/android/platform_packages_apps_music/blob/master/AndroidManifest.xml
private boolean playSong(String search){
try {
Intent intent = new Intent();
intent.setAction(MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(SearchManager.QUERY, search);
startActivity(intent);
return true;
} catch (Exception ex){
ex.printStackTrace();
// Try other methods here
return false;
}
}
It would be nice to find a solution that uses a content URI or URL but this solution works for my application.

Android:open default audio player

I am making an app in which I have to open default audio player. My code is as follows:
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File(songs.get(position));
//String introURI = "file:///sdcard/"+".mp3";
intent.setDataAndType(Uri.fromFile(file), "audio/*");
startActivity(intent);
When the song is touched, the player gets open but it gives message that "this type of file is not supported".
The MediaPlayer class should be used when you want to implement your own media player. If you want to use an existing player, you'll have to launch the appropriate intent, for example:
Uri uri = Uri.parse("http://www.example.com/file.mp3");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
EDIT
How to get android local files uri
It will work. Try this
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File(path);// path where your while is placed.For me like /storage/sdcard0/Media/audio/%2F1506580826442?alt=media&token=0e22f657-743c-4aed-9fed-48de69aced73.mp3
intent.setDataAndType(Uri.fromFile(file), "audio/*");
ActivityChatView.mContext.startActivity(intent);

startActivity on "file://" links crashes

I am trying to create program that simply opens file on sdcard. I tried opening mp3, mp4, and apk - the code bellow always crashes unexpectedly.
String _path = "file:///sdcard/1.apk";
Uri outputFileUri = Uri.parse(_path);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Market links also crashes. But when I set _path="http://google.com" - browser opens normally. How can I make this code work?
If you're trying to install the apk, you need to use the following:
String fileName = Environment.getExternalStorageDirectory() + "/1.apk";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(fileName)), "application/vnd.android.package-archive");
startActivity(intent);
If you're trying to launch the app (after installing), then:
Intent intent = new Intent();
intent.setClassName("com.pkg.addr", "com.pkg.addr.MainActivity");
startActivity(intent);
If you're trying to launch a player to play the mp3 file:
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
Uri data = Uri.parse("file:///sdcard/1.mp3");
intent.setDataAndType(data,"audio/mp3");
startActivity(intent);
Hope that helps.

Categories

Resources