Show how to use the Pause() function from the UI
Show how to use the AudioSession to prevent the iPhone from suspending us
This commit is contained in:
Miguel de Icaza 2010-01-08 02:10:03 -05:00
Родитель 80a91b5fbd
Коммит f6e61315ad
4 изменённых файлов: 165 добавлений и 29 удалений

Просмотреть файл

@ -30,22 +30,58 @@ namespace StreamingAudio
{
}
StreamingPlayback player = null;
bool paused;
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
window.AddSubview (viewController.View);
AudioSession.Initialize ();
// Nice creative commons source.
entry.Text = "http://ccmixter.org/content/bradstanfield/bradstanfield_-_People_Let_s_Stop_The_War.mp3";
entry.EditingDidEnd += delegate {
entry.ResignFirstResponder ();
};
window.MakeKeyAndVisible ();
return true;
}
partial void playControlClicked (UIButton sender)
{
paused = !paused;
if (player == null)
return;
if (paused)
player.Pause ();
else
player.Play ();
string title = paused ? "Play" : "Pause";
button.SetTitle (title, UIControlState.Normal);
button.SetTitle (title, UIControlState.Selected);
}
partial void volumeSet (UISlider sender)
{
if (player == null)
return;
player.OutputQueue.Volume = sender.Value;
}
partial void startPlayback (UIButton sender)
{
status.Text = "Starting HTTP request";
AudioSession.Category = AudioSessionCategory.MediaPlayback;
AudioSession.RoutingOverride = AudioSessionRoutingOverride.Speaker;
button.TitleLabel.Text = "Pause";
try {
var request = (HttpWebRequest) WebRequest.Create (entry.Text);
request.BeginGetResponse (StreamDownloaded, request);
@ -69,21 +105,21 @@ namespace StreamingAudio
});
pushed = true;
StreamingPlayback player = null;
try {
player = new StreamingPlayback ();
} catch (Exception e){
Console.WriteLine (e);
}
while ((n = stream.Read (buffer, 0, buffer.Length)) != 0){
l += n;
player.ParseBytes (buffer, n, false);
InvokeOnMainThread (delegate {
progress.Progress = l / (float) response.ContentLength;
});
//
// Create StreamingPlayer, the using statement will automatically
// force the resources to be disposed and the AudioSession to be
// reset when the playback is over
//
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 {

Просмотреть файл

@ -2,10 +2,10 @@
<archive type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="7.10">
<data>
<int key="IBDocument.SystemTarget">768</int>
<string key="IBDocument.SystemVersion">10B504</string>
<string key="IBDocument.SystemVersion">10C540</string>
<string key="IBDocument.InterfaceBuilderVersion">740</string>
<string key="IBDocument.AppKitVersion">1038.22</string>
<string key="IBDocument.HIToolboxVersion">457.00</string>
<string key="IBDocument.AppKitVersion">1038.25</string>
<string key="IBDocument.HIToolboxVersion">458.00</string>
<object class="NSMutableDictionary" key="IBDocument.PluginVersions">
<string key="NS.key.0">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
<string key="NS.object.0">62</string>
@ -13,8 +13,8 @@
<object class="NSMutableArray" key="IBDocument.EditedObjectIDs">
<bool key="EncodedWithXMLCoder">YES</bool>
<integer value="2"/>
<integer value="21"/>
<integer value="39"/>
<integer value="21"/>
</object>
<object class="NSArray" key="IBDocument.PluginDependencies">
<bool key="EncodedWithXMLCoder">YES</bool>
@ -62,7 +62,7 @@
<bool key="IBUIClipsSubviews">YES</bool>
<bool key="IBUIMultipleTouchEnabled">YES</bool>
</object>
<object class="NSArray" key="IBUIViewControllers">
<object class="NSMutableArray" key="IBUIViewControllers">
<bool key="EncodedWithXMLCoder">YES</bool>
<object class="IBUIViewController" id="138859781">
<object class="IBUIView" key="IBUIView" id="812770021">
@ -96,14 +96,14 @@
<bool key="IBUIClearsContextBeforeDrawing">NO</bool>
<int key="IBUIContentHorizontalAlignment">0</int>
<int key="IBUIContentVerticalAlignment">0</int>
<object class="NSFont" key="IBUIFont">
<object class="NSFont" key="IBUIFont" id="120824268">
<string key="NSName">Helvetica-Bold</string>
<double key="NSSize">15</double>
<int key="NSfFlags">16</int>
</object>
<int key="IBUIButtonType">1</int>
<string key="IBUINormalTitle">Start Playback</string>
<object class="NSColor" key="IBUIHighlightedTitleColor">
<object class="NSColor" key="IBUIHighlightedTitleColor" id="592136135">
<int key="NSColorSpace">3</int>
<bytes key="NSWhite">MQA</bytes>
</object>
@ -111,7 +111,7 @@
<int key="NSColorSpace">1</int>
<bytes key="NSRGB">MC4xOTYwNzg0MyAwLjMwOTgwMzkzIDAuNTIxNTY4NjYAA</bytes>
</object>
<object class="NSColor" key="IBUINormalTitleShadowColor">
<object class="NSColor" key="IBUINormalTitleShadowColor" id="780032884">
<int key="NSColorSpace">3</int>
<bytes key="NSWhite">MC41AA</bytes>
</object>
@ -218,7 +218,7 @@
<bool key="IBUIOpaque">NO</bool>
<bool key="IBUIClipsSubviews">YES</bool>
<bool key="IBUIUserInteractionEnabled">NO</bool>
<string key="IBUIText">Volume: (Not hooked up yet)</string>
<string key="IBUIText">Volume:</string>
<reference key="IBUITextColor" ref="79086812"/>
<nil key="IBUIHighlightedColor"/>
<int key="IBUIBaselineAdjustment">1</int>
@ -270,6 +270,25 @@
<bool key="IBUIClipsSubviews">YES</bool>
<bool key="IBUIMultipleTouchEnabled">YES</bool>
</object>
<object class="IBUIButton" id="412927193">
<reference key="NSNextResponder" ref="983264090"/>
<int key="NSvFlags">292</int>
<string key="NSFrame">{{124, 372}, {72, 37}}</string>
<reference key="NSSuperview" ref="983264090"/>
<bool key="IBUIOpaque">NO</bool>
<bool key="IBUIClearsContextBeforeDrawing">NO</bool>
<int key="IBUIContentHorizontalAlignment">0</int>
<int key="IBUIContentVerticalAlignment">0</int>
<reference key="IBUIFont" ref="120824268"/>
<int key="IBUIButtonType">1</int>
<string key="IBUINormalTitle">Pause</string>
<reference key="IBUIHighlightedTitleColor" ref="592136135"/>
<object class="NSColor" key="IBUINormalTitleColor">
<int key="NSColorSpace">1</int>
<bytes key="NSRGB">MC4xOTYwNzg0MyAwLjMwOTgwMzkzIDAuNTIxNTY4NjYAA</bytes>
</object>
<reference key="IBUINormalTitleShadowColor" ref="780032884"/>
</object>
</object>
<string key="NSFrameSize">{320, 460}</string>
<reference key="NSSuperview"/>
@ -359,6 +378,32 @@
</object>
<int key="connectionID">47</int>
</object>
<object class="IBConnectionRecord">
<object class="IBCocoaTouchOutletConnection" key="connection">
<string key="label">button</string>
<reference key="source" ref="987256611"/>
<reference key="destination" ref="412927193"/>
</object>
<int key="connectionID">52</int>
</object>
<object class="IBConnectionRecord">
<object class="IBCocoaTouchEventConnection" key="connection">
<string key="label">playControlClicked:</string>
<reference key="source" ref="412927193"/>
<reference key="destination" ref="987256611"/>
<int key="IBEventType">1</int>
</object>
<int key="connectionID">53</int>
</object>
<object class="IBConnectionRecord">
<object class="IBCocoaTouchEventConnection" key="connection">
<string key="label">volumeSet:</string>
<reference key="source" ref="230519471"/>
<reference key="destination" ref="987256611"/>
<int key="IBEventType">13</int>
</object>
<int key="connectionID">54</int>
</object>
</object>
<object class="IBMutableOrderedSet" key="objectRecords">
<object class="NSArray" key="orderedObjects">
@ -477,6 +522,7 @@
<reference ref="343504676"/>
<reference ref="85541954"/>
<reference ref="1021442588"/>
<reference ref="412927193"/>
</object>
<reference key="parent" ref="75935306"/>
</object>
@ -515,6 +561,11 @@
<reference key="object" ref="1021442588"/>
<reference key="parent" ref="983264090"/>
</object>
<object class="IBObjectRecord">
<int key="objectID">51</int>
<reference key="object" ref="412927193"/>
<reference key="parent" ref="983264090"/>
</object>
</object>
</object>
<object class="NSMutableDictionary" key="flattenedProperties">
@ -549,6 +600,7 @@
<string>48.IBPluginDependency</string>
<string>49.IBPluginDependency</string>
<string>50.IBPluginDependency</string>
<string>51.IBPluginDependency</string>
</object>
<object class="NSMutableArray" key="dict.values">
<bool key="EncodedWithXMLCoder">YES</bool>
@ -574,7 +626,7 @@
<string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
<string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
<string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
<string>{{306, 185}, {320, 480}}</string>
<string>{{355, 258}, {320, 480}}</string>
<string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
<string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
<string>AppDelegate</string>
@ -586,6 +638,7 @@
<string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
<string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
<string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
<string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
</object>
</object>
<object class="NSMutableDictionary" key="unlocalizedProperties">
@ -604,7 +657,7 @@
</object>
</object>
<nil key="sourceID"/>
<int key="maxID">50</int>
<int key="maxID">54</int>
</object>
<object class="IBClassDescriber" key="IBDocument.Classes">
<object class="NSMutableArray" key="referencedPartialClassDescriptions">
@ -612,13 +665,25 @@
<object class="IBPartialClassDescription">
<string key="className">AppDelegate</string>
<object class="NSMutableDictionary" key="actions">
<string key="NS.key.0">startPlayback:</string>
<string key="NS.object.0">id</string>
<bool key="EncodedWithXMLCoder">YES</bool>
<object class="NSArray" key="dict.sortedKeys">
<bool key="EncodedWithXMLCoder">YES</bool>
<string>playControlClicked:</string>
<string>startPlayback:</string>
<string>volumeSet:</string>
</object>
<object class="NSMutableArray" key="dict.values">
<bool key="EncodedWithXMLCoder">YES</bool>
<string>id</string>
<string>id</string>
<string>id</string>
</object>
</object>
<object class="NSMutableDictionary" key="outlets">
<bool key="EncodedWithXMLCoder">YES</bool>
<object class="NSArray" key="dict.sortedKeys">
<bool key="EncodedWithXMLCoder">YES</bool>
<string>button</string>
<string>entry</string>
<string>playController</string>
<string>progress</string>
@ -636,6 +701,7 @@
<string>id</string>
<string>id</string>
<string>id</string>
<string>id</string>
</object>
</object>
<object class="IBClassDescriptionSource" key="sourceIdentifier">

16
StreamingAudio/MainWindow.xib.designer.cs сгенерированный
Просмотреть файл

@ -19,6 +19,12 @@ namespace StreamingAudio {
[MonoTouch.Foundation.Export("startPlayback:")]
partial void startPlayback (MonoTouch.UIKit.UIButton sender);
[MonoTouch.Foundation.Export("playControlClicked:")]
partial void playControlClicked (MonoTouch.UIKit.UIButton sender);
[MonoTouch.Foundation.Export("volumeSet:")]
partial void volumeSet (MonoTouch.UIKit.UISlider sender);
[MonoTouch.Foundation.Connect("window")]
private MonoTouch.UIKit.UIWindow window {
get {
@ -88,5 +94,15 @@ namespace StreamingAudio {
this.SetNativeField("status", value);
}
}
[MonoTouch.Foundation.Connect("button")]
private MonoTouch.UIKit.UIButton button {
get {
return ((MonoTouch.UIKit.UIButton)(this.GetNativeField("button")));
}
set {
this.SetNativeField("button", value);
}
}
}
}

Просмотреть файл

@ -40,11 +40,25 @@ namespace StreamingAudio
int packetsFilled;
AudioStreamPacketDescription [] pdesc = new AudioStreamPacketDescription [512];
public void Pause ()
{
OutputQueue.Pause ();
}
public void Play ()
{
OutputQueue.Start ();
}
public StreamingPlayback ()
{
fileStream = new AudioFileStream (AudioFileType.MP3);
fileStream.PacketDecoded += AudioPacketDecoded;
fileStream.PropertyFound += AudioPropertyFound;
// The following line prevents the audio from stopping
// when the device autolocks.
AudioSession.Category = AudioSessionCategory.MediaPlayback;
}
public void ParseBytes (byte [] buffer, int count, bool discontinuity)
@ -62,6 +76,11 @@ namespace StreamingAudio
{
// Release unmanaged buffers, flush output, close files.
if (disposing){
AudioSession.Category = AudioSessionCategory.SoloAmbientSound;
if (OutputQueue != null)
OutputQueue.Stop (false);
if (outputBuffers != null)
foreach (var b in outputBuffers)
OutputQueue.FreeBuffer (b);
@ -70,7 +89,7 @@ namespace StreamingAudio
fileStream.Close ();
fileStream = null;
}
if (OutputQueue != null){
if (OutputQueue != null){
OutputQueue.Dispose ();
OutputQueue = null;
}
@ -111,7 +130,6 @@ namespace StreamingAudio
{
EnqueueBuffer ();
OutputQueue.Flush ();
OutputQueue.Stop (false);
// Release resources
Dispose ();