The first experience is called exploding head syndrome. The second experience is called sleep paralysis.

These experiences can seem scary but they're harmless. The best thing you can do to avoid these experiences is to lower your level of stress because stress is what often causes them. Just relax knowing that these experiences are harmless and nothing to worry about.