Play song on default music player - android - android

My app show list of song from sd card. I want to be able to play the song on the default player. I've got all the data about the song: id, title, album, artist, path...
Is there a way to launch the default player to play the song?
What I've tried:
Using Intent.CATEGORY_APP_MUSIC. I can launch the default player, but can't set the song.
Intent.makeMainSelectorActivity(Intent.ACTION_MAIN, Intent.CATEGORY_APP_MUSIC) open the default music app.
Howeverintent.setData(Uri.withAppendedPath(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id)) throws exception that intent not found.
Using deprecated MediaStore.INTENT_ACTION_MUSIC_PLAYER. Activity not found.
Intent(MediaStore.INTENT_ACTION_MUSIC_PLAYER).setDataAndType(Uri.fromFile(songFile), "audio/*")
Using INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH. Launch the search, but not the song, says couldn't prepare mix on Play music. However, it works from Google now, so it may be the key.
Intent(MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH).putExtra(SearchManager.QUERY, name)
Using ACTION_VIEW. it works but it launch a compact version of the player, and I want the full player.
Note: I want to launch External player, not in my app.
Update: Looks like Google Now hardcoded Play Music and Youtube support.

If you would like to start the default music player app on the device, should 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);

it is launching a compact version for playing as that app has implemented it that way,
you can find many 3rd party app that open the full app 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);
as you already tried this

Try with something simple like this:
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 one more example with MadiaPlayer and file saved in 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
More about: http://developer.android.com/guide/topics/media/mediaplayer.html

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 opened if the mime type is changed, for video mime-type video player will be opened, for audio music player and for image gallery.
If there are multiple player then Resolver activity will be opened.
Don’t forget to catch ActivityNotFoundException
In Music player for ACTION_VIEW AudioPreview activty is called with priority. there are other activities which handle this action , just check once with action "com.android.music.PLAYBACK_VIEWER" instead of ACTION_VIEW or with it.

Related

Playing raw audio file in default music player

My App is having audio files in raw folder. Now whenever app gets launched these audio files should play using default music player. I tried below code App is crashing with "No Activity found to handle this intent".
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("companion.harman.com.ceshuclient/res/raw/fav.mp3"), "audio/*");
startActivity(intent);
The same audio files can be played using "MediaPlayer" object but that is not what exactly I'm trying to acheive.
Any sample code or links can be helpful.
Thanks in Advance.
Please try the following (Where mp3file is the name of the file in the raw folder without extension. Use only lowercase characters, please):
MediaPlayer player = MediaPlayer(this,R.raw.mp3file);
player.prepare();
player.start();
player.stop();
player.reset();
player.release();
player = null;
Here is the link of demo source code to play mp3 File

Launching Media Player using Intent

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.
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.
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.

Start default music app in Android and play file

I have app where I have url of media file.
I wish if I could start Music and Video app and play file.
Is there any chance to do that.
I dont want to write my own media player app.
Create an ACTION_VIEW intent with the path to your media file and include the type of media you want to play.
String audioPath = "path/to/audio.mp3";
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
// set the data and type, for movies this would be video/*
intent.setDataAndType(Uri.parse(audioPath), "audio/*");
getContext().startActivity(intent);
You don't have to set the type, but I would suggest doing so. Setting the type will limit the activities to activities that know how to handle the given type.

android: using default video player

I have an application that plays video files.
I have been using code for using Videoview and starting the Videoview manually to play video files. However, I just wanted to know if I can use the default media player or video player of android rather than creating or using the VideoView to play the file..
Please make comments if the question is not clear.
Thanks alot
Sure - just use an Intent with the file Uri.
File file = new File("fileUri");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "video/*");
startActivity(intent);

Play audio files with default player

I want to play an MP3 file using the default Android player. I managed to get the file playing but it plays in the background. I want to have all the nice controls for pausing, playing, etc.
My code now is something like:
String link = "http://www.example.com/file.mp3";
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(link);
mp.prepare();
mp.start();
How can I do it that when this file begins to play to go to another screen with the player and all the nice controls?
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.site.com/file.mp3");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
If the particular Action doesn't work, take a look here: http://developer.android.com/reference/android/content/Intent.html

Categories

Resources