Include video for playback in xap file - c#

I require to include a video file for playback in xap file, instead of separately uploading it in Isolated Storage.
What are my options?
Originally I had wanted to play a video file from the MediaLibrary.
Video playback of a file stored in Media Library
As I learnt, it is not possible to do so. Hence, I am exploring the option of at least including the video file inside xap file.
Kindly note that the API for video playback supports reading video files only from Isolated storage, hence I can't just include it in the project and expect it to work.

Assuming your video is located inside assets folder (in your .xap file) the following should work:
private void PlayVideo()
{
var mediaPlayerLauncher = new MediaPlayerLauncher
{
Media = new Uri("Assets/video.mp4", UriKind.Relative),
Location = MediaLocationType.Install,
Controls = MediaPlaybackControls.Pause | MediaPlaybackControls.Stop,
Orientation = MediaPlayerOrientation.Landscape
};
mediaPlayerLauncher.Show();
}

Add it as a resource as you add images to your project. Solution explorer.

Related

C# - Play MP4 from Embedded Resources in Background

I have added a MP4 file to the Project resources and changed the building to Embedded Resource.
Now I want to load this mp4 file from the embedded resources and play it on the form (for example in a picturebox).
I know it's possible with Windows Media Player for example but I haven't found a way to play the mp4 from embedded resources without having to extract into a folder first.
Something like that would be awesome:
axWindowsMediaPlayer1.URL = global::PlayerUI.Properties.Resources.MP4File;
But that doesn't work of course. Is there a method like that?

Saving images and retrieving them Xamarin iOS

So after recently updating dependencies including Xamarin Forms, I can no longer see saved images within our app in iOS. We use https://github.com/jamesmontemagno/MediaPlugin for adding/taking pictures and have even tried specific OS file writes to environment folder:
iOS specific for saving an image:
public string WriteFile(byte[] data)
{
var path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
var filePath = Path.Combine(path, CameraHelper.GenerateFileName());
File.WriteAllBytes(filePath, data);
return filePath;
}
The images are saved under an ID that changes after every app update. This obviously breaks our previous method as we saved the image path (which was always the same prior) in the tables and used that to show preview of the images.
Here is the new image path:
"/var/mobile/Containers/Data/Application/215E777E-A624-487E-B687-6647F8C0D1DC/Documents/01_18_2021_04_24_07_98524.jpg"
Old path:
/data/user/0/com.company.ourapp/files/Pictures/
Which essentially makes it impossible for us to see pictures taken from a previous build/version of the app. I have scoured Xamarin Forms/Essentials and Stack Overflow but no luck.
Is there no other way to retrieve the images? Or do we have to set the library paths to be dynamic for our views?
Old path:
/data/user/0/com.company.ourapp/files/Pictures/
It is obviously an Android file path instead of a iOS file Path. Android's file system is different from iOS.
Here is the new image path:
"/var/mobile/Containers/Data/Application/215E777E-A624-487E-B687-6647F8C0D1DC/Documents/01_18_2021_04_24_07_98524.jpg"
This is the right path to iOS document folder in sandbox. See this document about the Application directories in iOS.
I think you should distinguish iOS and Android when retrieving the images.
Which essentially makes it impossible for us to see pictures taken
from a previous build/version of the app.
If the path really changes with different versions, you can get the app version number at runtime and give different filePath to different version.
(While I think your problem is the difference between iOS and Android file path.)

Playing a .wav file in c# using SoundPlayer

I am currently having a probelm, where a file was sent to me as .m4a and I renamed it to .wav..... (I think that is the issue as I can play other .wav files)
Now it is giving me this error when I run the code:
Additional information: The file located at c:\Windows\Media\dj.wav is not a valid wave file.
Maybe I just need to change it back to .m4a and then have some website convert it to .wav compared to me just renaming it?
My code looks like this:
using System.Media;
playSimpleSound();
private void playSimpleSound()
{
SoundPlayer simpleSound = new SoundPlayer(#"c:\Windows\Media\dj.wav");
simpleSound.Play();
}
Also, how can I write my path, so that it would hit the file in the debug folder? Because I am going to deploy this to a thin client in another building.
Changing the extension of a file does not change the content in it. M4A are MPEG-4 audio-only data, whereas WAV files are typically raw and uncompressed audio samples.
To convert the data itself, you'll need to use an audio transcoding tool like SoX or GoldWave.
As for your path specifier, you can simply use "dj.wav" or "wavfiles\dj.wav", basically relative to where your exe sits. This is because the current working directory is set to where it resides when started with a simple double click in explorer. You can, however also specify the full path using the answer here.
Hope this helps!

How to open a file in the project as a stream?

I have a collection of text files in my project containing level information that I want to load as streams and read in XNA. I am using MonoGame XNA and targeting Windows RT. Let's say I want to open /Content/Levels/Level1.txt as a stream. How would I do this?
On some other platforms using C# I would set the file's build action to resource and use
Application.GetResourceStream
but this is not available in XNA.
There are two kinds of storage in an XNA game: Title Storage and User Storage. See What is Storage? To read data from Title Storage, use TitleContainer.OpenStream as in this example.
string fileName = "Dummy.xml";
string resourceName = "Namespace0.Folder.Folder2.DataFolder." + fileName;
var resource = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);

Using files in my Windows Phone app, resource, content..?

I just started on my second Windows Phone application, and I ran into a problem (ehm..challange). In my app, I have added a folder called "Images", and some sub-folders, like this:
Images -> boys -> graphs
The boys folder contains 1 xml file, and the graphs folder contains hundreds of .gif images. And I'm now wondering how to access these files, how can I build them into the .xap file, what's the recommended way to go?
Should I set the files to "Content" or "Resource", "Copy to output folder"?
And how do I refer to these files, both the xml file in "boys", and the gif images in "graphs"?
A bit confused, and hoping that there is someone who can kick me in the right direction :)
edit, more info
I got it working, loading the xml file with the code below, I haven't tried with the images yet, but is this "the way to do it"?
string xmlBoys = #"images/boys/Names.xml";
Uri uri = new Uri(xmlBoys, UriKind.Relative);
StreamResourceInfo sm = Application.GetResourceStream(uri);
System.Xml.XmlReader xr = System.Xml.XmlReader.Create(sm.Stream);
XDocument data = XDocument.Load(xr);
If you set the BuildAction on the images to Content you can refer to them by the relative path.
e.g.: new Uri("\Images\boys\graphs\img1.gif", UriKind.Relative);
You don't need to "Copy to output folder" unless you need them there for your own purposes. You probably don't. That option is an artefact of other types of project. Everythign you need to distribute will be bundled in the XAP.
Importantly, however, note that WP7 does not have native support for gif format files. I'd suggest converting these to PNG before trying to use them.

Categories

Resources