Forum Xamarin.Android

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

How to turn off music when the app is run in the background?

BayuBayu Member ✭✭✭

I make the simple app with the music background.

when I pressed the home button (red color), the music still turn on.

how to make the music is turn off when I pressed the home button?

this is my main activity code

using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Widget;
using Xamarin.Forms;
using Android.Media;

namespace MC
{
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
MediaPlayer player;
TextView txt;
Android.Widget.Button np;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
txt = FindViewById(Resource.Id.music);
np = FindViewById<Android.Widget.Button>(Resource.Id.nextpage);
player = MediaPlayer.Create(this, Resource.Raw.a);

        np.Click += delegate
        {
            StartActivity(typeof(page2));
        };


        MessagingCenter.Subscribe<object>(this, "Hi", (sender) =>
        {
            player.Start();
        });
        MessagingCenter.Subscribe<object>(this, "Hallo", (sender) =>
        {
            player.Pause();
        });


    }
    protected override void OnStart()
    {
        base.OnStart();
        player.Start();
    }
    public virtual void OnSleep ()
    {
        player.Pause();
    }

}

}

Best Answer

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    How to turn off music when the app is run in the background?

    In fact, you can turn off the music by override method OnStop and add the code of turning off music into it.

         protected override void OnStop()
            {
                base.OnStop();
            }
    

    From the official document OnStop of activity-lifecycle , we know:

    OnStop is called when the activity is no longer visible to the user. This happens when one of the following occurs:

    A new activity is being started and is covering up this activity.
    An existing activity is being brought to the foreground.
    The activity is being destroyed.

  • BayuBayu Member ✭✭✭

    My apologies @jezh , I forgot to say that I have several pages, if I write the code

    protected override void OnStop()
    {
    base.OnStop();
    player.Stop();
    }

    the music is turn off when I go to the next page, how about it? thank you

  • jezhjezh Member, Xamarin Team Xamurai

    If you want the music player run in the background ( e.g. when you navigation between different pages) , you can use a Service to achieve this.
    For more details, you can check android document: Using MediaPlayer in a service
    . And you can refer James's article Background Audio Streaming with Xamarin.Android
    There is also a sample in this link, you can check it ,it should be helpful for you.

  • jezhjezh Member, Xamarin Team Xamurai

    Hey friend @Bayu have you resolved your question?

  • BayuBayu Member ✭✭✭

    I'm sorry, I'm still trying to understand it, I'm really a beginner in this field.

    maybe, @jezh can give me a simpler example by making an app that contains 2-3 pages with background music and the background service.

  • BayuBayu Member ✭✭✭

    okay, thank you @jezh ,

Sign In or Register to comment.