Hello Shaheenneedhelp,
Thanks for asking the Apple Support Communities for help with two-factor and signing in to your Apple Music on your Pixel 2. I am happy to check this out with you.
First, this article talks about two-factor authentication, and getting the codes:
Two-factor authentication for Apple ID - Apple Support
A verification code is a temporary code sent to your trusted device or phone number when you sign in to a new device or browser with your Apple ID. You can also get a verification code from Settings on your trusted device.
A verification code is different from the device passcode you enter to unlock your iPhone, iPad, or iPod touch.
A trusted device is an iPhone, iPad, or iPod touch with iOS 9 and later, or a Mac with OS X El Capitan and later that you've already signed in to using two-factor authentication. It’s a device we know is yours and that can be used to verify your identity by displaying a verification code from Apple when you sign in on a different device or browser.
If you're signing in and don’t have a trusted device handy that can display verification codes, you can have a code sent to your trusted phone number via text message or an automated phone call instead. Click Didn't Get a Code on the sign in screen and choose to send a code to your trusted phone number. You can also get a code directly from Settings on a trusted device. Learn how to get a verification code.
If you use iOS 11.3 or later on your iPhone, you might not need to enter a verification code. In some cases, your trusted phone number can be automatically verified in the background on your iPhone. It’s one less thing to do, and your account is still protected with two-factor authentication.
If possible with your device, you can try creating an app-specific password to log in to Apple Music also. This article will help:
Using app-specific passwords - Apple Support
Thanks and have a wonderful day.