question

Brickman7713-3172 avatar image
0 Votes"
Brickman7713-3172 asked ·

Android Audio Streaming controlled by Forms Project View

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.

dotnet-csharpdotnet-xamarinformsdotnet-android
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

LeonLu-MSFT avatar image
1 Vote"
LeonLu-MSFT answered ·

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.


· 16 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you for the response! I think I am missing something though, the app builds and deploys successfully but when I click the play button I get this exception error and VS goes into break mode.

Exception has been thrown by the target of an invocation.'

0 Votes 0 ·
LeonLu-MSFT avatar image LeonLu-MSFT Brickman7713-3172 ·

You cannot get ID in the OnCreate method, because the MainActivity.cs do not have SetContentView(Resource.Layout.activity_main);, you can delete it, use MessagingCenter like my above code.

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

I used your code above and that is what caused the exception error.

0 Votes 0 ·
Show more comments