Windows Phone 7 - Playing streaming video - windows-phone-7

On WP7 platform (using C# and Silverlight) I try to play an online stream into a MediaElement...
Here is the C# code:
(...)
WebClient wc = new WebClient();
wc.OpenReadCompleted += (s, e) =>
{
try
{
mediaElement.SetSource(e.Result);
}
catch (Exception we)
{
System.Diagnostics.Debug.WriteLine(we.Message);
}
};
wc.OpenReadAsync(new Uri(url, UriKind.Absolute));
(...)
Here is the XAML source code:
<MediaElement Height="316" HorizontalAlignment="Left" Margin="6,6,0,0" Name="mediaElement" VerticalAlignment="Top" Width="450" AutoPlay="False" />
The url is type of http://.../Manifest and the format is a one supported by the platform.
When SetSource is called then an exception is raised with the following message
"Stream must be of type IsolatedStorageFileStream".
What do I do wrong?
Thanks in advance for some help
Cheers

MSDN says:
Passing a generic stream to SetSource(System.IO.Stream) is not supported in Silverlight for Windows Phone. However, the IsolatedStorageFileStream class, which derives from Stream, is suppoted on Silverlight for Windows Phone.
Instead, consider setting the MediaElement.Source property directly to the stream uri. There's no reason to "download" it first.

Related

Image From Music Library Not Showing in UWP app

I'm new to UWP development & I'm just showing image in my app from the Music Library.
Infact, I have added Music Library in the app's "Capabilities" & I can confirm that I have access to Music Library as I can read & write files in it.
But when I try to load a image in XAML, it just does not shows...
<Image Height="200" Width="200" Source="C:/Users/Alex Mercer/Music/Album/albumArt.png" />
Please help me understand & solve the problem.
😃 Thanks a lot!
Although we enable the corresponding capabilities, accessing files through paths is still strictly restricted in UWP.
In fact, it is not a good idea to write the full path in XAML, because you cannot guarantee that the path must exist on the device where the application is installed.
To display the pictures in the music library, you can do this:
xaml
<Image Height="200" Width="200" x:Name="AlbumImage" Loaded="AlbumImage_Loaded"/>
xaml.cs
private async void AlbumImage_Loaded(object sender, RoutedEventArgs e)
{
try
{
var albumFolder = await KnownFolders.MusicLibrary.GetFolderAsync("Album");
var albumPic = await albumFolder.GetFileAsync("albumArt.png");
var bitmap = new BitmapImage();
using (var stream = await albumPic.OpenAsync(FileAccessMode.Read))
{
await bitmap.SetSourceAsync(stream);
}
AlbumImage.Source = bitmap;
}
catch (FileNotFoundException)
{
// File or Folder not found
}
catch (Exception)
{
throw;
}
}

After Creating Package in Xamarin UWP, Video is Playing Only With Voice, I am Not able to see Video

I am using MediaManager Plugin with latest version to play a video. everything works fine when i run application in debug mode, but when i create a package for window, video is not showing only voice is heard.
I am using below Package
Plugin.MediaManager.Forms
This is My XAML page
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Digi_Sign.Views.MediaScreen"
xmlns:forms="clr-namespace:MediaManager.Forms;assembly=MediaManager.Forms"
BackgroundColor="Black">
<ContentPage.Content>
<Grid x:Name="stkLayout" VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand" BackgroundColor="Black">
<forms:VideoView VideoAspect="AspectFill" x:Name="videoPlayer" ShowControls="False" />
</Grid>
</ContentPage.Content>
</ContentPage>
CS Page
CrossMediaManager.Current.Play(fileName);
No Errors Found in package error log, as i mentioned everything works fine when it is debug mode, but not working in release mode
Basically there is no way to initialize renderer for video in native code for xamrin.UWP, so we need to manually load render assembly in initialize it in App.xaml.cs of UWP platform
Below is my code where i have load assembly files inside OnLaunched() and replace existing Xamarin.Forms.Forms.Init()
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
Windows.UI.Xaml.Controls.Frame rootFrame = Window.Current.Content as Windows.UI.Xaml.Controls.Frame;
if (rootFrame == null)
{
rootFrame = new Windows.UI.Xaml.Controls.Frame();
rootFrame.NavigationFailed += OnNavigationFailed;
List<Assembly> assembliesToAdd = new List<Assembly>();
assembliesToAdd.Add(typeof(VideoViewRenderer).GetTypeInfo().Assembly);
Xamarin.Forms.Forms.Init(e, assembliesToAdd);
// Place the frame in the current Window
Window.Current.Content = rootFrame;
}
if (rootFrame.Content == null)
{
rootFrame.Navigate(typeof(MainPage), e.Arguments);
}
Window.Current.Activate();
}
for more details, here is the link. where you can find more explaination.
https://forums.xamarin.com/discussion/119451/crossmedia-works-in-debug-but-not-in-release-for-uwp-error-msg-about-onecore
Most likely from the behavior you are describing (and looking at your code), it sounds like it's because the Video is not being run on the UI thread, and this causes the app to play the audio but not the video. So change it to the following:
Device.BeginInvokeOnMainThread(() => { CrossMediaManager.Current.Play(fileName); });

Windows phone app, how to use narrator read page title automatically?

