Debug console makes an unwanted sound when displaying certain characters

omgMajk

Member
Joined
Mar 9, 2021
Messages
5
Programming Experience
Beginner
So I have this program that takes the title of the Spotify window and prints it to the console. The code is mostly stolen from Stack Overflow but the problem I'm having is that when certain characters are played, the Console.WriteLine() makes a windows system sound that I'm trying to get rid of.

Spotify Title Program:
using System;
using System.Diagnostics;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;

namespace SpotifyTest
{
    class Program
    {
        public string GetSpotifyTrackInfo()
        {
            var proc = Process.GetProcessesByName("Spotify").FirstOrDefault(p => !string.IsNullOrWhiteSpace(p.MainWindowTitle));

            if (proc == null)
            {
                return "Spotify is not running!";
            }

            if (string.Equals(proc.MainWindowTitle, "Spotify Premium", StringComparison.InvariantCultureIgnoreCase) || string.Equals(proc.MainWindowTitle, "Spotify", StringComparison.InvariantCultureIgnoreCase))
            {
                return "Paused";
            }
            return proc.MainWindowTitle;
        }

        static void Main(string[] args)
        {
            Program x = new Program();

            while (true)
            {
                string songName = x.GetSpotifyTrackInfo();
                //songName = Regex.Replace(songName, "[^A-Za-z0-9 -]", ""); //Not good since doesn't display swedish
                Thread.Sleep(2000);
                Console.Clear();
                Console.WriteLine(songName);
            }
        }
    }
}

Now, whenever I play certain songs by a band that is not properly formated on Spotify, the console makes this noise. But I still want to display most things like swedish and danish characters. Song name in question in image below. Any solution to this?

2021-03-09 13_44_31-Livsnekad - Ädesdigrande Slentrian.png


2021-03-09 13_45_47-C__Users_user_source_repos_SpotifyTest_SpotifyTest_bin_Debug_netcoreapp3.1...png
 
Solution
I believe it's correct formatting, at least how Spotify reads it, both desktop client and web client display this
1615316001929.png
and is also what MainWindowTitle returns.
It can be seen in screenshot 1 of post 1, but a bit blurry. The second screenshot in post 1 in from OP's console output that is likely has Windows codepage 1252 and won't display that char, but it will emit a beep sound twice.

A better solution than to remove that char is to change Console.OutputEncoding to Encoding.Unicode for the titles to display like they're supposed to. If you do this it won't break the encoding and "beep".

JohnH

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
1,197
Location
Norway
Programming Experience
10+
What does the MainWindowTitle return? Can't you just remove "Spotify" or "Spotify Premium"?
 

omgMajk

Member
Joined
Mar 9, 2021
Messages
5
Programming Experience
Beginner
What does the MainWindowTitle return? Can't you just remove "Spotify" or "Spotify Premium"?
When spotify is paused it has the title "Spotify" or "Spotify Premium" depending on which one you have. When it's not paused/when it's playing it returns "Band - Song".
 

omgMajk

Member
Joined
Mar 9, 2021
Messages
5
Programming Experience
Beginner
I suspect that this has to do with the check-mark sign in the song title but I am not sure. Basically looking for a way to turn off the bell sound windows makes when it prints the name of one of those songs that has such characters - or strip the song titles only of special chars like them.
 

JohnH

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
1,197
Location
Norway
Programming Experience
10+
I see, I thought you were trying to parse the string. Actually this is the string I get from that track:
Br√•ddjupets Sp√∂Rsm√•l · Livsnekad
The "•" is the bell character. Try to replace it:
C#:
songName = songName.Replace("•", string.Empty);
 

omgMajk

Member
Joined
Mar 9, 2021
Messages
5
Programming Experience
Beginner
I see, I thought you were trying to parse the string. Actually this is the string I get from that track:

The "•" is the bell character. Try to replace it:
C#:
songName = songName.Replace("•", string.Empty);
Fantastic, thank you for the help!
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,386
Location
Chesapeake, VA
Programming Experience
10+
You're code for pulling the Spotify track information is giving your incorrect data. C# strings support Unicode. It looks like the strings you are getting back are either UTF8 strings, or strings containing only bytes 0-255 in a particular code page.
 

JohnH

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
1,197
Location
Norway
Programming Experience
10+
I believe it's correct formatting, at least how Spotify reads it, both desktop client and web client display this
1615316001929.png
and is also what MainWindowTitle returns.
It can be seen in screenshot 1 of post 1, but a bit blurry. The second screenshot in post 1 in from OP's console output that is likely has Windows codepage 1252 and won't display that char, but it will emit a beep sound twice.

A better solution than to remove that char is to change Console.OutputEncoding to Encoding.Unicode for the titles to display like they're supposed to. If you do this it won't break the encoding and "beep".
 
Solution

omgMajk

Member
Joined
Mar 9, 2021
Messages
5
Programming Experience
Beginner
I believe it's correct formatting, at least how Spotify reads it, both desktop client and web client display this View attachment 1433 and is also what MainWindowTitle returns.
It can be seen in screenshot 1 of post 1, but a bit blurry. The second screenshot in post 1 in from OP's console output that is likely has Windows codepage 1252 and won't display that char, but it will emit a beep sound twice.

A better solution than to remove that char is to change Console.OutputEncoding to Encoding.Unicode for the titles to display like they're supposed to. If you do this it won't break the encoding and "beep".
That seems like a better idea. Thanks again. And thanks for the explanation.
 
Top Bottom