Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Windows Phone上在后台播放音频_C#_Windows Phone 7_Windows Runtime_Windows Phone - Fatal编程技术网

C# 在Windows Phone上在后台播放音频

C# 在Windows Phone上在后台播放音频,c#,windows-phone-7,windows-runtime,windows-phone,C#,Windows Phone 7,Windows Runtime,Windows Phone,我想在Windows Phone的后台播放一些音频。我已经从Microsoft()编写了一些类似此示例的代码,但在我的应用程序中,用户有机会选择后台代理必须播放的uri。但我不知道如何将应用程序中的audiotrack元素设置为后台代理的audiotrack元素 我已在我的代理中尝试了以下代码: private static AudioTrack _streamTrack; public static AudioTrack StreamTrack { get { return _streamTr

我想在Windows Phone的后台播放一些音频。我已经从Microsoft()编写了一些类似此示例的代码,但在我的应用程序中,用户有机会选择后台代理必须播放的uri。但我不知道如何将应用程序中的audiotrack元素设置为后台代理的audiotrack元素

我已在我的代理中尝试了以下代码:

private static AudioTrack _streamTrack;
public static AudioTrack StreamTrack { get { return _streamTrack; } set { _streamTrack = value; } }
并尝试在我的应用程序中设置此变量,如:

AudioPlayer.StreamTrack = new AudioTrack(new Uri(stream.StreamUri, UriKind.Absolute), stream.StreamName, stream.StreamGenre, stream.StreamGenre, null);

但它不起作用。如何解决此问题?

实现此目的的一种方法是使用XNA库

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
然后宣布你的音效

SoundEffect _BGMUSIC;
我使用这种加载声音效果的方法

 //Put this in your main method
 LoadSound("sfx/piano.wav", out _BGMUSIC);


 //put this method in the same class
 private void LoadSound(String SoundFilePath, out SoundEffect Sound)
        {
            // For error checking, assume we'll fail to load the file.
            Sound = null;

            try
            {
                // Holds informations about a file stream.
                StreamResourceInfo SoundFileInfo = App.GetResourceStream(new Uri(SoundFilePath, UriKind.Relative));

                // Create the SoundEffect from the Stream
                Sound = SoundEffect.FromStream(SoundFileInfo.Stream);
                FrameworkDispatcher.Update();
            }
            catch (NullReferenceException)
            {
                // Display an error message
                MessageBox.Show("Couldn't load sound " + SoundFilePath);
            }
        }
最后你可以播放你的音效

 _BGMUSIC.Play();

您应该只将url设置为BackgroundAudioPlayer.Instance.Track

XAML


如果下面的答案解决了您的问题,请确保选中并向上投票。如果没有,请提供一个评论,说明为什么它不起作用。谢谢
<StackPanel Orientation="Vertical">
  <TextBlock HorizontalAlignment="Center"
             VerticalAlignment="Center"
             Text="Enter url into textbox" />
  <TextBox Name="fileUrl" />
  <Button Content="&gt;"
          Height="100"
          Width="100"
          Click="playCustomFile_Click" />
</StackPanel>
private void playCustomFile_Click(object sender, RoutedEventArgs e)
{
  if (string.IsNullOrEmpty(fileUrl.Text.Trim().ToString()))
     MessageBox.Show("Please enter url first");
  else
     BackgroundAudioPlayer.Instance.Track = new AudioTrack(new Uri(fileUrl.Text.Trim().ToString(), UriKind.Absolute), "title","artist","album", new Uri("albumArtUrl",UriKind.RelativeOrAbsolute));
}