locked
Riprodurre file salvato nello storage in background? RRS feed

  • Domanda

  • Ciao a tutti,

    ho un altra domanda relativa al AudioPlayerAgent. Come posso riprodurre un file salvato precedentemente nell'isolate storage?

    Io ci ho provato ma la canzone non parte...

    AudioPlayer.cs

    using System;
    using System.Collections.Generic;
    using Microsoft.Phone.BackgroundAudio;
    namespace AudioPlaybackAgent1
    {
        public class AudioPlayer : AudioPlayerAgent
        {
            static string[] uris = 
            { 
                "isostore:/Shared/Media/Video1.mp3",
                "http://www.archive.org/download/DebussyPreludesBookI/02No.2-Voiles.mp3",
                "http://www.archive.org/download/DebussyPreludesBookI/03No.3-LeVentDansLaPlaine.mp3",
                "http://www.archive.org/download/DebussyPreludesBookI/04No.4-LesSonsEtLesParfumsTournentDansLairDuSoir.mp3",
                "http://www.archive.org/download/DebussyPreludesBookI/05No.5-LesCollinesDanacapri.mp3",
                "http://www.archive.org/download/DebussyPreludesBookI/06No.6-DesPasSurLaNeige.mp3",
                "http://www.archive.org/download/DebussyPreludesBookI/07No.7-CQuaVuLeVentDouest.mp3",
                "http://www.archive.org/download/DebussyPreludesBookI/08No.8-LaFilleAuxCheveuxDeLinNo.9-LaSrnadeInterrompue.mp3",
                "http://www.archive.org/download/DebussyPreludesBookI/09No.10-LaCathdraleEngloutie.mp3",
                "http://www.archive.org/download/DebussyPreludesBookI/10No.11-LaDanseDePuckNo.12-Minstrels.mp3"
            };
            static string[] titles = 
            { 
                "1. Danseuses de Delphes", 
                "2. Voiles", 
                "3. Le vent dans la plaine",
                "4. Les sons et les parfums tournent dans l'air du soir",
                "5. Les collines d'Anacapri",
                "6. Des pas sur la neige",
                "7. Cé qu'a vu le vent d'ouest",
                "8. La fille aux cheveux de lin\r\n9. La sérénade interrompue",
                "10. La cathédrale engloutie ",
                "11. La danse de Puck\r\n12. Minstrels "
            };
    
            static List<AudioTrack> playlist = new List<AudioTrack>();
            static int currentTrack = 0;
    
            static AudioPlayer()
            {
                // Build the playlist
                for (int i = 0; i < uris.Length; i++)
                {
                    EnabledPlayerControls playerControls =
                            EnabledPlayerControls.Pause |
                            (i != 0 ? EnabledPlayerControls.SkipPrevious : 0) |
                            (i != uris.Length - 1 ? EnabledPlayerControls.SkipNext : 0);
    
                    AudioTrack audioTrack = new AudioTrack(new Uri(uris[i],UriKind.Absolute),
                                                           titles[i],
                                                           "Alfred Cortot",
                                                           "Debussy: Preludes Book 1",
                                                           null, null, playerControls);
                    playlist.Add(audioTrack);
                }
            }
    
            /// <summary>
            /// Called when the playstate changes, except for the Error state (see OnError)
            /// </summary>
            /// <param name="player">The BackgroundAudioPlayer</param>
            /// <param name="track">The track playing at the time the playstate changed</param>
            /// <param name="playState">The new playstate of the player</param>
            /// <remarks>
            /// Play State changes cannot be cancelled. They are raised even if the application
            /// caused the state change itself, assuming the application has opted-in to the callback
            /// </remarks>
            protected override void OnPlayStateChanged(BackgroundAudioPlayer player, AudioTrack track, PlayState playState)
            {
                switch (playState)
                {
                    case PlayState.TrackReady:
                        player.Play();
                        break;
    
                    case PlayState.TrackEnded:
                        if (currentTrack < playlist.Count - 1)
                        {
                            currentTrack += 1;
                            player.Track = playlist[currentTrack];
                        }
                        else
                        {
                            player.Track = null;
                        }
                        break;
                }
                NotifyComplete();
            }
    
            /// <summary>
            /// Called when the user requests an action using system-provided UI and the application has requesed
            /// notifications of the action
            /// </summary>
            /// <param name="player">The BackgroundAudioPlayer</param>
            /// <param name="track">The track playing at the time of the user action</param>
            /// <param name="action">The action the user has requested</param>
            /// <param name="param">The data associated with the requested action.
            /// In the current version this parameter is only for use with the Seek action,
            /// to indicate the requested position of an audio track</param>
            /// <remarks>
            /// User actions do not automatically make any changes in system state; the agent is responsible
            /// for carrying out the user actions if they are supported
            /// </remarks>
            protected override void OnUserAction(BackgroundAudioPlayer player, AudioTrack track, UserAction action, object param)
            {
                switch (action)
                {
                    case UserAction.Play:
                        if (player.Track == null)
                        {
                            currentTrack = 0;
                            player.Track = playlist[currentTrack];
                        }
                        else
                        {
                            player.Play();
                        }
                        break;
    
                    case UserAction.Pause:
                        player.Pause();
                        break;
    
                    case UserAction.SkipNext:
                        if (currentTrack < playlist.Count - 1)
                        {
                            currentTrack += 1;
                            player.Track = playlist[currentTrack];
                        }
                        else
                        {
                            player.Track = null;
                        }
                        break;
    
                    case UserAction.SkipPrevious:
                        if (currentTrack > 0)
                        {
                            currentTrack -= 1;
                            player.Track = playlist[currentTrack];
                        }
                        else
                        {
                            player.Track = null;
                        }
                        break;
    
                    case UserAction.Seek:
                        player.Position = (TimeSpan)param;
                        break;
                }
                NotifyComplete();
            }
    
            /// <summary>
            /// Called whenever there is an error with playback, such as an AudioTrack not downloading correctly
            /// </summary>
            /// <param name="player">The BackgroundAudioPlayer</param>
            /// <param name="track">The track that had the error</param>
            /// <param name="error">The error that occured</param>
            /// <param name="isFatal">If true, playback cannot continue and playback of the track will stop</param>
            /// <remarks>
            /// This method is not guaranteed to be called in all cases. For example, if the background agent 
            /// itself has an unhandled exception, it won't get called back to handle its own errors.
            /// </remarks>
            protected override void OnError(BackgroundAudioPlayer player, AudioTrack track, Exception error, bool isFatal)
            {
                base.OnError(player, track, error, isFatal);
    
                //TODO: Add code to handle error conditions
    
                NotifyComplete();
            }
    
            /// <summary>
            /// Called when the agent request is getting cancelled
            /// </summary>
            protected override void OnCancel()
            {
                base.OnCancel();
                NotifyComplete();
            }
        }
    }

    martedì 1 aprile 2014 12:43

Tutte le risposte