Sound card switching method and system, electronic device, and storage medium
By using drivers, shell scripts, and the pulseaudio server in electronic devices running the Linux operating system to detect the insertion or removal of sound card output interfaces and prevent switching of sound card devices when there is audio output, the problem of sound interruption caused by automatic switching is solved, and audio continuity and user experience are improved.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- XIAN XINTONG SEMICON TECH CO LTD
- Filing Date
- 2025-12-19
- Publication Date
- 2026-04-17
AI Technical Summary
The existing automatic switching strategy for sound cards may cause sound interruptions, especially in electronic devices running Linux.
The system determines whether there is audio output when an output interface is plugged in or unplugged. If there is output, it prevents switching to the new sound card device, or selects the original sound card device for audio output if there is no output. The sound card switching is performed using drivers, shell scripts, and the pulseaudio server.
It avoids interruptions to audio playback due to hot-plugging operations, ensuring audio continuity and user experience, and has significant advantages, especially in professional application scenarios that require uninterrupted audio feedback.
Smart Images

Figure CN121349402B_ABST
Abstract
Description
Technical Field
[0001] This disclosure relates to the field of computer technology, and in particular to a sound card switching method, system, electronic device, and storage medium. Background Technology
[0002] In the field of computer technology, especially in electronic devices running the Linux operating system, audio output management is a common technical requirement. These systems typically employ the Advanced Linux Sound Architecture (ALSA) to manage multiple sound card devices. To improve user convenience, existing audio servers often include hot-plug detection functionality. For example, many Linux distributions load the module-switch-on-port-available module by default. This module's function is to automatically switch the default audio output to the newly inserted sound card device once a new sound card device output interface is detected. This automatic switching strategy can lead to audio interruption issues. Summary of the Invention
[0003] This disclosure provides a sound card switching method, system, electronic device, and storage medium; it can solve the problem that automatic switching strategies in the prior art may cause sound interruption.
[0004] The technical solution disclosed herein is implemented as follows:
[0005] Firstly, this disclosure provides a sound card switching method, the method comprising:
[0006] When an insertion event is detected at the current output interface, if the system is already outputting audio before the insertion event occurs, the audio output will be prevented from being switched to the current sound card device corresponding to the current output interface.
[0007] When a disconnection event is detected at the current output interface, if the system was already outputting audio before the disconnection event occurred, and the current sound card device corresponding to the current output interface is not a sound card device that is already outputting audio, then switching the audio output to the current sound card device corresponding to the current output interface will be prevented.
[0008] Secondly, this disclosure provides a sound card switching system, which includes:
[0009] The insertion execution module is used to prevent switching the audio output to the current sound card device corresponding to the current output interface when an insertion event is detected at the current output interface, if there is already audio output before the insertion event occurs.
[0010] The unplug execution module is used to prevent switching the audio output to the current sound card device corresponding to the current output interface when an unplug event is detected at the current output interface, if the system was already outputting audio before the unplug event occurred, and the current sound card device corresponding to the current output interface is not a sound card device that is already outputting audio.
[0011] Thirdly, this disclosure provides an electronic device, including a processor and a memory, and a computer program stored in the memory and executable on the processor, wherein the processor executes the program to implement the sound card switching method described in this disclosure.
[0012] Fourthly, this disclosure provides a computer-readable storage medium storing at least one instruction, which is executed by a processor to implement the sound card switching method described in this disclosure.
[0013] This disclosure provides a sound card switching method, system, electronic device, and storage medium. When an insertion event is detected at the current output interface, it first determines whether the system was already outputting audio before the insertion event. If audio is already outputting, the sound card device used before the insertion event is selected for audio output, actively preventing switching to the sound card device corresponding to the current output interface. When a removal event is detected at the current output interface, it first determines whether the current sound card device corresponding to the current output interface was outputting audio before the removal event is detected. If no audio is outputting, the sound card device used before the removal event is selected for audio output, preventing sound card device switching. This method avoids unwarranted interruptions to ongoing audio playback caused by hot-plugging operations, prioritizing audio continuity and user experience, and has significant advantages, especially in professional application scenarios requiring uninterrupted audio feedback. Attached Figure Description
[0014] Figure 1 This is a flowchart illustrating the sound card switching process using a driver, shell script, and pulseaudio server, as provided in this disclosure.
[0015] Figure 2 A flowchart of the sound card switching method provided in this disclosure.
[0016] Figure 3 This is a flowchart illustrating the state update process of the output interface when an insertion event occurs, as provided in this disclosure.
[0017] Figure 4 This is a flowchart illustrating the sound card switching process provided in this disclosure when an insertion event occurs.
[0018] Figure 5 This is a flowchart for preventing sound card switching when an insertion event occurs, as provided in this disclosure.
[0019] Figure 6 This is a flowchart of the sound card switching process provided in this disclosure when a disconnection event occurs.
[0020] Figure 7 This is a schematic diagram of the sound card switching system provided in this disclosure.
[0021] Figure 8 This is a schematic diagram of the structure of the electronic device provided in this disclosure. Detailed Implementation
[0022] The technical solutions in this disclosure will now be clearly and completely described with reference to the accompanying drawings.
[0023] Currently, most systems use the ALSA architecture-based jack plugin to switch sound card devices. The software implementation of this switching method is relatively simple. However, on some systems with older kernel versions, the ALSA architecture does not support the jack plugin very well. When switching sound cards, there may be instances where the switching command is issued, but the sound card is not actually switched successfully.
[0024] To address the aforementioned issues, this disclosure provides a flowchart for sound card switching using a driver, shell script, and pulseaudio server. For example... Figure 1 As shown, electronic devices with multiple sound cards will have multiple output interfaces, and sound card devices with the same device number may have multiple versions, such as single-output interface and multi-output interface versions. This embodiment takes a hot-swappable sound card with an HDMI / DP port (i.e., an output interface) as an example. The driver is used to maintain the connection status of the output interface and the audio support status of the backend playback devices connected to the output interface. Specifically, it includes:
[0025] Step S101: Monitor hot-plug events using a shell script;
[0026] Step S102: Maintain the output interface connection status and playback device audio support status through the driver.
[0027] Step S103: After the shell script detects the hot-plug event, it executes script logic processing based on the output interface connection status and the audio support status of the playback device.
[0028] Step S104: During the script logic processing, the pulseaudio-related control commands in the pulseaudio server are called to complete the sound card switching.
[0029] The method described above, which combines drivers, pulseaudio server, and shell scripts to control the switching of sound card selection, can better support systems with different kernel versions. Regardless of the kernel version, this method can be used to switch between multiple sound card devices. This method avoids the problem of sound card switching failure caused by using the jack plugin on some systems.
[0030] Figure 2 This is a flowchart illustrating the sound card switching method provided in this disclosure. Figure 2 As shown, the specific steps include:
[0031] Step S201: Detect whether an insertion event or a removal event has occurred at the current output interface; if an insertion event occurs, proceed to step S202; if a removal event occurs, proceed to step S203.
[0032] Step S202: Determine whether the system is already outputting audio before the insertion event occurs; if so, proceed to step S205.
[0033] Step S203: Determine whether the system was already outputting audio before the unplugging event occurred; if so, proceed to step S204.
[0034] Step S204: Determine whether the current sound card device is a sound card device that is already outputting audio; if not, proceed to step S205.
[0035] Step S205: Prevent switching the audio output to the current sound card device corresponding to the current output interface.
[0036] In this embodiment of the disclosure, the driver running in the kernel of the electronic device (e.g., the Linux kernel) monitors the physical connection status associated with each output interface (e.g., HDMI / DP port) in real time. When a playback device (e.g., a monitor) is connected to an output interface via a cable (e.g., an HDMI / DP cable), the driver detects the "insertion event." When a cable is unplugged from an output interface, the driver detects the "unplugging event." At this time, the output interface where the insertion or unplugging event occurred is defined as the current output interface, and the current sound card device corresponding to the current output interface is defined as the current sound card device.
[0037] Regarding the case of an insertion event at the current output interface, if a sound card was already outputting audio before the HDMI / DP cable was plugged in, the shell script will still prioritize using the sound card that was outputting audio before the HDMI / DP cable was plugged in, preventing sound card switching. Therefore, it ensures that audio playback will not be interrupted or silent during the sound card switching time after the HDMI / DP cable is plugged in. Furthermore, all sound card devices that can be switched normally can be enumerated for manual selection by the user.
[0038] In cases where the current output interface is unplugged, if the system's audio output was not from the corresponding sound card device before the HDMI / DP cable of the current output interface was unplugged, the shell script will prioritize selecting the sound card device that was already outputting audio before the HDMI / DP cable of the current output interface was unplugged. This ensures that audio playback will not be interrupted or silent during the sound card device switching time after an HDMI / DP cable is unplugged. Furthermore, all sound card devices that can be switched normally can be enumerated for manual selection by the user.
[0039] In this embodiment, when an insertion event is detected at the current output interface, it is first determined whether the system was already outputting audio before the insertion event occurred. If audio was already outputting, the sound card device before the insertion event occurred is still selected for audio output, actively preventing switching to the sound card device corresponding to the current output interface. When a removal event is detected at the current output interface, it is first determined whether the current sound card device corresponding to the current output interface was outputting audio before the removal event occurred. If the current sound card device corresponding to the current output interface was not outputting audio, the sound card device before the removal event occurred is still selected for audio output, preventing sound card device switching. This method avoids unwarranted interruptions to ongoing audio playback caused by hot-plugging operations, prioritizing audio continuity and user experience, and has significant advantages, especially in professional application scenarios requiring uninterrupted audio feedback.
[0040] In an optional embodiment, before detecting whether an insertion or removal event has occurred at the current output interface, the method further includes:
[0041] Build an identifier file to store the sound card device identifier;
[0042] Build a status file to store the status information of the sound card device; the status information is used to indicate whether the playback device connected to the output interface supports audio playback.
[0043] In this embodiment, for electronic devices with multiple sound cards (e.g., multiple HDMI / DP ports), the driver registers multiple sound card devices. To easily distinguish between different sound card models, the driver pre-adds an identifier file (denoted as `board_type`) and a status file (denoted as `audio_edid`) to the system's ` / proc` directory. These files store the sound card device identifier and information such as whether the backend playback device supports audio playback. The significance of distinguishing different sound card models lies in the fact that different usage scenarios often result in different versions of the same sound card device. For example, a sound card device with the same device number may have multiple versions, such as one with a single HDMI / DP port and one with multiple HDMI / DP ports.
[0044] In this embodiment, the driver saves the identifiers (IDs) of different sound card devices to the board_type file in the proc directory when the driver is loaded. This allows the shell script to retrieve the corresponding sound card device identifier from the board_type file when switching sound card devices later, and then call the pulseaudio command to perform the corresponding sound card device switching operation.
[0045] In addition, the driver continuously monitors whether an HDMI / DP cable is plugged into an output port. When an HDMI / DP cable for connecting to a playback device is detected, the driver retrieves Extended Display Identification Data (EDID) from the playback device. This EDID data contains information about whether the playback device supports audio playback. The driver then parses the EDID data. If it determines that the playback device does not support audio, the system's sound card will not make any adjustments. If it determines that the playback device supports audio playback, the driver writes the information about the connected HDMI / DP port (for sound cards with multiple output ports, this identifies which port is connected) and its audio playback support status to the `audio_edid` file in the system's ` / proc` directory. Simultaneously, the driver updates the `audio_edid` file in the ` / proc` directory in real time based on the plugging and unplugging of different output ports to ensure that subsequent shell scripts obtain the real-time status information of the output ports when switching sound card devices. It should be noted here that when performing real-time maintenance on the audio_edid file in the system's proc directory, the basic logic for maintaining the status information is as follows: when an output interface is not properly connected to a playback device via an HDMI / DP cable, or when a playback device is connected but does not support audio playback, the status information of the corresponding output interface will be set to the second state, recorded as false. Only when the output interface is connected to a playback device via an HDMI / DP cable, and the playback device's EDID is resolved to show that the playback device supports audio playback, will the status information of the corresponding output interface be set to the first state, recorded as true.
[0046] In an optional embodiment, Figure 3 This is a flowchart illustrating the state update process of the output interface when an insertion event occurs, as provided in this disclosure. Figure 3 As shown, the method also includes:
[0047] Step S301: When an insertion event is detected at the current output interface, check whether the playback device corresponding to the insertion event supports audio playback;
[0048] If supported, proceed to step S302: update the status information of the current output interface recorded in the preset status file to the first status;
[0049] If not supported, proceed to step S303: update the status information of the current output interface recorded in the preset status file to the second status.
[0050] In this embodiment, when the HDMI / DP cable between the current output interface and the playback device is plugged in, if the playback device supports audio playback, the driver will first update the audio_edid file in the system proc directory, change the status information of the current output interface from the previous second state (false) to the first state (true), and the status information of other output interfaces will remain unchanged.
[0051] If the playback device does not support audio playback, the status information of the audio_edid file in the system's proc directory will not change.
[0052] In an optional embodiment, the method further includes:
[0053] When an insertion event is detected at the current output interface, if there was no audio output before the insertion event occurred and the current output interface is in the first state, then the audio output is switched to the current sound card device corresponding to the current output interface.
[0054] Figure 4 This is a flowchart illustrating the sound card switching process provided in this disclosure when an insertion event occurs. For example... Figure 4 As shown, the specific steps include:
[0055] Step S401: An insertion event is detected at the current output interface;
[0056] Step S402: Determine if the system was outputting audio before the insertion event occurred; if not, proceed to step S403.
[0057] Step S403: Determine whether the current output interface is in the first state; if so, proceed to step S404.
[0058] Step S404: Switch the audio output to the current sound card device corresponding to the current output interface.
[0059] In this embodiment, when the driver detects an insertion event at the current output interface, it first updates the audio_edid file in the system's proc directory. When the updated audio_edid file records the current output interface's status information as the first state, it indicates that the playback device connected to the current output interface supports audio playback. In this case, the shell script is executed. The shell script reads the current output interface's status information from the audio_edid file and temporarily saves it. Then, it iterates through the sound card devices in the board_type file using the temporarily saved status information to find the corresponding current sound card device. Finally, it calls the pulseaudio command to switch the system's audio output to the current sound card device.
[0060] In an optional embodiment, the method further includes:
[0061] When an insertion event is detected at the current output interface, if there is no audio output before the insertion event occurs and the current output interface is in the second state, then switching the audio output to the current sound card device corresponding to the current output interface will be prevented.
[0062] Figure 5 This is a flowchart illustrating how to prevent sound card switching when an insertion event occurs, as provided in this disclosure. Figure 5 As shown, the specific steps include:
[0063] Step S501: An insertion event is detected at the current output interface;
[0064] Step S502: Determine whether the system is outputting audio before the insertion event occurs; if not, proceed to step S503.
[0065] Step S503: Determine whether the current output interface is in the second state; if so, proceed to step S504.
[0066] Step S504: Prevent switching the audio output to the current sound card device corresponding to the current output interface.
[0067] In this embodiment, when an insertion event occurs at the current output interface, the driver detects that the playback device connected to the current output interface does not support audio playback. Since the status information of the audio_edid file in the system's proc directory remains unchanged, it is in the second state (false) before and after the insertion. At this time, the shell script will not be executed, and the sound card device for system audio output will not change. In other words, when a playback device that does not support audio playback is connected, the sound card device corresponding to this output interface will not be included in the list of devices that can be switched normally by the shell script, thus avoiding the problem of switching the audio output to a device that does not support audio playback.
[0068] In an optional embodiment, the method further includes:
[0069] When a disconnection event is detected at the current output interface, the status information of the current output interface recorded in the preset status file is updated to the second status.
[0070] In this embodiment, when the HDMI / DP cable connecting a certain output interface to the backend playback device is disconnected, if the playback device originally connected to that output interface supports audio playback, the driver will first update the audio_edid file in the system proc directory, change the corresponding output interface from true to false, and keep the status information of other output interfaces unchanged.
[0071] If the playback device originally connected to the output interface does not support audio playback, the status information of the audio_edid file in the system's proc directory will not change when the HDMI / DP cable between the output interface and the playback device is disconnected.
[0072] In an optional embodiment, the method further includes:
[0073] When a disconnection event is detected at the current output interface, if the system was already outputting audio before the disconnection event occurred, and the current sound card device corresponding to the current output interface is a sound card device that is already outputting audio, then the audio output will be switched to the sound card device whose output interface is in the first state.
[0074] Figure 6 This is a flowchart illustrating the sound card switching process provided in this disclosure when a disconnection event occurs. For example... Figure 6 As shown, the specific steps include:
[0075] Step S601: A disconnection event is detected at the current output interface;
[0076] Step S602: Determine whether the system was already outputting audio before the unplugging event occurred; if so, proceed to step S603.
[0077] Step S603: Determine whether the current sound card device is a sound card device that is already outputting audio; if so, proceed to step S604.
[0078] Step S604: Switch the audio output to the sound card device with the output interface in the first state.
[0079] In this embodiment, before a disconnection event occurs at the current output interface, if the current sound card device corresponding to the current output interface is a sound card device that performs audio output, it means that the playback device connected to the current output interface supports audio playback before the disconnection event. Therefore, the status information of the current output interface in the audio_edid file is true before the disconnection event occurs. When a disconnection event occurs, the driver first updates the audio_edid file, changing the status information of the current output interface from true to false. Then, it executes a shell script to switch the audio output to the sound card device whose output interface is in the first state.
[0080] Optionally, the audio output can be switched to the sound card device with the output interface in the first state, including:
[0081] Execute a shell script to read and temporarily save the status information of all output interfaces in the preset status file (audio_edid);
[0082] It iterates through all sound card devices in the preset identifier file (board_type) based on the temporarily saved state information; the preset identifier file records the identifiers of all sound card devices;
[0083] Use the pulseaudio command to switch the audio output to the sound card device with the output interface in the first position. At the same time, the shell script will call the pulseaudio related commands to shut down the sound card device with the HDMI / DP port that has been unplugged.
[0084] When the shell script reads the status information of all output interfaces in the audio_edid file under the system's proc directory, there may be a situation where multiple sound card devices' output interfaces are all in the first state simultaneously. Therefore, during the traversal, multiple sound card devices that can be switched normally may be obtained. To address this situation, this embodiment can determine the first sound card device that can be switched normally as the sound card device that outputs audio after the unplug event occurs. At the same time, all sound card devices in the list of devices that can be switched normally are enumerated so that the user can manually select the corresponding sound card device for audio output.
[0085] In an optional embodiment, when a disconnection event is detected at the current output interface, if the current output interface was in the second state before the disconnection event occurred, it indicates that the playback device connected to the current output interface before the disconnection event did not support audio playback. Therefore, the status information of the audio_edid file in the system's proc directory will not change after the disconnection event occurs, and thus the sound card switching function in the shell script will not be executed, and the sound card device used for audio output by the system will not change.
[0086] Optionally, if no audio was being output before the unplug event was detected when the current output interface is unplugged, then there is no need to switch the sound card.
[0087] The method in this embodiment uses a combination of drivers, a pulseaudio server, and shell scripts to control the sound card selection switching method. This approach offers better compatibility with systems across different kernel versions, avoiding the sound card switching failure issue that can occur when using the jack plugin on certain systems. It also automatically filters out devices that do not support audio playback based on the playback devices connected to the sound card's output interface, preventing the system sound card from being switched to an unsupported output interface for various reasons, thus avoiding the illusion of a silent system. Furthermore, it solves the problem of sound interruption caused by plugging and unplugging the sound card's output interface during audio playback, ensuring the continuity of the entire sound.
[0088] Based on the same principle as the method provided in the embodiments of this application, the embodiments of this application also provide a sound card switching system, such as... Figure 7 As shown, the system includes:
[0089] The insertion execution module 701 is used to prevent switching the audio output to the current sound card device corresponding to the current output interface when an insertion event is detected at the current output interface, if the system already has audio output before the insertion event occurs.
[0090] The unplug execution module 702 is used to prevent switching the audio output to the current sound card device corresponding to the current output interface when an unplug event is detected at the current output interface, if the system was already outputting audio before the unplug event occurred, and the current sound card device corresponding to the current output interface is not a sound card device that is already outputting audio.
[0091] In this embodiment, when an insertion event is detected at the current output interface, it is first determined whether the system was already outputting audio before the insertion event occurred. If audio was already outputting, the sound card device before the insertion event occurred is still selected for audio output, actively preventing switching to the sound card device corresponding to the current output interface. When a removal event is detected at the current output interface, it is first determined whether the current sound card device corresponding to the current output interface was outputting audio before the removal event occurred. If the current sound card device corresponding to the current output interface was not outputting audio, the sound card device before the removal event occurred is still selected for audio output, preventing sound card device switching. This method avoids unwarranted interruptions to ongoing audio playback caused by hot-plugging operations, prioritizing audio continuity and user experience, and has significant advantages, especially in professional application scenarios requiring uninterrupted audio feedback.
[0092] The sound card switching system provided in this embodiment can achieve... Figure 1-6 The various processes implemented in the method embodiments shown will not be described again here to avoid repetition.
[0093] Figure 8 A schematic diagram of the structure of an electronic device provided in an exemplary embodiment of this disclosure is shown. (Refer to...) Figure 8 In some examples, electronic device 80 can be at least one of the following: smartphone, smartwatch, desktop computer, laptop, virtual reality terminal, augmented reality terminal, wireless terminal, and laptop computer. Electronic device 80 has communication capabilities and can access wired or wireless networks. Electronic device 80 can refer to one of multiple terminals; those skilled in the art will understand that the number of such terminals can be more or less. It is understood that electronic device 80 undertakes the computation and processing work of the technical solution of this disclosure, and this disclosure does not limit this aspect.
[0094] like Figure 8 As shown, the electronic device in this disclosure may include one or more of the following components: processor 810 and memory 820.
[0095] Optionally, the processor 810 connects various parts within the electronic device using various interfaces and lines, and performs various functions and processes data by running or executing instructions, programs, code sets, or instruction sets stored in the memory 820, and by calling data stored in the memory 820. Optionally, the processor 810 can be implemented using at least one hardware form of Digital Signal Processing (DSP), Field-Programmable Gate Array (FPGA), or Programmable Logic Array (PLA). The processor 810 can integrate one or a combination of several of the following: Central Processing Unit (CPU), Graphics Processing Unit (GPU), Neural-network Processing Unit (NPU), and baseband chip. Specifically, the CPU primarily handles the operating system, user interface, and applications; the GPU is responsible for rendering and drawing the content required to be displayed on the touch screen; the NPU is used to implement Artificial Intelligence (AI) functions; and the baseband chip is used to handle wireless communication. It is understandable that the aforementioned baseband chip may not be integrated into the processor 810, but may be implemented using a separate chip.
[0096] The memory 820 may include random access memory (RAM) or read-only memory (ROM). Optionally, the memory 820 may include a non-transitory computer-readable storage medium. The memory 820 may be used to store instructions, programs, code, code sets, or instruction sets. The memory 820 may include a program storage area and a data storage area, wherein the program storage area may store instructions for implementing an operating system, instructions for at least one function (such as touch function, sound playback function, image playback function, etc.), instructions for implementing the various method embodiments described above, etc.; the data storage area may store data created based on the use of the electronic device, etc.
[0097] In addition, those skilled in the art will understand that the structure of the electronic device shown in the above figures does not constitute a limitation on the electronic device. The electronic device may include more or fewer components than shown, or combine certain components, or have different component arrangements. For example, the electronic device may also include a display screen, camera assembly, microphone, speaker, radio frequency circuit, input unit, sensors (such as accelerometer, angular velocity sensor, fiber optic sensor, etc.), audio circuit, WiFi module, power supply, Bluetooth module, etc., which will not be described in detail here.
[0098] This disclosure also provides a computer-readable storage medium storing at least one instruction that is executed by a processor to implement the sound card switching method described in the above embodiments.
[0099] Those skilled in the art will recognize that the functions described in this disclosure in one or more of the examples above can be implemented using hardware, software, firmware, or any combination thereof. When implemented in software, these functions can be stored in a computer-readable medium or transmitted as one or more instructions or code on a computer-readable medium. Computer-readable media include computer storage media and communication media, wherein communication media include any medium that facilitates the transfer of a computer program from one place to another. Storage media can be any available medium accessible to a general-purpose or special-purpose computer.
[0100] It should be noted that the technical solutions described in this disclosure can be combined arbitrarily as long as they do not conflict.
[0101] The above description is merely a specific embodiment of this disclosure, but the scope of protection of this disclosure is not limited thereto. Any changes or substitutions that can be easily conceived by those skilled in the art within the scope of the technology disclosed in this disclosure should be included within the scope of protection of this disclosure.
Claims
1. A sound card switching method, characterized in that, The method includes: When an insertion event is detected at the current output interface, if the system was already outputting audio before the insertion event occurred, the audio output will be prevented from being switched to the current sound card device corresponding to the current output interface. When a disconnection event is detected at the current output interface, if the system was already outputting audio before the disconnection event occurred, and the current sound card device corresponding to the current output interface is not the sound card device that is already outputting audio, then the audio output is prevented from being switched to the current sound card device corresponding to the current output interface. When the insertion event is detected at the current output interface, it is checked whether the playback device corresponding to the insertion event supports audio playback; If supported, the status information of the current output interface recorded in the preset status file is updated to the first status; if not supported, the status information of the current output interface recorded in the preset status file is updated to the second status; wherein, the first status indicates that the output interface is connected to a playback device and the playback device connected to the output interface supports audio playback; the second status indicates that the output interface is not connected to a playback device, or the playback device connected to the output interface does not support audio playback. When the insertion event is detected at the current output interface, if the system had no audio output before the insertion event occurred and the current output interface was in the second state, then switching the audio output to the current sound card device corresponding to the current output interface is prevented.
2. The sound card switching method of claim 1, wherein, The method further includes: When the unplug event is detected at the current output interface, the status information of the current output interface recorded in the preset status file is updated to the second status.
3. The sound card switching method according to claim 1 or 2, characterized in that, The method further includes: When the insertion event is detected at the current output interface, if there was no audio output before the insertion event occurred and the current output interface is in the first state, then the audio output is switched to the current sound card device corresponding to the current output interface.
4. The sound card switching method according to claim 1 or 2, characterized in that, The method further includes: When the unplug event is detected at the current output interface, if the system was already outputting audio before the unplug event occurred, and the current sound card device corresponding to the current output interface is a sound card device that carries the existing audio output, then the audio output is switched to the sound card device whose output interface is in the first state.
5. The sound card switching method of claim 4, wherein, The step of switching the audio output to a sound card device with its output interface in the first state includes: Execute the shell script to read and temporarily save the status information of all output interfaces in the preset status file; Iterate through all sound card devices in the preset identification file based on the temporarily saved status information; Use the pulseaudio command to switch the audio output to the sound card device with the output interface in the first state; The default identification file contains the identifiers of all sound card devices.
6. A sound card switching system characterized by, The system includes: An insertion execution module is used to prevent switching the audio output to the current sound card device corresponding to the current output interface when an insertion event is detected at the current output interface, if the system was already outputting audio before the insertion event occurred. The unplug execution module is used to prevent switching the audio output to the current sound card device corresponding to the current output interface when an unplug event is detected at the current output interface, if the system was already outputting audio before the unplug event occurred, and the current sound card device corresponding to the current output interface is not the sound card device that is already outputting audio. The insertion execution module is also used for: When the insertion event is detected at the current output interface, it is checked whether the playback device corresponding to the insertion event supports audio playback; If supported, the status information of the current output interface recorded in the preset status file is updated to the first status; if not supported, the status information of the current output interface recorded in the preset status file is updated to the second status; wherein, the first status indicates that the output interface is connected to a playback device and the playback device connected to the output interface supports audio playback; the second status indicates that the output interface is not connected to a playback device, or the playback device connected to the output interface does not support audio playback. When the insertion event is detected at the current output interface, if the system had no audio output before the insertion event occurred and the current output interface was in the second state, then switching the audio output to the current sound card device corresponding to the current output interface is prevented.
7. An electronic device, comprising: It includes a processor and a memory, and a computer program stored in the memory and executable on the processor, wherein the processor executes the program to implement the sound card switching method according to any one of claims 1-5.
8. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores at least one instruction, which is executed by a processor to implement the sound card switching method as described in any one of claims 1-5.
Citation Information
Patent Citations
Mobile terminal and audio processing method thereof
CN104267924A
Audio output equipment switching method and device and computer readable storage medium
CN115904305A