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 make mute button if I have several page in the Project?

BayuBayu Member ✭✭✭

I have several page in the project, i put the Mediaplayer as background sound in the MainActivity, below is my code:

using Android.App;
using Android.Widget;
using Android.OS;
using Android.Support.V7.App;
using System;
using Android.Media;

namespace Akuuu
{
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true, Icon = "@drawable/Icon")]
public class MainActivity : AppCompatActivity
{
MediaPlayer player;

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

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.activity_main);
        player = MediaPlayer.Create(this, Resource.Raw.a);
        player.Looping = true;                       



        var pnj = FindViewById<Button>(Resource.Id.penjumlahan);
        var pnr = FindViewById<Button>(Resource.Id.pengurangan);
        var per = FindViewById<Button>(Resource.Id.perkalian);
        var pem = FindViewById<Button>(Resource.Id.pembagian);

        pnj.Click += delegate {
            StartActivity(typeof(A));
        };
        pnr.Click += delegate {
            StartActivity(typeof(B));
        };
        per.Click += delegate {
            StartActivity(typeof(C));
        };
        pem.Click += delegate {
            StartActivity(typeof(D));
        };
    }
    protected override void OnStart()
    {
        base.OnStart();
        player.Start();
    }
    protected override void OnDestroy()
    {
        base.OnDestroy();
        player.Stop();
    }

}

}

if I have a mute button in page two, how to connect it to mediaplayer in page 1 ? thanks

Best Answer

Answers

  • BayuBayu Member ✭✭✭

    I'm sorry @nikka, @GuyProvost, @FredyWenger , @JoeManke , can you help me?

  • BayuBayu Member ✭✭✭
  • ColeXColeX Member, Xamarin Team Xamurai
    edited April 2020

    As it mentioned in the link , use messaging center ..

    Send message in button click event in page 2 , and subscribe message in page 1 .

  • BayuBayu Member ✭✭✭

    thank you @ColeX , I am sorry, I am a beginner at Xamarin android,
    may I ask you to how to code message center, step by step?

  • ColeXColeX Member, Xamarin Team Xamurai
    edited April 2020

    Page2

    button.Click += delegate {
            MessagingCenter.Send<object>(this, "Hi");
        };
    

    Page1

     protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
    
          MessagingCenter.Subscribe<object> (this, "Hi", (sender) =>
             {
                 // Do something whenever the "Hi" message is received
             });
    
        }
    

    Check https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/messaging-center .

  • BayuBayu Member ✭✭✭

    Thank you @ColeX , I have next problem in Messaging center.
    I got this error when I use the Mess
    aging center

    I am sorry, how to fix this?

  • ColeXColeX Member, Xamarin Team Xamurai

    Messaging center belongs to Xamarin.Forms package , you have to install it first .

  • BayuBayu Member ✭✭✭
    edited April 2020

    thank you, Iam sorry, I got the next error, like this,

    How to fix it? thank you so much, @ColeX

  • ColeXColeX Member, Xamarin Team Xamurai

    Change target in Settings (right click your solution, click into properties).

  • BayuBayu Member ✭✭✭
    edited April 2020

    I don't have android 9.0 in my SDK, , how to install android 9.0? @ColeX

  • ColeXColeX Member, Xamarin Team Xamurai

  • BayuBayu Member ✭✭✭

    My VB version is VB2017 15.7.2 and there is not android 9.0 and 10.0 in there, how about it? @ColeX

  • ColeXColeX Member, Xamarin Team Xamurai

    Try to select Google as repository .

  • BayuBayu Member ✭✭✭

    this is my google unsupported repository.

  • BayuBayu Member ✭✭✭

    okay thank you so much, @ColeX , I will try it.

  • BayuBayu Member ✭✭✭

    Thank you @ColeX, I installed vs 2019 community, and it work properly,

    thank you for the help, nice discussion.

Sign In or Register to comment.