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??
Related
I pick a video from Google Photos app, using:
Intent intent = new Intent(Intent.ACTION_PICK,
MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, PLAY_VIDEO);
and on ActivityResult I get the following uri:
content://com.google.android.apps.photos.contentprovider/-1/2/content%3A%2F%2Fmedia%2Fexternal%2Fvideo%2Fmedia%2F86/ORIGINAL/NONE/video%2Fmp4/1871297128
Then I try to open the same video in Google Photos by given link, through:
Intent configIntent = new Intent(Intent.ACTION_VIEW, link);
startActivity(configIntent);
But the only thing I get is the toast message: "Media not found".
Why? Maybe I need to specify some extras to opening intent?
I am developing an application. I want to realize a function like that I want to call the system default player.
After downloading the custom Audio or video file. Could it play using the system audio and video player in the program?
I want to create intent, and give the file path. Then call the system’s player.
any idears?
Try This
videourl="/sdcard/zzzz.3gp";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(videourl));
startActivity(intent);
String video = "http://www.nandudu.com/hls/course/video/2/test.m3u8";
Intent openVideo = new Intent(Intent.ACTION_VIEW);
openVideo.setDataAndType(Uri.parse(video), "video/*");
startActivity(openVideo);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(videourl));
startActivity(intent);
The above code should works for you to play video files
For kotlin:
val path: String = contentUri.getPath()
val file = File(path)
val intent = Intent(
Intent.ACTION_VIEW,Uri.fromFile(file))
intent.setDataAndType(Uri.parse(path), "video/*")
startActivity(intent)
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.
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.
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);