1

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);
            }
        }
    }
user3448806
  • 897
  • 10
  • 22

1 Answers1

1

The AudioTrack object has some strange behaviour and there are two ways in which you can insert/edit data from an AudioTrack.

In this case you are accessing its properties and that you will generate the error you are getting.

You can do one of two things:

Option 1 - Insert data in the constructor

AudioTrack track = new AudioTrack(new Uri(item["url"].ToString(), UriKind.Absolute), item["artist"].ToString(), null, null);

Option 2 - Use BeginEdit() and EndEdit() methods

AudioTrack track = new AudioTrack();
track.BeginEdit();
track.Source = new Uri(item["url"].ToString(), UriKind.Absolute);
track.Title = item["name"].ToString();
track.Artist = item["artist"].ToString();
track.EndEdit();

I hope this helps and answers your question.

meneses.pt
  • 2,588
  • 3
  • 27
  • 38
  • I tried option 2 and it worked , thanks very much :) – user3448806 Sep 24 '14 at 01:54
  • and I'm having a trouble with my 2nd app too, could you help me ? http://stackoverflow.com/questions/25992470/the-background-audio-resources-are-no-longer-available-after-call-background – user3448806 Sep 24 '14 at 01:55