title: how to change the volume of the media player via main activity
body: <p>I use c# xamarin and I ran into a problem, I use backGround music and I want to control its volume with a seek bar, but I have a couple of problems, the music is an Intent.
  and I have written a method in the background music class to set the volume, but I can't access it because the music is an intent.
  here is the background music class:</p>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  
  using Android.App;
  using Android.Content;
  using Android.Media;
  using Android.OS;
  using Android.Runtime;
  using Android.Views;
  using Android.Widget;
  
  namespace Arkanoid
  {
      [Service]
  
      class backgroundMusic : Service
      {
          public ISharedPreferences sp;
          private MediaPlayer mp;
          public override IBinder OnBind(Intent intent)
          {
              return null;
          }
          public void SetVolume(int vol)
          {
              mp.SetVolume(vol, vol);
          }
          public override void OnCreate()
          {
              sp = this.GetSharedPreferences(&quot;details&quot;, FileCreationMode.Private);
              int volume = sp.GetInt(&quot;backgroundMusic&quot;, 50);
              base.OnCreate();
              mp = MediaPlayer.Create(this, Resource.Raw.backgroundMusic);
              mp.Looping = true;
              mp.SetVolume(volume, volume);
          }
          [return: GeneratedEnum]
          public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)
          {
              int volume = sp.GetInt(&quot;backgroundMusic&quot;, 50);
              mp.SetVolume(((float)volume)/100, ((float)volume)/100);
              int position = sp.GetInt(&quot;position&quot;, 0);
              mp.SeekTo(position);
              mp.Start();
              return base.OnStartCommand(intent, flags, startId);
          }
          public override void OnDestroy()
          {
              ISharedPreferencesEditor editor = sp.Edit();
              editor.PutInt(&quot;position&quot;, mp.CurrentPosition);
              editor.Commit();
              mp.Stop();
              mp.Release();
              base.OnDestroy();
          }
  
      }
  }
  </code></pre>
  <p>here is the way I start the music, in the main activity, inside OnCreate</p>
  <pre><code>         bMusic = new Intent(this, typeof(backgroundMusic));
           StartService(bMusic);
  </code></pre>
  <p>and here is the problem:</p>
  <pre><code> private void Music_ProgressChanged(object sender, SeekBar.ProgressChangedEventArgs e)
          {
              vMusic = e.Progress;
               //missing code here
          }
  </code></pre>
  <p>where I wrote &quot;missing code here&quot; is where I need to change the volume of the music.
  also, because I wrote the StartService(bMusic) in the OnCreate, it restarts the song every time i go to on create, which I don't want, I want it to keep on running as long as the app is running, and only me changing the volume via the seek bar.</p>
  <p>thanks in advance.</p>