Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Play disconnect sound android #113

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ proguard/
# Android Studio captures folder
captures/

#Android generated
android/.classpath
android/.project
android/.settings/org.eclipse.buildship.core.prefs

# Intellij
*.iml
.idea/workspace.xml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,32 @@
import android.content.Context;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.media.SoundPool;
import android.media.AudioManager;
import android.os.Build;
import android.net.Uri;

import static android.content.Context.AUDIO_SERVICE;

public class SoundPoolManager {

private boolean playing = false;
private static SoundPoolManager instance;
private Ringtone ringtone = null;
private SoundPool soundPool;
private int disconnectSoundId;

private SoundPoolManager(Context context) {
Uri ringtoneSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
ringtone = RingtoneManager.getRingtone(context, ringtoneSound);
int maxStreams = 1;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
soundPool = new SoundPool.Builder().setMaxStreams(maxStreams).build();
} else {
soundPool = new SoundPool(maxStreams, AudioManager.STREAM_MUSIC, 0);
}
disconnectSoundId = soundPool.load(context, R.raw.disconnect, 1);
}

public static SoundPoolManager getInstance(Context context) {
Expand All @@ -38,10 +53,11 @@ public void stopRinging() {
}

public void playDisconnect() {
if (!playing) {
if (playing) {
ringtone.stop();
playing = false;
}
soundPool.play(disconnectSoundId, 1f, 1f, 1, 0, 1f);
playing = false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ public class TwilioVoiceModule extends ReactContextBaseJavaModule implements Act
private HeadsetManager headsetManager;
private EventManager eventManager;

private SoundPoolManager soundPoolManager;

public TwilioVoiceModule(ReactApplicationContext reactContext,
boolean shouldAskForMicPermission) {
super(reactContext);
Expand All @@ -134,6 +136,7 @@ public TwilioVoiceModule(ReactApplicationContext reactContext,
headsetManager = new HeadsetManager(eventManager);

notificationManager = (android.app.NotificationManager) reactContext.getSystemService(Context.NOTIFICATION_SERVICE);
soundPoolManager = SoundPoolManager.getInstance(reactContext);

/*
* Setup the broadcast receiver to be notified of GCM Token updates
Expand Down Expand Up @@ -391,7 +394,7 @@ private void handleIncomingCallIntent(Intent intent) {
if (BuildConfig.DEBUG) {
Log.d(TAG, "handleIncomingCallIntent state = PENDING");
}
SoundPoolManager.getInstance(getReactApplicationContext()).playRinging();
soundPoolManager.playRinging();

if (getReactApplicationContext().getCurrentActivity() != null) {
Window window = getReactApplicationContext().getCurrentActivity().getWindow();
Expand Down Expand Up @@ -422,7 +425,7 @@ private void handleIncomingCallIntent(Intent intent) {
// - the call is answered (activeCall != null)
// - the call is rejected

SoundPoolManager.getInstance(getReactApplicationContext()).stopRinging();
soundPoolManager.stopRinging();

// the call is not active yet
if (activeCall == null) {
Expand Down Expand Up @@ -552,7 +555,7 @@ private void registerForCallInvites() {
@ReactMethod
public void accept() {
callAccepted = true;
SoundPoolManager.getInstance(getReactApplicationContext()).stopRinging();
soundPoolManager.stopRinging();
if (activeCallInvite != null){
if (activeCallInvite.getState() == CallInvite.State.PENDING) {
if (BuildConfig.DEBUG) {
Expand Down Expand Up @@ -582,7 +585,7 @@ public void accept() {
@ReactMethod
public void reject() {
callAccepted = false;
SoundPoolManager.getInstance(getReactApplicationContext()).stopRinging();
soundPoolManager.stopRinging();
WritableMap params = Arguments.createMap();
if (activeCallInvite != null){
params.putString("call_sid", activeCallInvite.getCallSid());
Expand All @@ -598,7 +601,7 @@ public void reject() {
@ReactMethod
public void ignore() {
callAccepted = false;
SoundPoolManager.getInstance(getReactApplicationContext()).stopRinging();
soundPoolManager.stopRinging();
WritableMap params = Arguments.createMap();
if (activeCallInvite != null){
params.putString("call_sid", activeCallInvite.getCallSid());
Expand Down Expand Up @@ -667,6 +670,7 @@ public void connect(ReadableMap params) {
@ReactMethod
public void disconnect() {
if (activeCall != null) {
soundPoolManager.playDisconnect();
activeCall.disconnect();
activeCall = null;
}
Expand Down
Binary file added android/src/main/res/raw/disconnect.wav
Binary file not shown.