I have a couple of ringtones, distributed with APK file and access them the following way
Ringtone r = RingtoneManager.getRingtone(mContext, uri);
if (r != null) {
r.setStreamType(AudioManager.STREAM_NOTIFICATION);
r.play();
}
In most cases this works as expected but in some devices system plays incorrect ringtone that was not supposed to be ever played. After short investigation I found out that documentation for the RingtoneManager.getRingtone method says that
If the given URI cannot be opened for any reason, this method will attempt to fallback on another sound. If it cannot find any, it will return null.
That means that I cannot ever be sure that returned ringtone I the one I have asked for. So my question is how can I at least verify that getRingtone method has returned me another(default) ringtone so that I could at least not play it?
Thanks in advance.
It seems the whole job of RingtoneManager is to do said fallback, so if that's not what you want, consider playing your audio using android.media.MediaPlayer.
Related
I do not know if this is a bug in Android or if I am missing something here, but I can never get the actual language code of a given audio track with this MediaPlayer method:
MediaPlayer.TrackInfo[] mediaplayerTrackInfoArray = myMediaPlayer.getTrackInfo();
String a_string = mediaplayerTrackInfoArray[index].getLanguage(); // index must indicate the position of an actual audio track
All I always get is the String: "und", which supposedly means: undetermined.
I know that the video file being analyzed does contain the audio tracks with their respective language codes, for example: "eng" or "rus", because they show up correctly in an external media player like VLC.
Is there a fix to this bug? Or a substitute method or object?
I have a working Alert Receiver etc., and it starts my 'Alarm Screen Activity'.
I want this activity to play an MP3 file from resources as an alarm tone.
Found lots of questions and answers but no working solution :-(
I have my file in: ... MyFirstApp\app\src\main\res\raw folder
The below code is on the onCreate method of my activity:
Uri uri_a = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
Uri uri_b = Uri.parse("android.resource://com.myapps.myfirstapp/res/raw/" + R.raw.def_alarm_tone);
ringTone = RingtoneManager.getRingtone(getApplicationContext(), uri_b);
ringTone.play();
If I use uri_a on line 3 - it works and plays the default alarm ringtone.
If I use uri_b - I get no sound - Errors in logs show java.io.FileNotFoundException.
So, any suggestions on what I'm doing wrong - or alternatives / better solutions???
Also, some suggestions on managing apps alarms/reminders would be nice.
What are the pros/cons of using MediaPlayer for alarms as an alternative?
Why don't you try something like this?
MediaPlayer BG;
....
BG = MediaPlayer.create(getBaseContext(), R.raw.def_alarm_tone);
BG.setLooping(false);
BG.setVolume(100, 100);
...
//whenever you want to use it
BG.start();
Make sure the file is in the App res/raw directory.
Turns out I was having trouble using URI's because my application ID didn't match my package name in Gradle Script (Module: app) ... as I changed my application name recently.
Fixed that, and now it works as expected.
Currently i am playing ringtones using the following code :
Ringtone ringtone = RingtoneManager.getRingtone(this, Uri.parse(selectedRingtone));
ringtone.play();
If i play default or system ringtones, they play in loop or repeatedly. But if i choose any other sound from my gallery or media store , it only plays it once. I have gone through some links but i dnt want to use MediaPlayer. I just need to use RingtoneManger.
You have to use MediaPlayer.
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
MediaPlayer player = MediaPlayer.create(this, notification);
player.setLooping(false);
player.start();
There is not any method related to loop in Ringtone and RingtoneManager
It looks like file, that you are playing (default), has an information about looping in file itself. If you want to loop other files, you have to make sure, that these files contain information about looping too. It will help you archive your goal with Ringtone.class.
Otherwise, I would recommend you using MediaPlayer. It will allow you to loop sounds even without information about loop in files (why you don't want to use this class?)
If you are targeting Api 28 or greater, there's now a setLooping method in Ringtone
https://developer.android.com/reference/android/media/Ringtone.html#setLooping(boolean)
So in your original example, you just add
ringtone.setLooping(true);
According to this post, the song's artist, album, track can be retrieved from a BroadcastReceiver, but the song's information which are current playing in Spotify or Pandora cannot be retrieved.(Maybe because these apps don't send broadcast while playing songs).
My question is, how to get current playing song's information on system level, no matter whether the app is broadcasting.
Listening for com.android.music.* broadcasts is the official way to do this on Android.
Many apps like lyrics displayers rely on it to get info on the current track.
Sadly, if the app is not broadcasting the solution is either to have a look at their Dev documentation in case they provide another way to do this or directly ask them to implement it.
For Spotify, it looks like right now they use a 'com.spotify.music.metadatachanged' broadcast (and a settings in the app). You will have to track down separate cases like this one, nothing forces Android devs to implement the standard broadcast.
In android 4.4+, you can use the RemoteController class to get track information from the current playing song. This works with every app that implements a RemoteControlClient. It also works with video- and streamservices like chromecast.
http://forum.xda-developers.com/showthread.php?p=48702254#post48702254
This is a very handy guide if you want to use this RemoteController.
And the developers page:
https://developer.android.com/reference/android/media/RemoteController.html
definitely u know the path of the song where is running
Uri myUri1 = Uri.parse(path);
now u can call this method having the path then we can get all information either in the activity or from the service by binding
private void getTrackInfo(Uri audioFileUri) {
MediaMetadataRetriever metaRetriever= new MediaMetadataRetriever();
metaRetriever.setDataSource(getRealPathFromURI(audioFileUri));
String artist = metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
String title = metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);
}
private String getRealPathFromURI(Uri uri) {
File myFile = new File(uri.getPath().toString());
String s = myFile.getAbsolutePath();
return s;
}
I need to know is it possible to play a particular audio file from android in-built ringtone files. For example assume Tone_23 is in android ringtone list, now i need to play this particular tone, when i click a button. I searched in Google i got a guidance how to call/show RingtonePicker Activity (The entire Ringtone list will be displayed). If this possible means, kindly share your thoughts. Thanks in advance.
You can try some thing like this :
/**
* Play ring tone.
*
* #param ringToneTitle the ring tone title
*/
void playRingTone(String ringToneTitle) {
RingtoneManager ringtoneManager = new RingtoneManager(
getApplicationContext());
ringtoneManager.setType(RingtoneManager.TYPE_RINGTONE);
int length = ringtoneManager.getCursor().getCount();
for (int i = 0; i < length; i++) {
Ringtone mRingtone = ringtoneManager.getRingtone(i);
if (mRingtone != null) {
Log.d("ringtoneTitle ", mRingtone.getTitle(getApplicationContext()));
if(ringToneTitle.equalsIgnoreCase(mRingtone
.getTitle(getApplicationContext())) {
mRingtone.play();
}
}
}
}
Hope this helps.
Added : Also have a look at this class RingtoneManager
RingtoneManager provides access to ringtones, notification, and other types of sounds. It manages querying the different media providers and combines the results into a single cursor. It also provides a Ringtone for each ringtone. We generically call these sounds ringtones, however the TYPE_RINGTONE refers to the type of sounds that are suitable for the phone ringer.
I think you can only play the ringtone that is set in the system automatically with for example:
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();
If you would always want to play a specific tone/file you would have to add it to your assets and play it from there.