Shaz
Shaz

Reputation: 2704

Monotouch sound doesn't play properly

I'm trying to play a sound file in monotouch but it gets cut off after a second or two. Here is the code I'm using to play the sound:

SoundFileName = filename; 
var sound = SystemSound.FromFile(filename);
sound.PlaySystemSound();

It's an MP3 file that I'm trying to play. Again, I hear it for a brief second and then it gets cut. I added a thread.sleep() line afterwards and THEN it'll play throughout.

But that's not ideal because the length of the mp3 files that I'm playing could vary. Any help would be greatly appreciated. Thank you.

Upvotes: 4

Views: 714

Answers (2)

Jason
Jason

Reputation: 89127

You may also want to look at this question: Playing a sound with MonoTouch

I don't think SystemSound is intended to use with MP3's of open duration.

Upvotes: 0

MusiGenesis
MusiGenesis

Reputation: 75296

You need to declare your sound object at the form or class level. In your posted sample, sound is only scoped to the function, so as soon as the method ends the variable goes out of scope and is disposed before the sound finished playing. Thread.sleep works because that call prevents the method from ending for a while (and thus prevents sound from going out of scope).

Upvotes: 6

Related Questions