背景音频播放器URL从主解决方案传递到类库项目

时间:2014-08-01 19:37:19

标签: windows-phone-7 windows-phone-8 microsoft-metro

我有几个网址,我想在Windows Phone 8中使用后台播放器播放它们,

基本上,在Windows Phone 8中使用背景音频我们有2个项目

enter image description here

我的问题是:

我的主项目中有10个或更多在线音频网址(此处:sdkBackgroundAudioPlayerCS)

我希望将它们传递给MyAudiplaybackAgent。

in

 public class AudioPlayer : AudioPlayerAgent
    {
        private static volatile bool _classInitialized;

        // What's the current track?
        static int currentTrackNumber = 0;

        // A playlist made up of AudioTrack items.
        private static List<AudioTrack> _playList = new List<AudioTrack>
        {
                        // A remote URI
            new AudioTrack(new Uri("https://api.soundcloud.com/tracks/33803384/stream?client_id=3904229f42df3999df223f6ebf39a8fe", UriKind.Absolute), 
                            "Episode 29", 
                            "Windows Phone Radio", 
                            "Windows Phone Radio Podcast",
                            new Uri("shared/media/Episode29.jpg", UriKind.Relative))
        };

这里是示例,AudioPlayer.cs中的url,但在我的真实场景中,我在我的项目中获得了这些url

如何通过后台播放器播放这些网址音频。?

在帖子中得到答案后更新:

我的问题是:

每一次,我点击下一步按钮我通过webclient方法在主项目中获得了mp3的新URL:

并在此处更新列表

 List<SongInfo> list = new List<SongInfo>();
            SongInfo s = new SongInfo();
            s.SongName = some new song name
            s.URL = some new url from webclient
            list.Add(s);

并添加agian

Class1.SaveAudioList(AudioListKey, list);

现在在BAP项目中,我想在这里加载更新列表:

// A playlist made up of AudioTrack items.
        private static List<AudioTrack> _playList = new List<AudioTrack>
        {

                        //Class1.LoadAudioList<T>("AudioList", ref list);

        };

请给我最好的解决方案。

1 个答案:

答案 0 :(得分:0)

有两种方法可以与您的BAP(some reference at MSDN)进行通信:

  • 使用 AudioTrack Tag属性 - 但它非常有限,可能很难传递歌曲列表
  • 使用主UI和BAP可以共享的其他资源 - 例如设置文件

以下是通过 IsolatedStorageSettings 传递列表的一个非常基本的示例:

// lets assume that you have a SongInfo class:
public struct SongInfo
{
    public string Title { get; set; }
    public string UriPath { get; set; }
}

// in your main UI you have a list of songs:
List<SongInfo> listOfSongs = new List<SongInfo>();
// we will also need a key to save/read data:
public const string AudioListKey = "AudioList";

// then lets provide two methods Save/Load:
public void SaveAudioList(string AudioListKey, IList<SongInfo> audioList)
{
    IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
    if (settings.Contains(AudioListKey)) settings[AudioListKey] = audioList;
    else settings.Add(AudioListKey, audioList);
}

public void LoadAudioList<T>(string AudioListKey, ref T target) where T : IList<SongInfo>
{
    IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
    if (!settings.Contains(AudioListKey)) throw new NotImplementedException(); // do something - list doesnt exist
    target = (T)settings[AudioListKey];
}

现在在主线程中,您可以保存列表:

SaveAudioList("AudioList", listOfSongs);

在BAP中加载应该是这样的:

LoadAudioList("AudioList", ref listOfSongsBAP);