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
Related
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.
This is my code for opening up the picker for Videos and Images - however I need to not show Videos that are longer than 5 minutes in length. Is this possible?
public void startChoosePhotoFromLibrary() {
if (checkOrRequestExternalStoreagePermission()) {
if (Build.VERSION.SDK_INT < 19) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/* video/*");
startActivityForResult(photoPickerIntent, PICK_PHOTO_ACTIVITY_REQUEST_CODE);
} else {
Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
photoPickerIntent.setType("*/*");
photoPickerIntent.putExtra(Intent.EXTRA_MIME_TYPES, new String[]{"image/*", "video/*"});
startActivityForResult(photoPickerIntent, PICK_PHOTO_ACTIVITY_REQUEST_CODE);
}
}
}
This is my code for opening up the picker for Videos and Images
ACTION_PICK does not use MIME types. ACTION_PICK picks from a collection of content, where that collection is identified by the Uri that you supply in the Intent.
Also, MIME types do not have spaces in them.
Is this possible?
Not via those Intent actions, or via any content-selection mechanism that is part of the Android SDK.
You are welcome to query the MediaStore for videos, and there may be a way to filter such videos by length. But then you would need to present your own UI for allowing the user to choose something from the query results (e.g., ListView, RecyclerView).
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.
I have working on to one android application.Normal concept of this application is to Play Song in Multiple Device at Same Time.Everything is done but Song is not play in Sync.I have done lot's of R&d to solve this problem but not getting success.but at end I have found one Issue of Android Media Player.For finding this issue I have making one demo Application.Description is follow.
Demo App Description
I create a two object of Android media player firstPlayer and secondPlayer.firstPlayer Song playing will be start once App is Lunch.now i have taking one Button in test.xml.once the User will click in this button secondPlayer is start and play same music which I set in firstPlayer.and I also Seek secondPlayer at Position where firstPlayer is Play.
Code
changePlayer = (Button) findViewById(R.id.changePlayer);
String fileName = "/qq.mp3";
String baseDir = Environment.getExternalStorageDirectory() + "/Test App" + fileName;
Log.e(TAG, "path :" + baseDir);
try {
firstPlayer = new MediaPlayer();
secondPlayer = new MediaPlayer();
firstPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
secondPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
firstPlayer.setDataSource(baseDir);
secondPlayer.setDataSource(baseDir);
firstPlayer.prepare();
secondPlayer.prepare();
firstPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
changePlayer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!secondPlayer.isPlaying()) {
Log.e(TAG, "Media Player Start M2");
secondPlayer.start();
}
int sek = firstPlayer.getCurrentPosition();
secondPlayer.seekTo(firstPlayer.getCurrentPosition());
//firstPlayer.seekTo(sek);
Log.e(TAG, "Current Playe Time ::" + secondPlayer.getCurrentPosition());
Log.e(TAG, "First Playe Time ::" + sek);
Log.e(TAG, "firstPlayer Playe Time ::" + firstPlayer.getCurrentPosition());
Log.e(TAG, "secondPlayer Playe Time ::" + secondPlayer.getCurrentPosition());
}
});
Problem
I am getting one Strange problem both media player not play in Sync it's having little bit different for play song.if I seek secondPlayer where firstPlayer is play then it's playing in Sync but it's not happend both player playing different.
Note
I also try to Solve this Issue using any third party Media player but i didn't get luck.I have implement ExoPlayer,VlcPlayer,UniversalPlayer to solve this problem but It's also having same problem.
Requirement.
I want to play song in Sync in Two different object of MediaPlayer.
Did anyone know about any powerful media player which is not having this delay issue.
I hardly Try to solve this Sync issue but i Didn't get Luck.I want 100% Sync in My Application.Just Like Seedio app in IOS.I also test this in IOS but in IOS it's Working fine Both Player play song with 100% sync.
Please Help me if Anyone have any Idea.
I hope you all are clear with my problem.
Thank You
The Seek does take some time on a MediaPlayer. (As I understand the official docs: https://developer.android.com/reference/android/media/MediaPlayer)
One idea is to have the second stream not started, seek to a known future position (higher than currect pos of first stream), hope it gets there "in time". And then start the second stream when you get there (time has passed). I have not tested it, just been thinking about it as I want to solve a very similar issue.
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();