public override void ViewDidDisappear (bool animated)
		{
			base.ViewDidDisappear (animated);
			updatingTimer.Invalidate ();

			if (player != null) {
				player.Pause ();
				player.FlushAndClose ();
				player = null;
			}
		}
        public override void ViewDidDisappear(bool animated)
        {
            base.ViewDidDisappear(animated);

            if (updatingTimer != null)
            {
                updatingTimer.Invalidate();
            }

            if (player != null)
            {
                player.FlushAndClose();
                player = null;
            }
        }
		void StreamDownloaded (IAsyncResult result)
		{
			var request = result.AsyncState as HttpWebRequest;
			bool pushed = false;
			try {
				var response = request.EndGetResponse (result);
				var stream = response.GetResponseStream ();
				var buffer = new byte [8192];
				int l = 0, n;
				
				InvokeOnMainThread (delegate {
					viewController.PushViewController (playController, true);
				});
				
				pushed = true;
							
				// The following line prevents the audio from stopping 
				// when the device autolocks.
				AudioSession.Category = AudioSessionCategory.MediaPlayback;

				// 
				// Create StreamingPlayer, the using statement will automatically
				// force the resources to be disposed and the playback to stop.
				//
				using (player = new StreamingPlayback ()){
					while ((n = stream.Read (buffer, 0, buffer.Length)) != 0){
						l += n;
						player.ParseBytes (buffer, n, false);
						
						InvokeOnMainThread (delegate {
							progress.Progress = l / (float) response.ContentLength;
						});
					}
					
				}
			} catch (Exception e){
				InvokeOnMainThread (delegate {
					if (pushed){
						viewController.PopToRootViewController (true);
						pushed = false;
					}
					status.Text = "Error fetching response stream\n" + e;
					Console.WriteLine (e);
				});
			}
	
			//
			// Restore the default AudioSession, this allows the iPhone
			// to go to sleep now that we are done playing the audio
			//
			AudioSession.Category = AudioSessionCategory.SoloAmbientSound;
			if (pushed){
				viewController.PopToRootViewController (true);
				status.Text = "Finished playback";
			}
		}
        void StreamDownloadedHandler(IAsyncResult result)
        {
            var    buffer = new byte [8192];
            int    l      = 0;
            int    inputStreamLength;
            double sampleRate = 0;

            Stream             inputStream;
            AudioQueueTimeline timeline = null;

            var request = result.AsyncState as HttpWebRequest;

            try {
                var response       = request.EndGetResponse(result);
                var responseStream = response.GetResponseStream();

                if (PlayerOption == PlayerOption.StreamAndSave)
                {
                    inputStream = GetQueueStream(responseStream);
                }
                else
                {
                    inputStream = responseStream;
                }

                using (player = new StreamingPlayback()) {
                    player.OutputReady += delegate {
                        timeline   = player.OutputQueue.CreateTimeline();
                        sampleRate = player.OutputQueue.SampleRate;
                    };

                    InvokeOnMainThread(delegate {
                        if (updatingTimer != null)
                        {
                            updatingTimer.Invalidate();
                        }

                        updatingTimer = NSTimer.CreateRepeatingScheduledTimer(0.5, (timer) => RepeatingAction(timeline, sampleRate));
                    });

                    while ((inputStreamLength = inputStream.Read(buffer, 0, buffer.Length)) != 0 && player != null)
                    {
                        l += inputStreamLength;
                        player.ParseBytes(buffer, inputStreamLength, false, l == (int)response.ContentLength);

                        InvokeOnMainThread(delegate {
                            progressBar.Progress = l / (float)response.ContentLength;
                        });
                    }
                }
            } catch (Exception e) {
                RaiseErrorOccurredEvent("Error fetching response stream\n" + e);
                Debug.WriteLine(e);
                InvokeOnMainThread(delegate {
                    if (NavigationController != null)
                    {
                        NavigationController.PopToRootViewController(true);
                    }
                });
            }
        }
		private void StreamDownloadedHandler (IAsyncResult result)
		{
			var buffer = new byte [8192];
			int l = 0;
			int inputStreamLength;
			double sampleRate = 0;

			Stream inputStream;
			AudioQueueTimeline timeline = null;

			var request = result.AsyncState as HttpWebRequest;
			try {
				var response = request.EndGetResponse (result);
				var responseStream = response.GetResponseStream ();

				if (PlayerOption == PlayerOption.StreamAndSave)
					inputStream = GetQueueStream (responseStream);
				else
					inputStream = responseStream;

				using (player = new StreamingPlayback ()) {
					player.OutputReady += delegate {
						timeline = player.OutputQueue.CreateTimeline ();
						sampleRate = player.OutputQueue.SampleRate;
					};

					InvokeOnMainThread (delegate {
						if (updatingTimer != null)
							updatingTimer.Invalidate ();

						updatingTimer = NSTimer.CreateRepeatingScheduledTimer (0.5, () => RepeatingAction (timeline, sampleRate));
					});

					while ((inputStreamLength = inputStream.Read (buffer, 0, buffer.Length)) != 0 && player != null) {
						l += inputStreamLength;
						player.ParseBytes (buffer, inputStreamLength, false, l == (int)response.ContentLength);

						InvokeOnMainThread (delegate {
							progressBar.Progress = l / (float)response.ContentLength;
						});
					}
				}

			} catch (Exception e) {
				RaiseErrorOccurredEvent ("Error fetching response stream\n" + e);
				Debug.WriteLine (e);
				InvokeOnMainThread (delegate {
					if (NavigationController != null)
						NavigationController.PopToRootViewController (true);
				});
			}
		}
        void StreamDownloaded(IAsyncResult result)
        {
            var  request = result.AsyncState as HttpWebRequest;
            bool pushed  = false;

            try {
                var    response       = request.EndGetResponse(result);
                var    responseStream = response.GetResponseStream();
                Stream inputStream;
                var    buffer = new byte [8192];
                int    l = 0, n;

                InvokeOnMainThread(delegate {
                    viewController.PushViewController(playController, true);
                });

                pushed = true;

                if (saveCopy)
                {
                    inputStream = MakeQueueStream(responseStream);
                }
                else
                {
                    inputStream = responseStream;
                }

                //
                // Create StreamingPlayer, the using statement will automatically
                // force the resources to be disposed and the playback to stop.
                //
                using (player = new StreamingPlayback()){
                    while ((n = inputStream.Read(buffer, 0, buffer.Length)) != 0)
                    {
                        l += n;
                        player.ParseBytes(buffer, n, false, l == (int)response.ContentLength);

                        InvokeOnMainThread(delegate {
                            progress.Progress = l / (float)response.ContentLength;
                        });
                    }
                }
            } catch (Exception e) {
                InvokeOnMainThread(delegate {
                    if (pushed)
                    {
                        viewController.PopToRootViewController(true);
                        pushed = false;
                    }
                    status.Text = "Error fetching response stream\n" + e;
                    Console.WriteLine(e);
                });
            }

            //
            // Restore the default AudioSession, this allows the iPhone
            // to go to sleep now that we are done playing the audio
            //
            AudioSession.Category = AudioSessionCategory.MediaPlayback;
            if (pushed)
            {
                viewController.PopToRootViewController(true);
                status.Text = "Finished playback";
            }
        }
