public void AttachAndStartPlayer(AudioVideoFlow audioVideoFlow, bool loop) { // Create a player and attach it to a AudioVideoFlow Player player = new Player(); player.AttachFlow(audioVideoFlow); //Subscribe to the player's state changed event, including the play completed event. player.StateChanged += delegate(object sender, PlayerStateChangedEventArgs args) { if (loop && args.TransitionReason == PlayerStateTransitionReason.PlayCompleted) { // Restart player as soon as it completes playing the file. ((Player)sender).Start(); } }; //Load the file into memory WmaFileSource source = new WmaFileSource("music.wma"); source.BeginPrepareSource(MediaSourceOpenMode.Buffered, source_PrepareSourceCompleted, source); _waitForPrepareSourceCompleted.WaitOne(); player.SetSource(source); //Start playing the file player.Start(); }
private void CreatePlayer() { var source = new WmaFileSource("music.wma"); source.EndPrepareSource(source.BeginPrepareSource(MediaSourceOpenMode.Buffered, null, null)); _player = new Player(); _player.SetSource(source); _player.SetMode(PlayerMode.Manual); _player.StateChanged += player_StateChanged; _player.Start(); }
void Flow_StateChanged(object sender, MediaFlowStateChangedEventArgs e) { Log("Flow_StateChanged PreviousState=" + e.PreviousState + " State=" + e.State); AudioVideoFlow avFlow = (AudioVideoFlow)sender; if (avFlow.State == MediaFlowState.Active) { Player player = new Player(); player.StateChanged += new EventHandler <PlayerStateChangedEventArgs>(player_StateChanged); player.AttachFlow(avFlow); WmaFileSource src = new WmaFileSource(_FileName); src.BeginPrepareSource(MediaSourceOpenMode.Buffered, ar => { try { src.EndPrepareSource(ar); player.SetSource(src); // For some reason, PlayerMode.Automatic does not loop audio player.SetMode(PlayerMode.Manual); player.Start(); Log("Playing \"" + _FileName + "\""); } catch (Exception ex) { Log(ex.ToString()); } }, null); } else if (avFlow.State == MediaFlowState.Terminated) { if (avFlow.Player != null) { if (avFlow.Player.Source != null) { avFlow.Player.Source.Close(); } avFlow.Player.DetachFlow(avFlow); } } }
/// <summary> /// Plays a WMA file using the specified Audio Video Flow. /// </summary> /// <param name="flowToPlayUsing"> /// The flow to use when playing the file. /// </param> /// <param name="fileToPlay"> /// The path to the wma file to be played. /// </param> private void PlayWmaFileAudio(AudioVideoFlow flowToPlayUsing, string fileToPlay) { // Set the media source that the player will use. WmaFileSource wmaSource = new WmaFileSource(fileToPlay); MediaSourceAndAvFlowContainer sourceAndFlowContainer = new MediaSourceAndAvFlowContainer( wmaSource, flowToPlayUsing); try { wmaSource.BeginPrepareSource(MediaSourceOpenMode.Unbuffered, WmaSourcePreparationCompleted, sourceAndFlowContainer); } catch (ArgumentOutOfRangeException argOOREx) { // TODO (Left to the reader): Write actual handling code for the // occurrence. Console.WriteLine("An ArgumentOutOfRangeException occured when preparing the media source: " + "{0}", argOOREx.ToString()); } }
protected override void StartupCore() { try { m_mohFileSource = new WmaFileSource(m_mohFilePath); m_mohPlayer = new Player(); m_mohPlayer.SetMode(PlayerMode.Manual); m_mohPlayer.SetSource(m_mohFileSource); m_mohPlayer.StateChanged += this.OnPlayerStateChanged; m_mohFileSource.BeginPrepareSource( MediaSourceOpenMode.Buffered, ar => { try { m_mohFileSource.EndPrepareSource(ar); m_mohPlayer.Start(); this.CompleteStartup(null); } catch (InvalidOperationException ioe) { this.CompleteStartup(ioe); } catch (OperationFailureException rte) { this.CompleteStartup(rte); } }, null); } catch (InvalidOperationException ioe) { this.CompleteStartup(ioe); } }
void Flow_StateChanged(object sender, MediaFlowStateChangedEventArgs e) { Log("Flow_StateChanged PreviousState=" + e.PreviousState + " State=" + e.State); AudioVideoFlow avFlow = (AudioVideoFlow)sender; if (avFlow.State == MediaFlowState.Active) { Player player = new Player(); player.StateChanged += new EventHandler<PlayerStateChangedEventArgs>(player_StateChanged); player.AttachFlow(avFlow); WmaFileSource src = new WmaFileSource(_FileName); src.BeginPrepareSource(MediaSourceOpenMode.Buffered, ar=> { try { src.EndPrepareSource(ar); player.SetSource(src); // For some reason, PlayerMode.Automatic does not loop audio player.SetMode(PlayerMode.Manual); player.Start(); Log("Playing \"" + _FileName + "\""); } catch (Exception ex) { Log(ex.ToString()); } }, null); } else if (avFlow.State == MediaFlowState.Terminated) { if (avFlow.Player != null) { if (avFlow.Player.Source != null) { avFlow.Player.Source.Close(); } avFlow.Player.DetachFlow(avFlow); } } }
public void Run() { // Create AudioVideoFlow AudioVideoFlowHelper audioVideoFlowHelper = new AudioVideoFlowHelper(); _audioVideoFlow = audioVideoFlowHelper.CreateAudioVideoFlow( null, audioVideoFlow_StateChanged); // Create a player and attach it to a AudioVideoFlow Player player = new Player(); player.AttachFlow(_audioVideoFlow); //Subscribe to the player's state changed event, including the play completed event. player.StateChanged += new EventHandler <PlayerStateChangedEventArgs>(player_StateChanged); //Load the file into memory WmaFileSource source = new WmaFileSource("music.wma"); source.BeginPrepareSource(MediaSourceOpenMode.Buffered, source_PrepareSourceCompleted, source); _waitForPrepareSourceCompleted.WaitOne(); //in automatic mode, player will start playing only when the flow is in the active state. //in manual mode, player will start playing immediately. player.SetMode(PlayerMode.Automatic); player.SetSource(source); //Start playing the file player.Start(); Console.WriteLine("Start playing for 10 seconds"); //Allow the player to play for 10 seconds by waiting for 10 seconds Thread.Sleep(10000); //Pauses player player.Pause(); Console.WriteLine("Pausing for 5 seconds"); //Wait 5 seconds Thread.Sleep(5000); //Change playback speed to half of the regular speed player.PlaybackSpeed = PlaybackSpeed.Half; Console.WriteLine("Playback speed change to half of the regular speed"); //Resume playing from where we paused the player at previously player.Start(); Console.WriteLine("Resume playing for 10 seconds"); Thread.Sleep(10000); //Stop the player and reset position to the beginning player.Stop(); Console.WriteLine("Stopping the player"); // Source must be closed after it is no longer needed, otherwise memory will not be released even after garbage collection. source.Close(); //player must be detached from the flow, otherwise if the player is rooted, it will keep the flow in memory. player.DetachFlow(_audioVideoFlow); // Shutdown the platform ShutdownPlatform(); //Wait for shutdown to occur. _waitForShutdownEventCompleted.WaitOne(); }