Ok So I watched this video and it showed me how to do background audio in ios but how does one do the same using the mediaelement control in andriod does anybody have a solution.
https://www.youtube.com/watch?v=1XVjsUO-Ayw
<xct:MediaElement x:Name="mediaElement"
Grid.Row="0"
HeightRequest="500"
AutoPlay="True"
Volume="1"
ShowsPlaybackControls="True"
VerticalOptions="FillAndExpand"
/>
In IOS am placing
public void EnableBackgroundAudioPlaying()
{
var currentStatus = AVAudioSession.SharedInstance();
currentStatus.SetCategory(AVAudioSessionCategory.Playback);
currentStatus.SetActive(true);
}
But when I USE the android simulator the audio disconnects when i press the home button.
Related
had try to add google admob ads to my projects but Ads does not appear in my project.
could you guide me or give me an example of how to implement it correctly?
I have tried the MarcTron.Admob nuget however it does not work with android target aversion >12 or API level >31.
I am using:
Android -
Visual Studio for Windows -
Operating System Windows 10 -
Xamarin.Forms Version 5.0.0.2478 -
MarcTron.Admob 1.9.0.3
my AdmobSample with
MarcTron.Admob 1.9.0.3
looks like this:
in XAML
<Frame x:Name="adsFrame" IsVisible="false" HeightRequest="50" BackgroundColor="Red">
<controls:MTAdView
x:Name="myAds"
AdsClicked="MyAds_AdsClicked"
AdsId="ca-app-pub-3940256099942544/6300978111"
IsVisible="false"
VerticalOptions="EndAndExpand"
HeightRequest="200"
HorizontalOptions="CenterAndExpand"
AdsLoaded="Ads_AdsLoaded" AdsFailedToLoad="myAds_AdsFailedToLoad">
</controls:MTAdView>
</Frame>
in MainPage.xaml.cs
private void Ads_AdsLoaded(object sender, EventArgs e)
{
MainThread.BeginInvokeOnMainThread(async () =>
{
await Task.Delay(3000);
adsFrame.IsVisible = true;
await Task.Delay(1000);
myAds.IsVisible = true;
});
}
there is more help here
https://github.com/marcojak/MTAdmob/issues/89
I want to substitute XCT:MediaElement with Plugin.MediaManger & Plugin.MediaManager.Forms for my videos to play cross-platform. Documentation I've read this like 5 times and it still doesn't make any sense. I'm putting my video into Assets in Android and using XAML code below. The picture is the output I get on the screen, it's just a black background which I can change to any color...
I've added the Init() to the MainActivity and also to the AppDelegate. Followed everything step by step.
What really doesn't make any sense from the documentation is (Examples). CrossMediaManager is a static class. I cannot create a new instance of static classes so I cannot actually assign anything to the layout. How does it suppose to play anything on the screen if there is nothing that can actually consume this?
await CrossMediaManager.Current.PlayFromAssembly("somefile.mp3", typeof(BaseViewModel).Assembly);
await CrossMediaManager.Current.PlayFromResource("assets:///somefile.mp3");
await CrossMediaManager.Android.PlayFromResource(Resource.Raw.somefile.ToString());
Further, on the documentation you come across the Xamarin.Forms part where it shows how to do this is XAML with mm:ViewView now this makes more sense and I can also assign an x:Name to it and pass through MVVM pattern through the Behind Code to the ViewModel and so on, but this would be cool if the video played in the first place...
Any idea why this is happening?
Many Thanks in Advance.
Nuget Packges I've added: Plugin.MediaManager & Plugin.MediaManager.Forms
<StackLayout BackgroundColor="Red"
HorizontalOptions="CenterAndExpand"
VerticalOptions="CenterAndExpand"
WidthRequest="400"
HeightRequest="600"
Margin="10"
Padding="20">
<mm:VideoView VerticalOptions="FillAndExpand"
Source="assets:///pea2.mp4"
AutoPlay="True"
ShowControls="True" />
</StackLayout>
At first, I install the Plugin.MediaManger & Plugin.MediaManager.Forms two packages in all the project, such as PCL(forms part), android part and ios part.
And then I added the following code in the MainActivity:
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
CrossMediaManager.Current.Init(this);
CrossMediaManager.Current.AutoPlay = true;
LoadApplication(new App());
}
And in the MainPage.Xaml:
<StackLayout>
<mm:VideoView VideoAspect="AspectFit" VerticalOptions="FillAndExpand" Source="https://www.appsloveworld.com/wp-content/uploads/2018/10/sample-mp4-video.mp4" ShowControls="True"/>
</StackLayout>
And here is the result image on the android 11 emulator:
Note, according to the issue I mentioned in the comments, the plugin will work on the android 12 after adding the CrossMediaManager.Current.Speed = 1F;. But it will throw exception when I added it. It's strange.
I have a page for playing songs in my project. When I go back to the previous page after playing a song (without pausing it), it will continue to play in the background, and users can pause it at any time from the notification bar. I am using Plugin.MediaManager (Version: 1.1.1) for the integration of songs.
Current song screens on my App
But we have a new suggestion, like playing the songs in the app itself on top or bottom on all the other pages (like WhatsApp). Our application has more than 100 pages, so adding the audio player on all these pages is a tedious task. So any other tricky way to implement it on all the pages by reusing it or any other better solution for this feature?
The expected feature is like the below one, like WhatsApp
If you have a common TabbedPage such as the screenshot you attached of Whatsapp, you could add at the beginning of its xaml file before the definition of the views:
<NavigationPage.TitleView>
<StackLayout>
...
</StackLayout>
</NavigationPage.TitleView>
That way you will have a common Navigation view on top of all of them with custom data that you could modify in the associated xaml.cs file of that TabbedPage.
If you have a different structure for your views you could check the documentation for Control Templates in Xamarin Forms, and include it in your App.xaml.
Good luck!
We implemented this using a pop-up page.
XAML:
<StackLayout
HorizontalOptions="Center"
VerticalOptions="End">
<mm:VideoView
Grid.Column="0"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
AutoPlay="True"
IsVisible="True"
x:Name="audioplayer"
ShowControls="True">
<mm:VideoView.HeightRequest>
<OnIdiom x:TypeArguments="x:Double">
<OnIdiom.Phone>80</OnIdiom.Phone>
<OnIdiom.Tablet>120</OnIdiom.Tablet>
<OnIdiom.Desktop>80</OnIdiom.Desktop>
</OnIdiom>
</mm:VideoView.HeightRequest>
</mm:VideoView>
</StackLayout>
XAML.CS
public partial class AudioPopupPage : PopupPage
{
public AudioPopupPage(object source)
{
InitializeComponent();
if (source != null)
{
audioplayer.Source = source;
}
}
protected override bool OnBackgroundClicked()
{
BackgroundInputTransparent = true;
return false;
}
}
We call the pop-up page when we leave the song page.
protected override bool OnBackButtonPressed()
{
PopupNavigation.Instance.PushAsync(new Views.AudioPopupPage(CrossMediaManager.Current.State), true);
return base.OnBackButtonPressed();
}
With this approach, this audio player is showing on all pages, and it is working fine on all platforms.
I have a ContentPage (actually an MvxContentPage) with ToolbarItems defined. The Items appear in Android and iOS as expected, but do not appear at all in UWP. I have tried setting SetToolbarPlacement to both Top and Bottom manually, in both the constructor and the OnAppearing method. Thus far, I have not been able to affect any change in the UWP application. Am I doing something wrong? Can Mvx not render the Toolbar?
<mvx:MvxContentPage
xmlns:mvx="clr-namespace:MvvmCross.Forms.Views;assembly=MvvmCross.Forms"
x:TypeArguments="viewModels:CategoryListViewModel"
xmlns:viewModels=""
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="GSP.X.AccuStore.Forms.Views.Site.Profiles.CategoryListView">
<ContentPage.ToolbarItems>
<ToolbarItem Text="Areas" Command="{Binding Path=GoToAreasCommand}" />
</ContentPage.ToolbarItems>
</mvx:MvxContentPage>
The problem is you have not add start page to NavigationPage. I have test with the follow code in the blank Xamrin.Froms app, the ToolbarItem display well in the top of MainPage.
public App()
{
InitializeComponent();
var nav = new NavigationPage(new MainPage());
MainPage = nav;
}
My app has a WebView for displaying some contact information. It has a link to a website that I want to load externally using Device.OpenUri(). I'm using FreshMvvm and I want to intercept the Navigating event from the WebView in the ViewModel and cancel the default action which would load the external page into the WebView.
I've tried using the Corcav.Behaviors plugin which does call my ViewModel command:
<WebView
HorizontalOptions="Fill"
VerticalOptions="FillAndExpand"
Source="{Binding WebViewSource}">
<b:Interaction.Behaviors>
<b:BehaviorCollection>
<b:EventToCommand
EventName="Navigating"
Command="{Binding NavigatingCommand}"
CommandParameter="{Binding}"/> <!-- what goes here -->
</b:BehaviorCollection>
</b:Interaction.Behaviors>
</WebView>
But I'm not sure what the CommandParameter should be - I need the URI of the link that was tapped, and I don't know how to then prevent the default behaviour from occurring.
Is this the best approach or should I be looking at an alternative?
Having revisited this recently for another project I stumbled across the answer. The updated XAML is:
<WebView
x:Name="webView"
HorizontalOptions="Fill"
VerticalOptions="FillAndExpand"
Source="{Binding WebViewSource}">
<behaviors:Interaction.Behaviors>
<behaviors:BehaviorCollection>
<behaviors:EventToCommand
EventName="Navigating"
Command="{Binding NavigatingCommand}"
PassEventArgument="True" />
</behaviors:BehaviorCollection>
</behaviors:Interaction.Behaviors>
</WebView>
The code in the ViewModel, that matches the tapped url against a list of valid options before opening the link in the device's browser, is:
public Command<WebNavigatingEventArgs> NavigatingCommand
{
get
{
return navigatingCommand ?? (navigatingCommand = new Command<WebNavigatingEventArgs>(
(param) =>
{
if (param != null && -1 < Array.IndexOf(_uris, param.Url))
{
Device.OpenUri(new Uri(param.Url));
param.Cancel = true;
}
},
(param) => true
));
}
}
You can´t navigate with a WebView, you must use a custom render (hybridwebview).
Here is an explanation:
https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/custom-renderer/hybridwebview/