Android Audio Streaming controlled by Forms Project View

Brickman7713 21 Reputation points
2021-02-24T03:04:45.83+00:00

Hello, so I have a Xamarin Forms app with Android and iOS projects inside it along with the main project where the pages are. I am trying to setup a live audio stream using this GitHub repo, but I am having trouble triggering the start from the main project. In the repo everything was done in an Android project. Thanks!

https://github.com/jamesmontemagno/AndroidStreamingAudio/tree/master/Part%203%20-%20MediaSessionCompat/Resources

MainPage.xaml (in TheLight)

<?xml version="1.0" encoding="utf-8" ?>

<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             BackgroundColor="White"
             x:Class="TheLight.MainPage">

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid Grid.Row="0">
            <StackLayout Margin="0, 0, 0, 0" 
                         Padding="{OnPlatform Android='10, 15', iOS='10, 40, 10, 15'}"
                         Orientation="Horizontal"
                         BackgroundColor="#991b1e"
                         Spacing="15">
                <Image Source="menu"
                       VerticalOptions="CenterAndExpand">
                    <Image.GestureRecognizers>
                        <TapGestureRecognizer Tapped="OnMenuTapped" />
                    </Image.GestureRecognizers>
                </Image>
                <Label Text="Home"
                       TextColor="White"
                       FontSize="Title"
                       FontAttributes="Bold"
                       VerticalOptions="CenterAndExpand"
                       VerticalTextAlignment="Center"/>
            </StackLayout>
        </Grid>

        <StackLayout Grid.Row="1" Padding="10, 0, 10, 25">
            <StackLayout.Resources>
                <ResourceDictionary>
                    <Style TargetType="Image">
                        <Setter Property="Margin" Value="10,0"/>
                        <Setter Property="WidthRequest" Value="70"/>
                    </Style>
                </ResourceDictionary>
            </StackLayout.Resources>

            <Image Source="https://thelightapp.apex-sites.com/uploads/xml/WQLU_FM_00.jpg" WidthRequest="500" Margin="0, 20, 0, 10"></Image>
            <Label x:Name="SongInfo" Text="90.9 The Light" HorizontalOptions="CenterAndExpand" Margin="0, 0, 0, 10" FontSize="Title" FontFamily="Roboto" TextColor="Black"/>
            <Grid HorizontalOptions="CenterAndExpand">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>



                <Image x:Name="droidPauseBtn" Source="pauseBtn.png" Grid.Column="0">
                    <Image.GestureRecognizers>
                        <TapGestureRecognizer Tapped="Pause" />
                    </Image.GestureRecognizers>
                </Image>

                <Image x:Name="droidStartBtn" Source="playBtn.png" Grid.Column="1" WidthRequest="100">
                    <Image.GestureRecognizers>
                        <TapGestureRecognizer Tapped="Play" />
                    </Image.GestureRecognizers>
                </Image>

                <Image x:Name="droidStopBtn" Source="stopBtn.png" Grid.Column="2">
                    <Image.GestureRecognizers>
                        <TapGestureRecognizer Tapped="Stop" />
                    </Image.GestureRecognizers>
                </Image>

            </Grid> 

        </StackLayout>

    </Grid>
</ContentView>

MainPage.xaml.cs (in TheLight)

using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using Firebase.Database;  
using Firebase.Database.Query;
using System.Net.Mail;
using System.Xml.Linq;
using System.Linq;
using System.Xml;
using System.IO;
using Android.Content;


namespace TheLight
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class MainPage : ContentView
    {
        [Obsolete]
        public MainPage()
        {
            InitializeComponent();

            if (Device.OS == TargetPlatform.Android)
            {
                droidStopBtn.IsVisible = true;
                droidStartBtn.IsVisible = true;
                droidPauseBtn.IsVisible = true;
            }
            else
            {
                droidStopBtn.IsVisible = false;
                droidStartBtn.IsVisible = false;
                droidPauseBtn.IsVisible = false;
            }
        }

        public event EventHandler ToggleMenu;

        void OnMenuTapped(object sender, System.EventArgs e)
        {
            ToggleMenu?.Invoke(sender, e);
        }

        private void Play(object sender, System.EventArgs e)
        {
            DependencyService.Get<IStreaming>().Play();
            //IsPlaying = true;
        }

        private void Stop(object sender, System.EventArgs e)
        {
            DependencyService.Get<IStreaming>().Stop();
            //IsPlaying = false;
        }

        private void Pause(object sender, System.EventArgs e)
        {
            DependencyService.Get<IStreaming>().Pause();
            //IsPlaying = false;
        }

    }
}

MainActivity.cs (in TheLight.Android)

using System;

using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Content;
using Xamarin.Forms;

namespace TheLight.Droid
{
    [Activity(Label = "The Light", Icon = "@mipmap/icon", Theme = "@style/MainTheme", ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;

            base.OnCreate(savedInstanceState);

            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
            LoadApplication(new App());

            var play = FindViewById<Button>(Resource.Id.playButton);
            var pause = FindViewById<Button>(Resource.Id.pauseBtn);
            var stop = FindViewById<Button>(Resource.Id.stopBtn);

            play.Click += (sender, args) => SendAudioCommand(StreamingBackgroundService.ActionPlay);
            pause.Click += (sender, args) => SendAudioCommand(StreamingBackgroundService.ActionPause);
            stop.Click += (sender, args) => SendAudioCommand(StreamingBackgroundService.ActionStop);
        }

        private void SendAudioCommand(string action)
        {
            var intent = new Intent(action);
            StartService(intent);
        }

        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
        {
            Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }

    }
}

I also added a Services folder and Receivers folder in TheLight.Android and added the files StreamingBackgroundService.cs and MusicBroadcastReceiver.cs.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,293 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,234 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 68,491 Reputation points Microsoft Vendor
    2021-02-24T07:25:02.023+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    You can use MessageCenter to control it.

    In the Xamarin Forms background code. When click the button, you can send a Message,

       MessagingCenter.Send<App, string>((App)App.Current, "ActionPlay", "");  
    

    then you can Subscribe the message in the OnCreate() of MainActivity.cs

       Xamarin.Forms.MessagingCenter.Subscribe<App, string>(App.Current, "ActionPlay", (snd, arg) =>  
                   {  
                        SendAudioCommand(StreamingBackgroundService.ActionPlay);  
                   });  
    

    For Pauseand Stop function, you can add two MessageCenter to achieve it.

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.