Launching Media Player using Intent - android

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.

Related

Record audio using recorder and then upload that on server android

I am trying to record audio in my app
On a button press
Intent intent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
startActivityForResult(intent, ACTIVITY_RECORD_SOUND);
But it is giving me error
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.provider.MediaStore.RECORD_SOUND }
but when i install any sound recorder from play store this error is gone but after recording the audio it does not return anything . It stay on the recorder window .
also what i have to write to get that audio file and upload it to the server
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
}
You can use below code.
MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setOutputFile(path);
// recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC); for mp3 audio
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.prepare();
recorder.start();
and path could be anything where file exists.for e.g.
path = Environment.getExternalStorageDirectory() + "/Android/data/" + context.getApplicationContext().getPackageName() + "/files/" + "myrecording.m4a";
But it is giving me error
There is no requirement for an Android device to have an activity that responds to that Intent action.
when i install any sound recorder from play store this error is gone but after recording the audio it does not return anything . It stay on the recorder window .
That app has a bug, apparently.
also what i have to write to get that audio file and upload it to the server
Quoting the documentation for RECORD_SOUND_ACTION, the output is "An uri to the recorded sound stored in the Media Library if the recording was successful". IOW, call getData() on the Intent delivered to onActivityResult(), and that is supposed to be a Uri pointing to the recorded audio. Note that this does not have to represent a file on the filesystem, let alone one that you can access.

Play song on default music player - 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.

How to upload a sound clip to SoundCloud

In my android app I'm recording an audio file which I then want to share to SoundCloud and GET the url of that. What's the best way to do so?
I can share through an explicit Intent but how can I get the track url then? shall I use the java wrapper to upload the audio?
File myAudiofile = new File("/path/to/audio.mp3");
Intent intent = new Intent("com.soundcloud.android.SHARE")
.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(myAudiofile))
.putExtra("com.soundcloud.android.extra.title", "Demo");
// more metadata can be set, see below
try {
// takes the user to the SoundCloud sharing screen
startActivityForResult(intent, 0);
} catch (ActivityNotFoundException e) {
// SoundCloud Android app not installed, show a dialog etc.
}
Refer:
https://github.com/soundcloud/android-intent-sharing/wiki/Explicit-intent-sharing

Android how to play video using VLC Player?

I have app for play video in android like as
https://play.google.com/store/apps/details?id=com.vlcforandroid.vlcdirectprofree&hl=en
I want to integrate this app in my app. I have Url for video Streaming and i want to open this video in this app(Vlc Direct), Any idea?
I open this app using:
Intent i = new Intent(Intent.ACTION_MAIN);
PackageManager manager = getPackageManager();
i = manager.getLaunchIntentForPackage("com.vlcdirect.vlcdirect");
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
But how it start with video streaming, Or any other Player for video Streaming?
More like,
Intent i = new Intent(Intent.ACTION_MAIN);
i.setComponent(new ComponentName("com.vlcdirect.vlcdirect", "com.vlcdirect.vlcdirect.URLStreamerActivity"));
i.putExtra("url", url);
startActivity(i);
Which supposes that the component, activity, and payload are as shown, and also that the activity is explicitly or implicitly exported -- I don't know the actual values, or if the activity is exported. vlcdirect doesn't document this, but you can
ask the developer, or
view the log as you stream from a URL within that app, to identify the component and activity; dedex and decompile the .apk, to confirm the payload; duplicate payload classes, if necessary; give up and fume after the developer ignores you, if the activity is not exported.
Ideally you would broadcast a "view the stream from this URL" intent, and vlcdirect or any other suitable app would pick it up, but I don't know if vlcdirect or any other app respond to such.
VLC needs to be explicitly told the type:
Intent i = new Intent(Intent.ACTION_VIEW);
i.setPackage("org.videolan.vlc.betav7neon");
i.setDataAndType(Uri.parse("http://ip:8080"), "video/h264");
startActivity(i);
i.e. just "video/*" wouldn't work for me
Previous answers didn't work me. After a bit of searching I found the official docs here.
Basically, here it is:
int vlcRequestCode = 42; //request code used when finished playing, not necessary if you only use startActivity(vlcIntent)
Uri uri = Uri.parse("file:///storage/emulated/0/Movies/KUNG FURY Official Movie.mp4"); //your file URI
Intent vlcIntent = new Intent(Intent.ACTION_VIEW);
vlcIntent.setPackage("org.videolan.vlc");
vlcIntent.setDataAndTypeAndNormalize(uri, "video/*");
vlcIntent.putExtra("title", "Kung Fury");
vlcIntent.putExtra("from_start", false);
vlcIntent.putExtra("subtitles_location", "/sdcard/Movies/Fifty-Fifty.srt"); //subtitles file
startActivityForResult(vlcIntent, vlcRequestCode);
You can remove unnecessary parts for you.

