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);
Related
I'm developing an app where I have a Fragment for a Level Complete and I want to play a sound when this Fragment loads.
I think that I have to write the MediaPlayer in the onCreate method but I don't know exactly how to write the MediaPlayer code.
I've already loaded the mp3 file in the Raw Folder
MediaPlayer player = MediaPlayer.create(Activity.this, R.raw.some_file);
player.start();
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.
[First App] I am creating a sort of Alarm app that allows user to select alarm sound from either sd-card or app-supplied sounds. Since, the app essentially plays alarms, I want the volume to be 'alarm volume' of the device. I am able to achieve this for sd card sounds. But, I am unable to setAudioStreamType for raw resource sounds.
I am using following code :
MediaPlayer m_player = new MediaPlayer();
m_player.setAudioStreamType(AudioManager.STREAM_ALARM);
switch (bin_name) { //bin_name = various user selectable music files
default:
m_player = MediaPlayer.create(context, R.raw.blu);
break;
}
m_player.setLooping(true);
m_player.start();
My blu.mp3 plays at media volume only. Upon checking the documentation for MediaPlayer.create(Context context, int resid), I found this :
Note that since prepare() is called automatically in this method, you cannot change the audio stream type (see setAudioStreamType(int)), audio session ID (see setAudioSessionId(int)) or audio attributes (see setAudioAttributes(AudioAttributes) of the new MediaPlayer.
I also tried finding code samples for above method but none of them showed how to set AudioStreamType to AudioManager.STEAM_ALARM. I will accept answers with alternative ways that simply play the sound with infinite loop. How to achieve this ?
As the documentation you are referring to says, you must create and prepare the MediaPlayer yourself. Haven't tried with the STREAM_ALARM but I'm using following snippet to play on STREAM_VOICE_CALL
Uri uri = Uri.parse("android.resource://com.example.app/" + R.raw.hdsweep);
MediaPlayer mMediaPlayer = new MediaPlayer();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
mMediaPlayer.setDataSource(context, uri);
mMediaPlayer.prepare();
mMediaPlayer.start()
I am working with streaming video and I want to change from one source to another dinamically.
First I set the video uri to the VideoView
view.setVideoURI(myUri);
And I know that I am capable of changing it afterwards by doing (this is in onPrepare method but it could go somewhere else where I have access to the MediaPlayer).
#Override
public void onPrepared(MediaPlayer mp)
{
Uri newUri = getOtherUri();
mp.reset();
mp.setDataSource(getApplicationContext(), newUri);
mp.prepare();
mp.start();
}
The thing is, I want to change the source without reseting the mediaPlayer (I do not want to disturb the user).
I tried to create a new VideoView with the new Uri and then change one object for the other, and likewise with the media player. However, none of that seems to work.
Is it possible to replace a video while it is playing in Android?
Any comments would be appreciated.
There is no option to reset the video without using mediaplayer.stopPlayback() or mediaplayer.reset. The reason is that; the previous object of the mediaplayer has to be released before you can play other video .
I want to change the source without reseting the mediaPlayer (I do not want to disturb the user).
Well, this cannot be achieved as the mediaplayer has to be reset. So there will be lag while changing videos. And to satisfy you, you can see these behavior in any videoplayer app like youtube or mxplayer.
So the only thing you can do is to show progressbar while loading or changing video.
Hope it helps. Cheeers.:)
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.