Sharing Media Playback Between Pages with Windows Phone 7 Media Player - c#

What I want to achieve:
I want to initiate audio playback from an mp3 and/or aac HTTP stream in a WP7 application
I want to initiate playback from a specific 'PhoneApplicationPage' instance, but still allow navigation to other pages whilst maintaining playback without any interuption - i.e. I want playback to be 'application-scope'
I want to be able to 'seek' within my media
I playback to continue whilst the phone is locked
What I have tried:
MediaElement:
If the MediaElement is not owned by a page, no sound is produced when Play() is called, despite no exceptions being thrown.
After following 'http://blog.jayway.com/2010/10/04/enable-background-audio-for-multiple-pages-in-windows-phone-7/', playback still resets between page transitions
It also seems like a quite a hacky way of doing things...
Microsoft.Xna.Framework.MediaPlayer:
Works, but "MediaPlayer.PlayPosition" is read-only, and there is no seek method.
See post: 'http://forums.create.msdn.com/forums/t/17318.aspx' - Apparently this is by design due to XBox constraints with Xna (?!)
Microsoft Silverlight Media Framework:
http://smf.codeplex.com/
My favourite option, as it seems very comprehensive
Downloaded 'Silverlight Media Framework 2.3, WP7 specific' assemblies from:
http://smf.codeplex.com/releases/view/57991#DownloadId=190196
I know this is hacky, but to get something working, in the code below, the 'SMFPlayer' is static, and added to each page's layout on navigation.
If the 'SMFPlayer' is not owned by a page, no sound is produced when Play() is called, despite no exceptions being thrown.
Playback still resets between page transitions...
Code:
using System;
using System.Diagnostics;
using Microsoft.Phone.Controls;
using Microsoft.SilverlightMediaFramework.Core;
using Microsoft.SilverlightMediaFramework.Core.Media;
using Microsoft.SilverlightMediaFramework.Plugins.Primitives;
namespace WindowsPhoneApplication1
{
public partial class MainPage : PhoneApplicationPage
{
public static readonly SMFPlayer Player = new SMFPlayer();
static MainPage()
{
Player.VolumeLevel = 1.0f;
Player.Playlist.Add(new PlaylistItem {MediaSource = new Uri("http://smf.vertigo.com/videos/wildlife.wmv", UriKind.Absolute)});
Player.LogLevel = LogLevel.All;
Player.LogEntryReceived += PlayerLogEntryReceived;
}
// Constructor
public MainPage()
{
InitializeComponent();
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
LayoutRoot.Children.Add(Player);
}
protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
{
base.OnNavigatingFrom(e);
LayoutRoot.Children.Remove(Player);
}
private static void PlayerLogEntryReceived(object sender, CustomEventArgs<LogEntry> e)
{
Debug.WriteLine(e.Value.Severity + e.Value.Message + e.Value.Type);
}
private void button1_Click(object sender, System.Windows.RoutedEventArgs e)
{
this.NavigationService.Navigate(new Uri("/Page1.xaml", UriKind.RelativeOrAbsolute));
}
}
}
Does anyone have any idea how I can satisfy my requirements?
Example code?
From an architectural point of view, what I really want is a Media Service which i can send streaming URLs to without caring about which page is currently shown.

