Android - Ringtone wont stop playing - java

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();

Related

How to get button press event from camera

I've got a dental camera and iam try to get windows to press space when the camera button is pressed
I have the OEM software and driver installed, it works perfect, gets the feed and makes a snapshot when camera button is pressed. I need to use another software for the feed and the snapshot, the software gets the feed but doesn't react to camera button, it only reacts to space key press(part of the oem driver), so my way of solving this was getting the device by product id and listening the button press event and remapping it space press.
I am pretty much stuck at this point.
How can I listen on events coming from the device I've got?
public static Device findDCam(){
// Create the libusb context
Context context = new Context();
// Initialize the libusb context
int result = LibUsb.init(context);
if (result < 0)
{
throw new LibUsbException("Unable to initialize libusb", result);
}
// Read the USB device list
DeviceList list = new DeviceList();
result = LibUsb.getDeviceList(context, list);
if (result < 0)
{
throw new LibUsbException("Unable to get device list", result);
}
try
{
// Iterate over all devices and list them
for (Device device: list)
{
DeviceDescriptor descriptor = new DeviceDescriptor();
result = LibUsb.getDeviceDescriptor(device, descriptor);
if (result < 0)
{
throw new LibUsbException(
"Unable to read device descriptor", result);
}
if(descriptor.idProduct()== -3810){
System.out.println("D cam found");
return device;
}
}
}
finally
{
// Ensure the allocated device list is freed
LibUsb.freeDeviceList(list, true);
}
// Deinitialize the libusb context
LibUsb.exit(context);
return null;
}
I've also thought that maybe it's impossible using usb4java since as far as i understood, if i want to listen on the usb port i need to take control from the driver and then its pointless.
Maybe iam going all wrong and i should use the driver instead?
Or maybe there is an app that can read button presses from a specific device and remap it?
If the camera has a standard driver, this should work through this video capture SDK. To quick test it, run the demo executable included in the package, select the camera in the list, check the "webcam snapshot button" checkbox and start the camera. Then press the camera button to test the snapshot.

Android Webview fullscreen video working inconsistently

Mostly my android webview works fine when fullscreening videos but there are some videos where it encounters problems, some I've already fixed, some I can't
1) Fullscreen button doesn't always trigger onShowCustomView. FIXED: Updating Chrome from the Google app store fixes this bug
2) Video goes fullscreen and you can hear the audio but sometimes the whole screen is black, you cannot even see the video player UI. Example: https://www.engadget.com/2019/05/22/vader-immortal-review-oculus-quest-vr/
I have a hack fix for this at the moment by resizing the view after a 2s delay but I'd obviously prefer a more robust solution
3) Video goes fullscreen temporarily but then closes itself immediately. This only happens occasionally but I've seen it happen when opening vimeo videos from twitter. It's not consistent so you'll have to click through a few https://mobile.twitter.com/search?q=Vimeo&src=typed_query
Any idea how I can make this work more consistently?
My custom web chrome client:
public void onShowCustomView(View paramView, CustomViewCallback paramCustomViewCallback)
{
if (this.mCustomView != null)
{
onHideCustomView(); // I verified this wasn't the cause of point 3
return;
}
this.mCustomView = paramView;
this.mOriginalSystemUiVisibility = browserUi.getBrowser().getWindow().getDecorView().getSystemUiVisibility();
previousOrientation = browserUi.getBrowser().getRequestedOrientation();
this.mCustomViewCallback = paramCustomViewCallback;
FrameLayout decorView;
if(browserUi.isPreviewWindow()){
decorView = browserUi.getBrowser().findViewById(R.id.prev_window_fullscreen_video_container);
}else {
decorView = browserUi.getBrowser().findViewById(R.id.fullscreen_video_container);
}
decorView.setVisibility(View.VISIBLE);
decorView.addView(this.mCustomView, new FrameLayout.LayoutParams(-1, -1));
if(!browserUi.isPreviewWindow()) {
this.mCustomView.setKeepScreenOn(true); // prevents sleep while viewing
int flags = SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_HIDE_NAVIGATION;
flags |= SYSTEM_UI_FLAG_FULLSCREEN;
flags |= SYSTEM_UI_FLAG_LAYOUT_STABLE;
flags |= SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
flags |= SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
flags |= SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
decorView.setSystemUiVisibility(flags);
int screenOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
browserUi.getBrowser().setRequestedOrientation(screenOrientation);
}
}

Android Random Alarm Tone

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.

Two Object of media Player not play song in Sync at same time

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.

Force audio routing to speaker (and optionally route to BT if user preferences) and setStreamType ignored by Lollipop

My goal is the following, I want to give the user the following options in preferences:
Play sounds: "Always on speaker" or "Use Bluetooth when available"
Use stream: STREAM_MUSIC, STREAM_ALARM, STREAM_NOTIFICATION or STREAM_RING
My first problem is that when a user has a (Lollipop) device connected to an a2dp enabled device (car stereo), I can't seem to get the sound to get played through the device speaker. (The sound also isn't being played through the a2dp device)
The second problem I encounter is that setStreamType somehow stopped working since Lollipop? On Lollipop setStreamType is ignored and sound is always played through AudioManager.STREAM_MUSIC? (On device pre-Lollipop the same code still works)
private void playSound() {
try {
if (am == null) am = (AudioManager) _context.getSystemService(Context.AUDIO_SERVICE);
if (am.isWiredHeadsetOn() || am.isBluetoothA2dpOn()){
am.setWiredHeadsetOn(false);
am.setBluetoothScoOn(false);
// am.setMode(AudioManager.MODE_IN_CALL); // bel speaker
// am.setSpeakerphoneOn(false); // bel speaker
am.setMode(AudioManager.MODE_IN_CALL); // media speaker
am.setSpeakerphoneOn(true); // media speaker
} else {
// am.setMode(AudioManager.MODE_IN_CALL); // bel speaker
// am.setSpeakerphoneOn(false); // bel speaker
am.setMode(AudioManager.MODE_NORMAL); // media speaker
am.setSpeakerphoneOn(true); // media speaker
}
Class audioSystemClass = Class.forName("android.media.AudioSystem");
Method setForceUse = audioSystemClass.getMethod("setForceUse",
int.class,
int.class);
// 1 == FOR_MEDIA, 10 == FORCE_NO_BT_A2DP (FORCE_SPEAKER would be 1).
setForceUse.invoke(null, 1, 1);
alarmsound = _prefs.getString("alarm", alarmsound);
as = Uri.parse(alarmsound);
if (mp == null) mp = MediaPlayer.create(_context, as);
mp.reset();
if (alarmsound.startsWith("content://settings/system/"))
mp.setDataSource(_context, Uri.parse("android.resource://nl.xxxxxxxx.xxxxxxxx/raw/beep"));
else setMediaPlayerDataSource(_context, mp, as.toString());
mp.setAudioStreamType(Globals.AudioStreamType);
mp.prepare();
long alarmDuration = mp.getDuration();
mp.start();
}
From the Globals class:
public static int AudioStreamType = AudioManager.STREAM_MUSIC; (this can be changed by the user to the other stream types)
I declared uses-permission android.permission.MODIFY_AUDIO_SETTINGS in the Manifest.
Hopefully someone can point me in the right direction.

Categories

Resources