• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Help with mediaplayer and release

kabal3

Lurker
Hi here is my code, I want to press buttons that each play a sound but I'm having trouble releasing mediaplayer when I'm done. So eventually too many instances are open and sound crashes, here is my code:


using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Media;


namespace soundplayer

{
[Activity (Label = "soundplayer", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
MediaPlayer mediaPlayer;

protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);

// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);

Button button1 = FindViewById<Button> (Resource.Id.button1);
Button button2 = FindViewById<Button> (Resource.Id.button2);
button1.Click += (object sender, EventArgs e) => {
mediaPlayer = MediaPlayer.Create (this, Resource.Raw.sound01);
mediaPlayer.Start ();
};

button2.Click += (object sender, EventArgs e) => {
mediaPlayer = MediaPlayer.Create (this, Resource.Raw.sound02);
mediaPlayer.Start ();
};
}


public void onStop(){
mediaPlayer.Stop ();
mediaPlayer.Release();
mediaPlayer = null;
}

}

}

-----------------------------
I tried stepping through my program and noticed that after mediaPlayer.Start (); my program never bothers to access onStop. Is there a way to make it do that or another solution to my problem. Thank you for your time
 
Back
Top Bottom