/**
 * Copyright 2012 The PlayN Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package playn.ios;

import cli.MonoTouch.AVFoundation.AVAudioPlayer;
import cli.MonoTouch.Foundation.NSError;
import cli.MonoTouch.Foundation.NSUrl;
import playn.core.Asserts;
import playn.core.PlayN;
import playn.core.ResourceCallback;

import playn.core.Sound;

/**
 * An implementation of Sound using the AVAudioPlayer.
 */
class IOSSound implements Sound
{
  private AVAudioPlayer player;

  public IOSSound (String path) {
    NSError[] error = new NSError[1];
    player = AVAudioPlayer.FromUrl(NSUrl.FromFilename(path), error);
    if (error[0] != null) {
        PlayN.log().warn("Error loading sound [" + path + ", " + error[0] + "]");
        return;
    }
    player.PrepareToPlay();
  }

  @Override
  public boolean play() {
    Asserts.check(player != null, "Playing a null player");
    player.set_CurrentTime(0);
    return player.Play();
  }

  @Override
  public void stop() {
    Asserts.check(player != null, "Stopping a null player");
    player.Pause();
    player.set_CurrentTime(0);
  }

  @Override
  public void setLooping(boolean looping) {
    Asserts.check(player != null, "Setting looping on a null player");
    player.set_NumberOfLoops(looping ? -1 : 0);
  }

  @Override
  public void setVolume(float volume) {
    Asserts.check(player != null, "Setting volume on a null player");
    player.set_Volume(volume);
  }

  @Override
  public boolean isPlaying() {
    return player != null && player.get_Playing();
  }

  @Override
  public void addCallback(ResourceCallback<? super Sound> callback) {
    if (player != null) {
      // non-null players are always ready
      callback.done(this);
    }
  }

  public void dispose() {
    if (player != null) {
      player.Dispose();
      player = null;
    }
  }
}

@@ -1160,18 +1160,16 @@
 %7B%0A      
-  
 PlayN.lo
@@ -1234,18 +1234,16 @@
 + %22%5D%22);%0A
-  
       re
@@ -1329,37 +1329,27 @@
 ) %7B%0A    
-Asserts.check
+if 
 (player 
 != null,
@@ -1344,41 +1344,29 @@
 yer 
-!
+=
 = null
-, %22Playing a null player%22)
+) return false
 ;%0A  
@@ -1460,37 +1460,27 @@
 top() %7B%0A    
-Asserts.check
+if 
 (player != n
@@ -1486,37 +1486,14 @@
 null
-, %22Stopping a null player%22);%0A
+) %7B%0A  
     
@@ -1504,24 +1504,26 @@
 er.Pause();%0A
+  
     player.s
@@ -1537,24 +1537,30 @@
 entTime(0);%0A
+    %7D%0A
   %7D%0A%0A  @Over
@@ -1608,37 +1608,27 @@
 ping) %7B%0A    
-Asserts.check
+if 
 (player != n
@@ -1634,50 +1634,9 @@
 null
-, %22Setting looping on a null player%22);%0A   
+)
  pla
@@ -1741,21 +1741,11 @@
     
-Asserts.check
+if 
 (pla
@@ -1759,49 +1759,9 @@
 null
-, %22Setting volume on a null player%22);%0A   
+)
  pla
@@ -1974,36 +1974,8 @@
 ) %7B%0A
-    if (player != null) %7B%0A  
     
@@ -2015,17 +2015,35 @@
 ady%0A    
- 
+if (player != null)
  callbac
@@ -2056,22 +2056,16 @@
 (this);%0A
-    %7D%0A
   %7D%0A%0A  p

Stop blowing up when an attempt is made to play a sound that didn't load properly.