Android Random Alarm Tone - java

I'm currently working on a alarm app that plays a random alarm tone. I've been able to load a ringtone from the ringtone dialog but I'm thinking of loading a random ringtone directly from the ringtone directory. How would I go about accomplishing this?

You can query for available ringtones in the device:
RingtoneManager ringtoneMgr = new RingtoneManager(this);
ringtoneMgr.setType(RingtoneManager.TYPE_ALARM);
Cursor alCursor = ringtoneMgr.getCursor();
This alCursor will contain the available ringtone URIs. Now iterate through them & build a URI array:
Uri[] alarms = new Uri[alCursor.getCount()];
while(alCursor.moveToNext()) {
int pos = alCursor.getPosition();
alarms[pos] = ringtoneMgr.getRingtoneUri(pos);
}
alCursor.close();
Now, just generate a random number in the range of 0-alCursor.getCount(), take that ringtone from alarms array & play it.
Random r = new Random();
int randNum = r.nextInt(alCursor.getCount());
Ringtone ringtone = RingtoneManager.getRingtone(this, alarms[randNum]);
ringtone.play();
This can be an easy approach to achieve your goal. Hope it helps. Thanks.

Related

How to make exoplayer's ClippingMediaSource more precise?

I try to play short piece of mp3 audio (44100 samples rate) with exoPlayer using it's ClippingMediaSource but it cuts the start of the piece for about 250 milliseconds, it's too much, is it possible to make it more precise?
I start exoplayer like this:
dataSourceFactory = new DefaultDataSourceFactory(context, Util.getUserAgent(context, "com.example.player"));
Uri uri = Uri.fromFile(new File(path));
MediaSource audioSource = new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(uri);
ExoPlayer exoPlayer = ExoPlayerFactory.newSimpleInstance(context, new DefaultTrackSelector());
exoPlayer.setPlayWhenReady(true);
ClippingMediaSource clip = new ClippingMediaSource(audioSource, 7_225_000, 8_175_000);
exoPlayer.prepare(clip);
According to the exoplayer documentation, there are the two methods
ClippingMediaSource(MediaSource MediaSource, long startPosition, long endPositionUs)
Creates a new clipping source that wraps the specified source and provides samples between the specified start and end position.
ClippingMediaSource(MediaSource mediaSource, long startPositionUs, long endPositionUs, boolean enableInitialDiscontinuity)
try the second method with false or adjust startPosition and endPosition.
For more check, this clipping documentation of ExoPlayer. Hope this will solve your issue.

How to stream the media, playing in media player to other peer devices media player over WiFi?

So, in my application, I want to add the functionality of local audio streaming over WiFi to connected peer devices. i'm following this tutorial for audio streaming over WiFi. It is working perfectly. But the issue is, I've to specify the file name and exact path which I want to stream, explicitly.
In My application, I've used Media Store to get music available on the users device. So there is no path available for each media item. Even if I can get exact path of each media item, the problem is to seek to the desired time. Another problem is that I've to restart all the streaming threads when one song gets completed and other song starts.
So my Idea is to stream the content of media player itself over the WiFi. So that there will be no problem of seeking or updating the threads. When host seeks to desired time, all devices will receive update automatically because we are now streaming the content of media player itself.
I've searched on Google about this, all I got is MediaPlayer.setDataSource() method. Which is exactly reverse of my case. Also there is no such thing mentioned in official Android documentation of Media Player.
For reference, I'm using the following method to get song list using media store.
public void getSongList() {
//retrieve song info
ContentResolver musicResolver = getActivity().getContentResolver();
Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);
//iterate over results if valid
if(musicCursor!=null && musicCursor.moveToFirst()){
//get columns
int titleColumn = musicCursor.getColumnIndex
(android.provider.MediaStore.Audio.Media.TITLE);
int idColumn = musicCursor.getColumnIndex
(android.provider.MediaStore.Audio.Media._ID);
int artistColumn = musicCursor.getColumnIndex
(android.provider.MediaStore.Audio.Media.ARTIST);
int albumId = musicCursor.getColumnIndex
(MediaStore.Audio.Media.ALBUM_ID);
int data= musicCursor.getColumnIndex(MediaStore.Audio.Media.DATA);
int albumkey=musicCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_KEY);
//add songs to list
do {
long thisId = musicCursor.getLong(idColumn);
String thisTitle = musicCursor.getString(titleColumn);
String thisArtist = musicCursor.getString(artistColumn);
long thisalbumId = musicCursor.getLong(albumId);
String thisdata= musicCursor.getString(data);
String AlbumKey = musicCursor.getString(albumkey);
tempSongList.add(new Song(thisId, thisTitle, thisArtist, thisalbumId, thisdata, AlbumKey));
}
while (musicCursor.moveToNext());
}
}
I know I can get the exact path from this. Here is the answer.
But I want to stream the content of media player itself. Is it possible or not? Is there any alternatives for this functionality?