Beispiel #7
0
        void StreamDownloaded(IAsyncResult result)
        {
            var  request = result.AsyncState as HttpWebRequest;
            bool pushed  = false;

            try {
                var    response       = request.EndGetResponse(result);
                var    responseStream = response.GetResponseStream();
                Stream inputStream;
                var    buffer = new byte [8192];
                int    l = 0, n;

                InvokeOnMainThread(delegate {
                    viewController.PushViewController(playController, true);
                });

                pushed = true;

                if (saveCopy)
                {
                    inputStream = MakeQueueStream(responseStream);
                }
                else
                {
                    inputStream = responseStream;
                }

                //
                // Create StreamingPlayer, the using statement will automatically
                // force the resources to be disposed and the playback to stop.
                //
                using (player = new StreamingPlayback()){
                    AudioQueueTimeline timeline   = null;
                    double             sampleRate = 0;

                    player.OutputReady += delegate {
                        timeline   = player.OutputQueue.CreateTimeline();
                        sampleRate = player.OutputQueue.SampleRate;
                    };
                    InvokeOnMainThread(delegate {
                        if (updatingTimer != null)
                        {
                            updatingTimer.Invalidate();
                        }

                        updatingTimer = NSTimer.CreateRepeatingScheduledTimer(0.5, delegate {
                            var queue = player.OutputQueue;
                            if (queue == null || timeline == null)
                            {
                                return;
                            }

                            bool disc           = false;
                            AudioTimeStamp time = new AudioTimeStamp();
                            queue.GetCurrentTime(timeline, ref time, ref disc);

                            playbackTime.Text = FormatTime(time.SampleTime / sampleRate);
                        });
                    });
                    while ((n = inputStream.Read(buffer, 0, buffer.Length)) != 0)
                    {
                        l += n;
                        player.ParseBytes(buffer, n, false, l == (int)response.ContentLength);

                        InvokeOnMainThread(delegate {
                            progress.Progress = l / (float)response.ContentLength;
                        });
                    }
                }
            } catch (Exception e) {
                InvokeOnMainThread(delegate {
                    if (pushed)
                    {
                        viewController.PopToRootViewController(true);
                        pushed = false;
                    }
                    status.Text = "Error fetching response stream\n" + e;
                    Console.WriteLine(e);
                });
            }

            //
            // Restore the default AudioSession, this allows the iPhone
            // to go to sleep now that we are done playing the audio
            //
            AudioSession.Category = AudioSessionCategory.MediaPlayback;
            if (pushed)
            {
                viewController.PopToRootViewController(true);
                status.Text = "Finished playback";
            }
        }
