Yes, of course there are rules.
When ב is in front of an indefinite noun, it will be pronounced "be" and in front of a definite noun "ba". However, in formal speech, it can also be pronounced "bi" if it is in front of an indefinite noun that has a shva under its first letter, or starts with a yod. This rule is, I think, almost entirely disregarded in modern colloquial speech.
As for מ, when it is followed by guttural letters (א, ה, ח, ע) or ר then it should be pronounced as "me", and in all other instances, it should be pronounced "mi". However, nowadays I'd say it's mostly pronounced "me" everywhere, unless it's a set phrase or something like that.