I'm developing windows universal app(XAML,C#) on phone, and am enabling accessibility for Narrator. Does anybody know how to get narrator automatically to read page title when a page is opened?
I tried setting automationproperties.name in page but didn't work:
<Page
x:Class="xxxxxx"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
AutomationProperties.Name="Page title to be read"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
The features of the Narrator for UWP are applied when you select a control in a list, or editing a textbox. If you want to read content when the app is opened you should use the SpeechSynthesizer API, which is really easy to implement:
1.- In XAML add a Media Element
<MediaElement x:Name="MediaElement"/>
2.-Then in the code behind of the page:
public MainPage()
{
this.InitializeComponent();
this.Loaded += MainPage_Loaded;
}
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
ReadTitle();
}
private async void ReadTitle()
{
var voice = SpeechSynthesizer.AllVoices.First();
SpeechSynthesizer reader = new SpeechSynthesizer() { Voice = voice };
var text= this.GetValue(AutomationProperties.NameProperty) as String;
await reader.SynthesizeTextToStreamAsync(text);
MediaElement.SetSource(stream, stream.ContentType);
MediaElement.Play();
}
You can read everything you want passing the string to the reader.
You need to make view-able by narrator. I don't believe you can declare the Name property within the Page Class. Try something like this within the content of your Page:
<HyperlinkButton
NavigateUri="www.bing.com"
AutomationProperties.AutomationID="bing url" //Not Required to work
AutomationProperties.Name="Go to the Bing Homepage"//Narrator will read this
<StackPanel>
<TextBlock Text="Bing Dot Com" />
<TextBlock Text="www.bing.com" />
<StackPanel>
</HyperlinkButton>
https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.automation.peers.accessibilityview
EDIT: You may also need to programmatically set focus on the item for this to work

System.OutOfMemoryException while reading and binding image from isolated storage

This is the code I am using for binding image in XAML
<Border toolkit:TiltEffect.IsTiltEnabled="true" Height="350" Width="400" Grid.ColumnSpan="3">
<Grid Height="350" Width="400" Margin="70,0,70,0" x:Name="Container1">
<Grid.Background>
<ImageBrush ImageSource="{Binding ImageCollection[0]}" Stretch="Uniform" AlignmentX="Left" AlignmentY="Center"/>
</Grid.Background>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Tap">
<i:InvokeCommandAction Command="{Binding ImageTapCommand}" CommandParameter="CONTAINER0"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Grid>
</Border>
Likewise I am using 4 border for displaying my recent images.
In my ViewModel I am using the below method for reading image from isolated storage.
public Stream GetFileStream(string filename, ImageLocation location)
{
try
{
lock (SyncLock)
{
if (location == ImageLocation.RecentImage)
{
filename = Constants.IsoRecentImage + #"\" + filename;
}
using (var iSf = IsolatedStorageFile.GetUserStoreForApplication())
{
if (!iSf.FileExists(filename)) return null;
var fs = iSf.OpenFile(filename, FileMode.Open, FileAccess.Read);
return fs;
}
}
}
catch (Exception ex)
{
return null;
}
}
And after getting the stream I will use this below written method four building the WritableBitmap for UI binding
private WriteableBitmap BuildImage(Stream imageStream)
{
using (imageStream)
{
var image = new BitmapImage();
image.SetSource(imageStream);
return new WriteableBitmap(image);
}
}
In this case my issue is after navigating to and from my page for two - three times. The app crashes on BuildImage() method where I am using " image.SetSource(imageStream);" method. I tried many alternatives but failed. The exception I am getting is "System.OutOfMemoryException "
I tried Image control instead of Image brush.
I tried Bitmap instead of WritableBitmap etc. but the result is same.
The app crashing rate will reduce if I use small images. But the crash rate is high with the images taken through camera.
I am trying for a solution to this issue for last one week, But didn't find any alternative to fix the issue.
I found a link that talks about similar issue But didn't get many to solve the issue
Try this,
var bitmapImage = new BitmapImage();
bitmapImage.SetSource(stream);
bitmapImage.CreateOptions = BitmapCreateOptions.None;
var bmp = new WriteableBitmap((BitmapSource) bitmapImage);
bitmapImage.UriSource = (Uri) null;
return bmp;
Silverlight caches images by default to improve performance. You should call
image.UriSource = null after using the BitmapImage to dispose of the resources.
Are you reseting/disposing the IsolatedStorageFileStream and the IsolatedStorageFile after you use them?
Have you tried forcing the garbage collector to run to see if that makes any difference.
GC.Collect();
This is not intended as a solution - you should never have to call GC.Collect, but it might help determine whether your problem is a memory leak or just a delay in the memory being reclaimed.

adding media element in windows phone 7?

I want to play an audio of an image whenever click on it .If we click on another image that sound will play in that way we want write the logic.
If you're binding to an object that has the image uri and audio clip uri:
<Image Source="{Binding ImagePath}" Tag="{Binding AudioPath} MouseLeftButtonDown="img_MouseLeftButtonDown" />
then in the event handler
void img_MouseLeftButtonDown(object sender,MouseButtonEventArgs e)
{
Image img = sender as Image;
if (img != null)
mePlayer.Source = img.Tag as Uri;
}
i dont recommend mediaElement for more than one audio item ..it has weird effects ...use something like:
Stream stream = TitleContainer.OpenStream(#"Audio/buzzer.wav");
SoundEffect effect = SoundEffect.FromStream(stream);
FrameworkDispatcher.Update();
effect.Play();
using the xna framework ....

Resources