Beispiel #8
0
		void StreamDownloaded (IAsyncResult result)
		{
			var request = result.AsyncState as HttpWebRequest;
			bool pushed = false;
			try {
				var response = request.EndGetResponse (result);
				var responseStream = response.GetResponseStream ();
				Stream inputStream;
				var buffer = new byte [8192];
				int l = 0, n;
				
				InvokeOnMainThread (delegate {
					viewController.PushViewController (playController, true);
				});
				
				pushed = true;
				
				if (saveCopy)
					inputStream = MakeQueueStream (responseStream);
				else
					inputStream = responseStream;
				
				// 
				// Create StreamingPlayer, the using statement will automatically
				// force the resources to be disposed and the playback to stop.
				//
				using (player = new StreamingPlayback ()){
					AudioQueueTimeline timeline = null;
					double sampleRate = 0;
					
					player.OutputReady += delegate {
						timeline = player.OutputQueue.CreateTimeline ();
						sampleRate = player.OutputQueue.SampleRate;
					};
					InvokeOnMainThread (delegate {
						if (updatingTimer != null)
							updatingTimer.Invalidate ();
								
						updatingTimer = NSTimer.CreateRepeatingScheduledTimer (0.5, delegate {
							var queue = player.OutputQueue;
							if (queue == null || timeline == null)
								return;
							bool disc;
							AudioTimeStamp time;
							queue.GetCurrentTime (timeline, ref time, ref disc);
							
							playbackTime.Text = FormatTime (time.SampleTime / sampleRate);
						});
					});
					while ((n = inputStream.Read (buffer, 0, buffer.Length)) != 0){
						l += n;
						player.ParseBytes (buffer, n, false, l == (int)response.ContentLength);
						
						InvokeOnMainThread (delegate {
							progress.Progress = l / (float) response.ContentLength;
						});
					}
					
				}
			} catch (Exception e){
				InvokeOnMainThread (delegate {
					if (pushed){
						viewController.PopToRootViewController (true);
						pushed = false;
					}
					status.Text = "Error fetching response stream\n" + e;
					Console.WriteLine (e);
				});
			}
	
			//
			// Restore the default AudioSession, this allows the iPhone
			// to go to sleep now that we are done playing the audio
			//
			AudioSession.Category = AudioSessionCategory.MediaPlayback;
			if (pushed){
				viewController.PopToRootViewController (true);
				status.Text = "Finished playback";
			}
		}