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.
Related
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.
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);
I'm trying to run a mp3 file using android media player. I'm using this code below
MediaPlayer mediaPlayer = MediaPlayer.create(this, "mmt/sdcard/mp3/file.mp3");
mediaPlayer.start();
But Ecliplse showing error & it says I can't use string as argument of create method. How can I run the file using this file location?
Plz help
you should use setDataSource to set the source of your media files. For files on the sdcard, the setDataSource(String ds) works best.
Also, note that your url should probably be mnt instead of mmt, and you need a forwardslash at the beginning, making your full url look like this: /mnt/sdcard/mp3/file.mp3
Hi there I'm an android and java newbie and is wondering if there is a better way of doing the following:
I currently have 250 short audio files (.3gp) that i put inside my res/raw folder and based on a certain condition, I want to play certain audio file. For example if the text of the button that is being clicked is the word "and", I want to play the and.3gp sound file.
The following is the code that I use to accomplish what I want to do:
if(word.equals("No"))
{
MediaPlayer mp = MediaPlayer.create(HomeActivity.this,R.raw.no);
mp.start();
mp.setOnCompletionListener(listener );
}
else if(word.equals("And"))
{
MediaPlayer mp = MediaPlayer.create(HomeActivity.this,R.raw.and);
mp.start();
mp.setOnCompletionListener(listener );
}
.....
The above code is working fine so far but I'm wondering if I can do this without having a 250 else if condition. Is there a way to pass in the audio file to MedialPlayer.Create by file name via Uri? If yes, how do I do it? Thank you.
You can make a hash function that maps a string key to the uri
Loop through all your files in your raw folder and remove the file extension, then test against that.
I'm new in Java/Android programming, so please have patience with me.
I try to play a mp3 which is locate und the assets folder. I know there is another way with the /res/raw/ folder, but use the assets-folder because later I'll try to access the file by String.
This code works to play a mp3-file:
try
{
MediaPlayer mp = new MediaPlayer();
FileDescriptor sfd = getAssets().openFd("song.mp3").getFileDescriptor();
mp.setDataSource(sfd);
mp.prepare();
mp.start();
}
catch(Exception e) {}
Now the problem: In the same assets-folder is another mp3 file stored. Though I specify the name of the mp3 to use it take the one which comes first in alphabet. E.g. the other file is named "music.mp3" it plays this one. Renaming it to "worldmusic.mp3" it will play "song.mp3". Rerename "worldmusic.mp3" back to "music.mp3" it will take this mp3 again. Another test: Renaming "song.mp3" to something other so the application can find whats specify by the code above will result that no song is played. So this means the songname have to exist, although it take arbitrary the song first in alphabet.
I'm testing with the AVD emulator of eclipse. But I think the behaviour would be the same on a real device.
Have someone an idea about this problem?
I don't believe using FileDescriptor is the right way to do this. Try using .create() and a Uri instead:
MediaPlayer mp = MediaPlayer.create(getBaseContext(), songUri);
mp.start();
Not sure why, but the URI syntax doesn't seem to work for the assets. Try getting the AssetFileDescriptor instead, as answered in the related question:
Play audio file from the assets directory