[UWP]如何将耳机切换到扬声器? [英] [UWP]How to switch the headset to the speaker?

查看:100
本文介绍了[UWP]如何将耳机切换到扬声器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想尝试编写单击按钮的uwp,它会播放你想要的输出设备中的音乐


我找到了要使用的AudioGraph API。



我的应用程序可以找到两个输出设备,耳机和扬声器,使用此功能。

 Windows.Devices.Enumeration.DeviceInformation.FindAllAsync 

但无论我选择什么,我的winphone总是播放来自耳机的音乐。


如何切换它? 哪个部分我做错了?


或者还有其他api可以做到吗?


thx。



(我已经测试了其他应用程序工作正常。我的意思是说话者没有坏掉。)

(实际上,我想写一个拨号器,但是动作拨盘仍在测试。所以我想先试试扬声器。)


 public AudioGraph audioflow; 
public AudioDeviceOutputNode deviceOuput;
public AudioFileInputNode fileInput;
Public AudioGraphSettings设置;

private async void PlayButton_Click(object sender,RoutedEventArgs e)
{


Windows.Devices.Enumeration.DeviceInformationCollection devices =
await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(Windows.Media.Devices.MediaDevice.GetAudioRenderSelector());

//显示UI以允许用户选择设备
Windows.Devices.Enumeration.DeviceInformation selectedDevice = ShowMyDeviceSelectionUI1(devices);


var song = String.Format(" /Assets/Mozart1.mp3");
var soundFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri(BaseUri,song));


settings = new AudioGraphSettings(AudioRenderCategory.Communications);
settings.QuantumSizeSelectionMode = QuantumSizeSelectionMode.LowestLatency;

settings.PrimaryRenderDevice = selectedDevice;
CreateAudioGraphResult resultg = await AudioGraph.CreateAsync(settings);


if(resultg.Status == AudioGraphCreationStatus.Success)
{
audioflow = resultg.Graph;

CreateAudioDeviceOutputNodeResult deviceOutputNodeResult = await audioflow.CreateDeviceOutputNodeAsync();

//确保音频输出准备好并且可用
if(deviceOutputNodeResult.Status == AudioDeviceNodeCreationStatus.Success)
{
deviceOuput = deviceOutputNodeResult.DeviceOutputNode;



CreateAudioFileInputNodeResult fileInputResult = await audioflow.CreateFileInputNodeAsync(soundFile);
fileInput = fileInputResult.FileInputNode;
fileInput.AddOutgoingConnection(deviceOuput);



audioflow.Start();
SpeakerButton.IsEnabled = true;



}

}

}

private DeviceInformation ShowMyDeviceSelectionUI1(DeviceInformationCollection devices)
{
foreach(设备中的var a)
ContactNameText.Text + = a.Name.ToString()+" \\\
" ;;
返回设备[1]; // [0]是耳机[1]是扬声器
}


解决方案

同样的问题:

http://stackoverflow.com/questions/34586852/how -to-开关最outputdevice耳机到所述扬声器

I wanna try to write the uwp that I click a button ,it will play music from the outputdevice that you want

I found AudioGraph API to use.

my app can find two outputdevice , headset and speaker, by using this.

Windows.Devices.Enumeration.DeviceInformation.FindAllAsync

but no matter what I chose , my winphone always plays music from the headset.

How can I switch it?  which part that I did wrong

or is there any other api can do that?

thx.

(I've tested other app working fine.  I mean the speaker isn't broken.)
(Actually, I wanna write a dialer ,but the action dial is still testing.So I wanna try the speaker first.)

public AudioGraph audioflow;
public AudioDeviceOutputNode deviceOuput;
public AudioFileInputNode fileInput;
Public AudioGraphSettings settings;
    
    private async void PlayButton_Click(object sender, RoutedEventArgs e)
        {
            

            Windows.Devices.Enumeration.DeviceInformationCollection devices =
            await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(Windows.Media.Devices.MediaDevice.GetAudioRenderSelector());

            // Show UI to allow the user to select a device
            Windows.Devices.Enumeration.DeviceInformation selectedDevice = ShowMyDeviceSelectionUI1(devices);


            var song = String.Format("/Assets/Mozart1.mp3");
            var soundFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri(BaseUri, song));


            settings = new AudioGraphSettings(AudioRenderCategory.Communications);
            settings.QuantumSizeSelectionMode = QuantumSizeSelectionMode.LowestLatency;

            settings.PrimaryRenderDevice = selectedDevice;
            CreateAudioGraphResult resultg = await AudioGraph.CreateAsync(settings);
            

            if (resultg.Status == AudioGraphCreationStatus.Success)
            {
                audioflow = resultg.Graph;

                CreateAudioDeviceOutputNodeResult deviceOutputNodeResult = await audioflow.CreateDeviceOutputNodeAsync();

                // Ensure there's audio output ready and available
                if (deviceOutputNodeResult.Status == AudioDeviceNodeCreationStatus.Success)
                {
                    deviceOuput = deviceOutputNodeResult.DeviceOutputNode;
                   

                   
                        CreateAudioFileInputNodeResult fileInputResult = await audioflow.CreateFileInputNodeAsync(soundFile);
                        fileInput = fileInputResult.FileInputNode;
                        fileInput.AddOutgoingConnection(deviceOuput);
                    
                 
                   
                    audioflow.Start();
                    SpeakerButton.IsEnabled = true;



                }

            }

        }

 private DeviceInformation ShowMyDeviceSelectionUI1(DeviceInformationCollection devices)
        {
            foreach (var a in devices)
                ContactNameText.Text += a.Name.ToString() + "\n";
            return devices[1];//[0]is headset [1]is speaker
        }


解决方案

Same question:
http://stackoverflow.com/questions/34586852/how-to-switch-the-outputdevice-headset-to-the-speaker


这篇关于[UWP]如何将耳机切换到扬声器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