I eventually found a simple, but effective solution:
http://blog.reis.se/post/Enable-background-audio-for-multiple-pages-in-Windows-Phone-7-e28093-Take-2.aspx
In App.xaml:
<APPLICATION.RESOURCES>
<MEDIAELEMENT x:key="GlobalMedia"></MEDIAELEMENT>
</APPLICATION.RESOURCES>
In App.xaml.cs:
public static MediaElement GlobalMediaElement
{
get { return Current.Resources["GlobalMedia"] as MediaElement; }
}
In your page:
public partial class MyPage : PhoneApplicationPage
{
MediaElement MEAudio;
public MainPage()
{
InitializeComponent();
MEAudio = App.GlobalMediaElement;
}
private void OnSomeEvent(object sender, RoutedEventArgs e)
{
MEAudio.xxxxx();

Related

Using System.Windows.Media API in a Winforms application results in weird UI behaviour

I am trying to use System.Windows.Media.Typeface to get some details of a font.
When I use it in my WinForms application the UI is resizing and the fonts are changing.
I noticed that my display resolution is at 125%. When I test this on 100% screen it does not happen.
Is there any issue of referencing PresentationCore.Dll in a winforms app. Or is
it about Typeface class.
namespace WindowsFormsApp1
{
public partial class WinFormample : Form
{
public WinFormample()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
System.Windows.Media.Typeface t = new System.Windows.Media.Typeface("Arial");
}
}
}

Main page with System.NotImplementedException error

I get a System.NotImplementedException error whenever I try to launch the page on the emulator that takes photographs. Whenever I attempt to take a photo with the emulator's camera, I get taken to the main page in the Xamarin Studio project that launches the user interface. I get the error:
System.NotImplementedException has been thrown
This functionality is not implemented in the portable version of this assembly. You should reference the NuGet package from your main application project in order to reference the platform-specific implementation.
Here is the code:
using UIKit;
namespace Relate.iOS
{
public class Application
{
// This is the main entry point of the application.
static void Main(string[] args)
{
/* if you want to use a different Application Delegate class
from "AppDelegate" you can specify it here. */
UIApplication.Main(args, null, "AppDelegate");
}
}
}
Can anyone help?
Here is the code for the camera. I added Media Plugin to my project.
using System;
using Relate.Model;
using Xamarin.Forms;
using Plugin.Media;
namespace Relate.Views
{
public partial class EditMemberPage : ContentPage
{
public EditMemberPage()
{
InitializeComponent();
saveButton.Clicked += async (sender, args) =>
{
if (CrossMedia.Current.IsCameraAvailable &&
CrossMedia.Current.IsTakePhotoSupported)
{
// Supply media options for saving our photo after
it's taken.
var mediaOptions = new
Plugin.Media.Abstractions.StoreCameraMediaOptions
{
Directory = "Receipts",
Name = $"{DateTime.UtcNow}.jpg"
};
// Take a photo of the business receipt.
var file = await
CrossMedia.Current.TakePhotoAsync(mediaOptions);
}
};
}
async void SaveButton_OnClicked(object sender, EventArgs e)
{
var famMemberItem = (FamMember)BindingContext;
await App.Database.SaveFamMemberAsync(famMemberItem);
await Navigation.PopAsync();
}
}
}
The answer here should explain why this is not working for you: https://forums.xamarin.com/discussion/93536/error-while-accessing-camera
You cannot access platform specific features using portable common libraries. If you want to access your emulators camera you'll have to use something like Media Plugin
https://github.com/jamesmontemagno/MediaPlugin

Zxing.net.mobile, cannot get an event when scanner is closed xamarin.forms

I'm using the Zxing.net.mobile library in my xamarin.forms application, but I've found a big limitation in the using of it.
When the barcode scanner is active and I press the back button, the UI returns to the previous page but there's no way to execute code, because there's no way to trigger an event or passing a callback function to the scanner and the OnAppearing() method doesn't fire. Does anyone know a solution that might work? Thank you!
Edit: example code
public partial class MyPage : ContentPage
public MyPage()
{
InitializeComponent();
}
public async void MyMethod()
{
var scanner = new MobileBarcodeScanner();
scanner.ScanContinuously(MyScanningAction);
}
private async void MyScanningAction(ZXing.Result scanningResult)
{
... do something ....
}

Handling suspend, resume, and activation in windows 10 UWP

In the windows 8.1 universal apps, the suspend/resume modes were handled using the NavigationHelper.cs ans SuspensionManager.cs classes included in the APP template. These classes doesn't seem to be there in the windows 10 UWP apps. Is there a way by which we can handle the suspend/resume states?
There's an interesting framework being developed by the community (but mostly I think Jerry Nixon, Andy Wigley etc.) called Template10. Template10 has a Bootstrapper class with OnSuspending and OnResuming virtual methods that you can override. I am not sure that there's an exact example of doing suspension/resuming yet with Template10, but the idea seems to be to make App.xaml.cs inherit from this Bootstrapper class so you can easily override the methods I mentioned.
sealed partial class App : Common.BootStrapper
{
public App()
{
InitializeComponent();
this.SplashFactory = (e) => null;
}
public override Task OnStartAsync(StartKind startKind, IActivatedEventArgs args)
{
// start the user experience
NavigationService.Navigate(typeof(Views.MainPage), "123");
return Task.FromResult<object>(null);
}
public override Task OnSuspendingAsync(object s, SuspendingEventArgs e)
{
// handle suspending
}
public override void OnResuming(object s, object e)
{
// handle resuming
}
}
The above solution will only work for people who install Template10.
The generic solution is,
paste these lines in the constructor of App.xaml.cs
this.LeavingBackground += App_LeavingBackground;
this.Resuming += App_Resuming;
It will look like this
public App()
{
this.InitializeComponent();
this.Suspending += OnSuspending;
this.LeavingBackground += App_LeavingBackground;
this.Resuming += App_Resuming;
}
These are the methods, although you can press TAB and they will autogenerate.
private void App_LeavingBackground(object sender, LeavingBackgroundEventArgs e)
{
}
private void App_Resuming(object sender, object e)
{
}
The methods LeavingBackground and the one not mentioned here EnteredBackground are newly added to uwp.
Before these methods we would use resuming and suspending to save and restore ui, but now the recommended place to do that work is here.Also these are the last places to perform work before the app is resumed. So the work on these methods should be small ui or other stuff like remaking values which are stale as a long held method here will affect app startup time while resuming.
Source
Windows dev material ,
Windoes dev material 2
Thanks , and have a good day.

MMVM Light NavigationService vs NavigationHelper in Universal App

I was wondering if someone could shed some light on how best handle the navigation service in a universal app as I'm confused on how to handle this.
If I create a blank application and just use a basic NavigationService as suggested in tutorial for mvvmlight and universal app, it doesn't handle the windows phone hardware back key and when I click it, it just closes the wp8.1 app.
I found an article that suggest to add the HardwareButtons.BackPressed to the app.cs, but I don't like the idea to be honest. Maybe it's ok? Let me know.
The basic IHavigationService is registered in the SimpleIocand is injected in via the constructor but as mentioned, this does not handle the back key for wp8 apps.
When I create a universal hub app, it doesn't use mvvmlight and it creates a navigationHelper class which contains the necessary code to handle both windows and wp apps but every time it is used, the declaration is done in the code behind of the relevant page rather than in the ViewModel.
Any suggestions on how best handle this?
Thanks.
You can combine the best of both world:
In App.xaml.cs, you subscribe to HardwareButtons.BackPressed event:
HardwareButtons.BackPressed += HardwareButtons_BackPressed;
private void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
if(NavigationService.CanGoBack)
{
NavigationService.GoBack();
e.Handled = true;
}
}
Create NavigationService class:
public static class NavigationService
{
public static Dictionary<Pages, Type> PageDictionary = new Dictionary<Pages, Type>();
public static Frame MainFrame;
public static void Configure(Frame frame)
{
PageDictionary.Add(Pages.MainPage, typeof(MainPage));
PageDictionary.Add(Pages.Setting, typeof(SettingPage));
MainFrame = frame;
}
internal static void GoBack()
{
if (MainFrame.CanGoBack)
MainFrame.GoBack();
}
internal static bool CanGoBack
{
get
{
return MainFrame.CanGoBack;
}
}
internal static void NavigateTo(Pages page, object parameter)
{
MainFrame.Navigate(PageDictionary[page], parameter);
}
}
Pages is an enum
In App.xaml.cs, after you initiate rootFrame, config the navigation service:
NavigationService.Configure(rootFrame);

Categories