Ronak99 / Skype-Clone

Making a fully functional skype clone in flutter.
MIT License
318 stars 172 forks source link

Question: How to add ringtone to incoming call screen #32

Open alexlovar opened 4 years ago

alexlovar commented 4 years ago

hello, the app is working only video call, the incoming call screen is showed when a video call is receive but there is not sound, how to add a ring tone to incoming call screen?

MaheshPeri19 commented 4 years ago

I used below package Add this package in yaml file : flutter_ringtone_player: ^2.0.0

And added below code.

@override void initState() { super.initState(); // Start ringtone. FlutterRingtonePlayer.playRingtone(); }

Call stop ringtone method in call accept button & call end button and also dispose method. // Stop Ringtone FlutterRingtonePlayer.stop();

Mafazkhan commented 3 years ago

I used below package Add this package in yaml file : flutter_ringtone_player: ^2.0.0

And added below code.

@override void initState() { super.initState(); // Start ringtone. FlutterRingtonePlayer.playRingtone(); }

Call stop ringtone method in call accept button & call end button and also dispose method. // Stop Ringtone FlutterRingtonePlayer.stop();

where do I add this code in which file or tell me where u added this and did it work for u?

MaheshPeri19 commented 3 years ago

Yes, it worked for me. I have added this in incoming call screen (pickup screen) where accept reject button appear.

Example : User A make the call to User B. Then User B gets the incoming call with accept/reject buttons. When ever User B gets incoming call, i am calling start method of ringtone. When he clicks accept or reject button, i am stopping the ringtone by calling stop method.

My Scenario :

I am using this sample project, so that in PickupScreen initstate method, i am calling FlutterRingtonePlayer.playRingtone(); Then when user accept or reject, i am calling FlutterRingtonePlayer.stop();

PickupScreen class will execute whenever current user gets incoming call. So it works.

Note : this ringtone code is required only when the app is in foreground or running state. For background or closed or killed state, this is not required as must use callkeep package or callkit package.

Mafazkhan commented 3 years ago

Hiii can you please give me a hint code I dont get you Im confused

On Wed, Nov 18, 2020, 22:33 MaheshPeri19 notifications@github.com wrote:

Yes, it worked for me. I have added this in incoming call screen (pickup screen) where accept reject button appear.

Example : User A make the call to User B. Then User B gets the incoming call with accept/reject buttons. When ever User B gets incoming call, i am calling start method of ringtone. When he clicks accept or reject button, i am stopping the ringtone by calling stop method.

My Scenario :

I am using this sample project, so that in PickupScreen initstate method, i am calling FlutterRingtonePlayer.playRingtone(); Then when user accept or reject, i am calling FlutterRingtonePlayer.stop();

PickupScreen class will execute whenever current user gets incoming call. So it works.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/Ronak99/Skype-Clone/issues/32#issuecomment-729816421, or unsubscribe https://github.com/notifications/unsubscribe-auth/APLFFLUXJJLA7WV37P5QSODSQP46ZANCNFSM4QPK7JTQ .

Mafazkhan commented 3 years ago

Can u please elaborate a little more

On Wed, Nov 18, 2020, 22:33 MaheshPeri19 notifications@github.com wrote:

Yes, it worked for me. I have added this in incoming call screen (pickup screen) where accept reject button appear.

Example : User A make the call to User B. Then User B gets the incoming call with accept/reject buttons. When ever User B gets incoming call, i am calling start method of ringtone. When he clicks accept or reject button, i am stopping the ringtone by calling stop method.

My Scenario :

I am using this sample project, so that in PickupScreen initstate method, i am calling FlutterRingtonePlayer.playRingtone(); Then when user accept or reject, i am calling FlutterRingtonePlayer.stop();

PickupScreen class will execute whenever current user gets incoming call. So it works.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/Ronak99/Skype-Clone/issues/32#issuecomment-729816421, or unsubscribe https://github.com/notifications/unsubscribe-auth/APLFFLUXJJLA7WV37P5QSODSQP46ZANCNFSM4QPK7JTQ .

MaheshPeri19 commented 3 years ago

The same code which exists in sample. I have added ringtone start and stop methods. If this sample project (Ronak99 /Skype-Clone) is working for you, nothing new to change. I have added only ringtone methods in between. First try to run this sample project in two devices or two simulators. Then you will get it.

class PickupScreen extends StatefulWidget {
  final Call call;

  PickupScreen({
    @required this.call,
  });

  @override
  _PickupScreenState createState() => _PickupScreenState();
}

class _PickupScreenState extends State<PickupScreen> {
  final CallMethods callMethods = CallMethods();

  bool isCallMissed = true;

  addToLocalStorage({@required String callStatus}) {
    Log log = Log(
      callerName: widget.call.callerName,
      callerPic: widget.call.callerPic,
      receiverName: widget.call.receiverName,
      receiverPic: widget.call.receiverPic,
      timestamp: DateTime.now().toString(),
      callStatus: callStatus,
    );

    LogRepository.addLogs(log);
  }

  @override
  void dispose() {
    if (isCallMissed) {
      addToLocalStorage(callStatus: kFirebaseMissedCallKey);
      print("======= dispose/pickupscreen ========= ");
      // Stop Ringtone
      FlutterRingtonePlayer.stop();
    }
    super.dispose();
  }

  @override
  void initState() {
    super.initState();
    // Start ringtone.
    FlutterRingtonePlayer.playRingtone();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: kWhiteColor,
      body: Container(
        alignment: Alignment.center,
        padding: EdgeInsets.symmetric(vertical: 100),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              "Incoming Call...",
              style: TextStyle(
                fontSize: 30,
              ),
            ),
            SizedBox(height: 50),
            CachedImage(
              widget.call.callerPic,
              isRound: true,
              radius: 180,
            ),
            SizedBox(height: 15),
            Text(
              widget.call.callerName,
              style: TextStyle(
                fontWeight: FontWeight.bold,
                fontSize: 20,
              ),
            ),
            SizedBox(height: 75),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                IconButton(
                  icon: Icon(
                    Icons.call_end,
                    size: 30,
                  ),
                  color: kPrimaryColor,
                  onPressed: () async {
                    // stop ringtone.
                    FlutterRingtonePlayer.stop();

                    isCallMissed = false;
                    addToLocalStorage(
                        callStatus: kFirebaseReceivedCallKey);
                    await callMethods.endCall(call: widget.call);
                  },
                ),
                SizedBox(width: 25),
                IconButton(
                    icon: Icon(
                      Icons.call,
                      size: 30,
                    ),
                    color: kGreenColor,
                    onPressed: () async {
                      // stop ringtone.
                      FlutterRingtonePlayer.stop();
                      isCallMissed = false;
                      addToLocalStorage(
                          callStatus: kFirebaseReceivedCallKey);
                      await Permissions.cameraAndMicrophonePermissionsGranted()
                          ? Navigator.push(
                              context,
                              MaterialPageRoute(
                                builder: (context) =>
                                    CallScreen(call: widget.call),
                              ),
                            )
                          // ignore: unnecessary_statements
                          : {};
                    }),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
hasnainsaeed9692 commented 3 years ago

@MaheshPeri19 Did you implement the calling screen when the app is closed or fone is locked? I badly need to implement this. Please help or share some code if you have implement it. Thanks

MaheshPeri19 commented 3 years ago

No. At the time of implementing this skype clone, it is working in only app is in active state, not in background or locked state.

If you are looking for custom incoming call like whatsapp and skype for Android only, then look into below link

https://github.com/rafaelsetragni/awesome_notifications/issues/35#issuecomment-751431492

hasnainsaeed9692 commented 3 years ago

@MaheshPeri19 Can you share your project if it's possible?

MaheshPeri19 commented 3 years ago

Sorry. I can't share.