I'm trying to make an music app , I have 2 project in solution: a Main project and an AudioPlaybackAgent (APA) project. When I chose a song from my main project (which contained in a list of song) , I copied this whole list into a Json file in ISolate Storage and read it from my APA project.
Problem is, I get the error when I tried to create a new AudioTrack from items I get in the json file
Code to create the file (from my main project)
public static void CreateOnlineList(string id, ObservableCollection<BasicItemModel> list)
{
using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
{
string fileName = "OnlineList.json";
JObject obj = new JObject();
JArray arr = new JArray();
for (int dem = 0; dem < list.Count(); dem++)
{
if (list[dem].Id == id)
obj.Add("index", dem);
JObject item = new JObject();
item.Add("name", list[dem].Name);
item.Add("artist", list[dem].Artist);
item.Add("url", list[dem].Url);
arr.Add(item);
}
obj.Add("list", arr);
string jsonString = JsonConvert.SerializeObject(obj);
using (IsolatedStorageFileStream stream = file.OpenFile(fileName,FileMode.OpenOrCreate,FileAccess.Write))
{
using (StreamWriter writer = new StreamWriter(stream))
{
writer.WriteLine(jsonString);
}
}
}
}
Code to read this file (from APA project)
public static void ReadOnlineList()
{
using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
{
string fileName = "OnlineList.json";
string jsonString = "";
using (IsolatedStorageFileStream stream = file.OpenFile(fileName, FileMode.Open, FileAccess.Read))
{
using (StreamReader writer = new StreamReader(stream))
{
jsonString = writer.ReadLine();
}
}
JObject obj = new JObject();
obj = JObject.Parse(jsonString);
AudioPlayer.currentTrackNumber = int.Parse(obj["index"].ToString());
List<JToken> arr = obj["list"].ToList();
if (AudioPlayer.playList == null) AudioPlayer.playList = new List<AudioTrack>();
else AudioPlayer.playList.Clear();
foreach (var item in arr)
{
AudioTrack track = new AudioTrack()
{
Source = new Uri(item["url"].ToString(), UriKind.Absolute),
Title = item["name"].ToString(),
Artist = item["artist"].ToString(),
Album = null
};
//// I get error of this line when trying to create an track
AudioPlayer.playList.Add(track);
}
}
}