简体   繁体   中英

play audio in background on Windows Phone

I want to play some audio in the background on my Windows Phone. I've written some code like this sample from Microsoft ( http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh202978(v=vs.105).aspx ), but on my app the user has the opportunity to select a uri which the background agent has to play. But I don't know how I can set the audiotrack element from my app to the audiotrack element of the background agent.

I've tried the following code in my agent:

private static AudioTrack _streamTrack;
public static AudioTrack StreamTrack { get { return _streamTrack; } set { _streamTrack = value; } }

And try to set this variable in my app like:

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

But it doesn't work. How can I fix this problem?

One way to accomplish this is to use the XNA library

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;

Then declare your soundeffect

SoundEffect _BGMUSIC;

I use this method of loading sound effects

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

Finally you can play your sound effect

 _BGMUSIC.Play();

You should only set the url to BackgroundAudioPlayer.Instance.Track.

Source code

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM