Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Help with mediaplayer release()

kabalkabal AUMember

Hi 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 of mediaplayer 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 = 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


  • CheesebaronCheesebaron DKInsider, University mod

    Why would it enter onStop when you are not calling it?

  • kabalkabal AUMember

    I'm not sure when to call it.
    If I do it after start like:
    mediaPlayer.Start ();

    My audio file plays for half a second before stopping

  • ChristopherDrososChristopherDrosos GRMember ✭✭

    you want to be on stop state or something after the playback? Use .oncompletion()

  • CasperSkouboCasperSkoubo USUniversity, Developer Group Leader

    What is it you are trying to play? If its minor sounds on button clicks etc then you can use he SoundPool class.
    If its songs i would suggest to use the mediaplayer, but you really have to read up on it first. Its a hardware component, so it has its own states. If you don't follow these, you will not hear anything but only see your app crash at a point.

  • kabalkabal AUMember

    Can you tell me exactly how to use the oncompletion because I have read that elsewhere too but I'm very new to this

  • ChristopherDrososChristopherDrosos GRMember ✭✭

    check this blog post for an example

    player = new MediaPlayer();
    //Tell our player to stream music
    //When we have prepared the song start playback
    player.Prepared += (sender, args) => player.Start();
    //When we have reached the end of the song stop ourselves, however you could signal next track here.
    player.Completion += (sender, args) => Stop();
    player.Error += (sender, args) => {
      //playback error
      Console.WriteLine("Error in playback resetting: " + args.What);
      Stop();//this will clean up and reset properly.
  • JeremyKolbJeremyKolb USMember ✭✭✭

    Are you trying to override Activity's OnStop?

  • kabalkabal AUMember

    I just want to know how to release mediaplayer when the soundfile has finished playing

  • ChristopherDrososChristopherDrosos GRMember ✭✭

    How about something like this:

    player.Completion += (sender, args) => {
    if (!player.IsPlaying){
    player = null;

  • kabalkabal AUMember

    Where do I put that

  • CasperSkouboCasperSkoubo USUniversity, Developer Group Leader

    You need to add it to the mediaplayer you want to get notified from when its complete. So in your case:

    button1.Click += (object sender, EventArgs e) => { mediaPlayer = MediaPlayer.Create (this, Resource.Raw.sound01); mediaPlayer.Completion += (sender, args) => { if (!player.IsPlaying){ player.Release(); player = null; } }; mediaPlayer.Start (); };

  • ChristopherDrososChristopherDrosos GRMember ✭✭

    Do what the error suggest then.. use (object sender, EventArgs e)

Sign In or Register to comment.