Hi Tom,

I had similar problems with that code fragment, I had to use AVAudioPlayer for 
exactly the same reason (volume control) and found that some combination of the 
GC, creating additional objects and my handling of the FinishedPlaying event 
would cause the same crash.  I only have 5 sounds I play so I finally ended up 
punting on the issue and holding hard references to each of the players for the 
lifetime of the application (as I play the sounds periodically throughout).  
That solved my problem.

mark

Mark Smith

___________________________________________________

.NET Solutions Mentor & Consultant
DEVELOPMENTOR Instructor and Course Author         

http://www.julmar.com   Twitter: marksm
Blog: http://www.julmar.com/blog/mark
___________________________________________________






On Jul 19, 2011, at 5:28 AM, Tom Fanning wrote:

> 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

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

Reply via email to