Calling YouTube app using ACTION_VIEW intent Failing most of the time

I've written a small app to parse some RSS feeds from YouTube and launch videos selected by the user. To play the video, I'm using an intent:
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(videoAddress);
In order to call the YouTube app, if installed on the device.
The problem I'm having is that, of the population of videos I am using in my app, about 90% of them display a 'Cannot play video' error message: "Sorry, this video cannot be played.". A few of them work just fine from my app. The videos that do not work will play fine in the YouTube app if searched for and launched entirely from within the YouTube app.
Has anybody seen this behavior, or does anybody have any ideas for things to try? Obviously the YouTube app launches videos in a slightly different way internally than it does from an Intent request, but I haven't a clue how to get to the bottom of it.
I have the same issue. Are you sure that all of the video play correctly from the youtube app? In my case, on an old G1, the videos I can't play from my app won't play even if searched from within the youtube app.
I think the video encoding is not supported in some case and/or the combination of a slow cpu and slow network make the video not playable.
I've read about people just refreshing many times untill the video starts playing... I guess in thier care it was a network/buffering issue.
More discussion here:
http://www.google.com.tw/support/forum/p/android/thread?tid=3a62cdf7188384af&hl=en
For this reason my App (similar to yours) got a lot of bed comments. I republished it only for Android >=2.1 and I now I have fewer bad feedback.
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(YOUTUBE_URL)));
The above line of code works for my App.
What it basically does is lets Android handle the startActivity with available installed software on the device. Android in turn opens the IntentChooser and lets the user decide which appropriate software to use in this case a Browser and Youtube App to open the video.
Try it out and let me knwo if it works for you or f you have any other issues.
The most reliable method I have found for accessing youtube from an application is using the mobile site, try this instead (eg for searching):
String videoUrl = "http://m.youtube.com/#/results?q=ciaconna+bach";
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(videoUrl)));
This solved the "Cannot play video' error message" that I was receiving.
I use this code:
String vid= Uri.parse(urlVideo).getQueryParameter("v");
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:" + vid));
try{
startActivity(intent);
}
catch (ActivityNotFoundException ex){
Log.e(TAG, "Couldn't find activity to view this video");
}
May be works for you.
I have got the same problem only with HTC Hero 2.1. You can force the intent to launch the htc flash player instead of the Youtube app. With the flash player app I have not had any problem:
Uri uri = Uri.parse("vnd.youtube:" + videoUrl);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
String availableFlashPlayer = availableFlashPlayer();
if (availableFlashPlayer != null) {
// launch the intent with the available flash player
intent.setPackage(availableFlashPlayer);
}
startActivity(intent);
The availableFlashPlayer method:
public String availableFlashPlayer() {
String availableFlashPlayer = null;
String FLASH_PLAYER = "com.htc.flash";
PackageManager pm = getPackageManager();
try {
ApplicationInfo ai = pm
.getApplicationInfo(FLASH_PLAYER, 0);
if (ai != null) {
availableFlashPlayer = FLASH_PLAYER;
}
} catch (Exception e) {
Log.e(TAG, e.getMessage(), e);
}
return availableFlashPlayer;
}
You can also check the Adobe Flash Player:
String FLASH_PLAYER = "com.adobe.flashplayer";
Alternatively, you can force the intent to launch the Android Browser as follows:
Uri uri = Uri.parse(videoUrl);
String packageName = "com.android.browser";
String className = "com.android.browser.BrowserActivity";
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.setClassName(packageName, className);
startActivity(intent);

Categories

Resources