2

我想在我的 Windows Phone 的后台播放一些音频。我已经从 Microsoft ( http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh202978(v=vs.105).aspx )编写了一些类似此示例的代码,但在我的应用程序中用户有机会选择后台代理必须播放的uri。但我不知道如何将应用程序中的音轨元素设置为后台代理的音轨元素。

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

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);

但它不起作用。我该如何解决这个问题?

4

2 回答 2

0

实现此目的的一种方法是使用 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();
于 2013-03-10T19:27:12.863 回答
0

您应该只将 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>

CS

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));
}
于 2015-12-06T11:35:28.163 回答