Okay, I think I've got this reliable - at least it's not crashed with
150+ button pushes on my last few goes.

For reference

The AudioSession code is required to get volume control working.
Without that, my volume buttons change the ringer volume, not the
media volume.

I have to call AudioSession.SetActive(true); every time I create an
AVAudioPlayer, otherwise I risk an AudioSessionException with message
"Application Audio Session is not active".

I removed the anonymous method from FinishedPlaying, and instead do
the following.

I have a class-scoped List<AVAudioPlayer> called toDispose.

On button press:

{
    // call Play on a freshly instantiated AVAudioPlayer ap:
    var ap = AVAudioPlayer.FromUrl(url, out err);
    ap.Play();

    // next I check to see if toDispose has a bunch of AVAudioPlayers
piled up, and if so, dispose all but the last couple in the list (my
samples are only 0.15 seconds long so it's unlikely I'll dispose one
that's currently playing this way)
    // code snipped for brevity

    // add the current AVAudioPlayer to the list of players to keep track of
    toDispose.Add(ap);
}

then finally in ViewWillDisappear() I dispose any remaining
AVAudioPlayers in toDispose, and remove the disposed player from the
list.

Horrible but stable with many short samples being played, unlike the
sample code on the MonoTouch website.

Thanks for the suggestions.
Tom

On Tue, Jul 19, 2011 at 1:07 AM, David Moles <[email protected]> wrote:
> FWIW, I'm using AVAudioPlayer.FromUrl() and it works for me. I'm not
> disposing on FinishedPlaying, though, I've got separate code that calls Stop
> and then Dispose. I'm also not using AudioSession at all.
> Note though that I'm still on MT 3.2.x, if that makes a difference.
>
> On Mon, Jul 18, 2011 at 3:18 PM, Tom Fanning <[email protected]> wrote:
>>
>> Weird one this.
>>
>> In a ViewController I call the following code in ViewDidLoad():
>>
>> AudioSession.Initialize();
>> AudioSession.Category = AudioSessionCategory.MediaPlayback;
>> AudioSession.SetActive(true);
>>
>> Then in response to some button presses I do:
>>
>> NSError err;
>> var ap = AVAudioPlayer.FromUrl(new NSUrl("mysound.caf"), out err);
>> ap.FinishedPlaying += delegate { ap.Dispose(); };
>> ap.Play();
>>
>> And this works great, pretty responsive etc.
>>
>> Except sometimes, seemingly randomly, I get the following unhandled
>> exception in release mode on the device (iPhone 3GS):
>>
>> System.MissingMethodException: No constructor found for
>> MonoTouch.AVFoundation.InternalAVAudioPlayerDelegate::.ctor(System.IntPtr)
>>
>> Any ideas? Google comes up blank for that specific missing constructor.
>>
>> Thanks
>> Tom
>>
>> --
>> Tom Fanning
>> _______________________________________________
>> MonoTouch mailing list
>> [email protected]
>> http://lists.ximian.com/mailman/listinfo/monotouch
>
>
>
> --
> David Moles
> [email protected]
>



-- 
Tom Fanning
_______________________________________________
MonoTouch mailing list
[email protected]
http://lists.ximian.com/mailman/listinfo/monotouch

Reply via email to