Disable notification sound in android

I want to create a notification without any sounds. How can I do this?Thanks.
This is my code: Link
Excuse me my friends.I could not put my code here.
Thank you so much.
i really do not understand your problem but in simpler terms..this is the code for creating notifications in android:
Notification notifica = new Notification();
notifica.flags |= Notification.FLAG_AUTO_CANCEL;
notifica.icon = R.drawable.serie_notification;
notifica.when = System.currentTimeMillis();
And to disable sound you have to add this:
notifica.defaults = 0;

How set duration of the ringtone's play

My app play ringtone. My code:
RingtoneManager.getRingtone( context, RingtoneManager.getDefaultUri( RingtoneManager.TYPE_NOTIFICATION)).play();
Ringtone play very well. But if ringtone is long I need cut the first N milliseconds from it.
How get full duration of this ringtone?
How set maximum duration in milliseconds of this ringtone?
Just put the RingTone in mediaPlayer
for eg
Mediaplayer mp =new MediaPlayer();
uri = RingtoneManager.getDefaultUri( RingtoneManager.TYPE_NOTIFICATION))
mp.setDataSoure(getApplicationContext(), uri);
mp.prepare();
mp.getDuration()
Use the mp.getDuration and do whatever u want.
Enjoy

Android - Ringtone wont stop playing

I currently start a Ringtone on Android and it plays fine.
However when I try to stop the ringtone it doesn't stop or atleast doesn't stop straight away, it will keep in playing until it just plays out.
Here is how I set up the Ringtone:
int rm = audio_service.getRingerMode();
int vs = audio_service.getVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER);
android.os.Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
if ((rm == AudioManager.RINGER_MODE_VIBRATE ||
(rm == AudioManager.RINGER_MODE_NORMAL && vs == AudioManager.VIBRATE_SETTING_ON)))
v.vibrate(vibratePattern,1);
if (audio_service.getStreamVolume(AudioManager.STREAM_RING) > 0) {
oRingtone = RingtoneManager.getRingtone(this, Settings.System.DEFAULT_RINGTONE_URI);
oRingtone.setStreamType(AudioManager.STREAM_RING);
oRingtone.play();
}
And here is how I try to stop it
if (CallDialogActivity.oRingtone != null) {
Log.d("RINGTONE", "Into Ringtone if");
Ringtone ringtone = CallDialogActivity.oRingtone;
oRingtone = null;
ringtone.stop();
}
Has anyone come across similiar problems or see any mistakes in my code?
EDIT:
Just to add I have added logging to see if ringtone.isPlaying() returns true or false after I call ringtone.stop() and it returns false, however the tone keeps playing for a couple of seconds (3 - 5 seconds) after before stopping
For anyone that comes across this:
I had to use the RingTone manager to help stop the ringtone.
Adding the following line sorted it out
ringtoneManager.stopPreviousRingtone();

Categories

Resources