How would I use a pack uri resource with media player? - c#

I have some very simple code which just needs to play a sound with a specified volume, as follows :
var mp = new MediaPlayer();
mp.Open(uri);
mp.Volume = volume;
mp.Play();
The problem arises because this is part of a Prism module. I don't know where the executing assembly is going to be so I can't use a relative Uri or siteoforigin and MediaPlayer doesn't understand the pack Uri syntax.
I have a resource wav file at the root of my assembly called "notify.wav", but I have no way to pass it into MediaPlayer via a Uri and can't see any other way to load it.
How do I play the file?

Since the resource is embedded and the MediaPlayer doesn't support pack uri, you'll need to read the resource in as a stream and write it out to file.You should then be able to load the file into the player as necessary.
I would write the file to my applications directory so that once extracted from the assembly, you can just reference the file directly.
Hope this helps

First you should declare a variable that is string for your media folder's path. This variable holds the path. just like:
string url = #"C:\Users\Alico\Documents\visual studio 2010\Projects\WpfBrushesTest\WpfBrushesTest\Dido - Thank You.mp4";
and then
mp.Open(new Uri(url,UriKind.Relative));

I’m afraid Media player does not support pack URI.
Have you tried Directory.GetCurrentDirectory or Environment.CurrentDirectory?

Use ContentResolver class to retrieve the songs from the device and catch with Song.XML that include (TextViews for Title, Article, Album, etc). This XML is later embedded into the MainActivity XML(including ListView) using Adapter class and then you can call onClick(Song.XML) to play the songs (Setting URI ) on MainActivity XML, later you can set Seekber, Next, Previous and Play function.
If you want, I will give you full codes but I am trying to play a song from another Activity using a new UI design and class file, still searching.

Related

How to create an instance of a FileDescriptor for recording audio in Android Xamarin?

I have an app that plays audio and I would like to record at least 10 seconds of audio when playback is happening. I have checked the official documentation of the MediaRecorder class from both Google and Microsoft and I have followed every step of the recipe required when you want to record audio using that class. I have also declared all the needed permissions but I have a problem setting the output file of the MediaRecorder object I have, The documentation says I need an object of FileDescriptor in the method mediaRecorder.SetOutputFile(FileDescriptor dp) but the constructor of that class does not accept a string for a file name, instinctively I know that if the MediaRecorder object writes audio to a file then that file name needs a name so that I can search for it later and test how the audio was recorded.
Instantiating FileDescriptor like this compiles but what about the file name?
MediaRecorder recorder = new MediaRecorder(this);
//set the source of the audio
recorder.SetAudioSource(AudioSource.Mic);
//set the media encoding of the output
recorder.SetOutputFormat(Android.Media.OutputFormat.Mpeg2Ts);
//specify a file descriptor where to save the recording
//what about the file name?, It surely needs a string for the file name
FileDescriptor destination = new FileDescriptor();
recorder.SetOutputFile(destination);
//prepare the recorder
recorder.Prepare();
//start recording for 10 seconds
recorder.SetMaxDuration(10000);
//start the recording session
recorder.Start();
Checking how FileDescriptor is used in Java then I learnt that it can work like below due to inheritance and stuff.
FileDescriptor destination = new FileOutputStream("myrecording");
//the line above does not compile for Xamarin Android
How can I create an instance of a FileDescriptor while passing a file name to it that I can use for searching the recorded audio?

C# Reading Video Metadata in Windows (Title,description,year)

Hey everyone just trying to make a program that browses video files and reads the title and description from the files metadata. I found some docs from microsoft here giving whats needed but how do I access these functions? what using namespaces are needed in c#? I would love any help that can be provided.
In that link you posted, scroll to the bottom and click "Shell Metadata Providers". There's more more information and some sample C++ code.
Here are some other relevant links:
Reading/Writing metadata of audio/video files
http://www.codeproject.com/Articles/14535/Accessing-WMF-metadata-with-C
https://social.msdn.microsoft.com/Forums/pt-BR/0f36a3b2-4d3d-4842-88a4-bea493bbbace/read-video-filemov-avi-mpg-etc-meta-data?forum=csharpgeneral
https://web.archive.org/web/20170225230114/https://stackoverflow.com/questions/7396265/c-sharp-to-read-properties-of-video-files
Sorry I can't give you anything more concrete, however it looks like some tag libraries (i.e. for reading MP3 metadata) may work as well, as the metadata for videos seems to be stored in a similar, if not identical, format. That being said, you can give TagLib# a shot.
https://www.nuget.org/packages/taglib/
I've made a simple C# code (portable to Unity, too) csatomreader. It's optimized for speed and can read the atoms over HTTP, too.
E.g. Get title:
using (FileStream stream = new FileStream(fileName, FileMode.Open))
{
var mp4Reader = new AtomReader(stream);
string value = mp4Reader.GetMetaAtomValue(AtomReader.TitleTypeName);
Console.WriteLine($"{atomTypeName}: {value}");
}
If you need to get more metadata values at once, then iterate over ParseAtoms(), e.g. see the GetMetaAtomValue() source.

ExpressionPlayer does not play Azure Smooth Streaming Source

I'm writing a VOD solution. For some time I have been working with the SSME:SmoothStreamingMediaElement successfully for testing and now I would like to utilise one of the Expression Players.
I'm using Azure Media Services, specifically Smooth Streaming. While these work fine in SSME I can't get them to work with an ExpressionPlayer. I don't know why.
I'm now at a point where I'm hard coding a Uri to try and get this to work as below:
void dataConectorPopulatePlaylistDownloadComplete(MemoryStream returnData, EventArgs e)
{
<snip>
var myPlaylist = new ExpressionMediaPlayer.Playlist();
var playlistItem = new PlaylistItem();
playlistItem.MediaSource = new Uri("http://xxxxxms1.origin.mediaservices.windows.net/b78750fc-9e2f-448c-86e3-d5de084791ea/GOPR0009.MP4-b2d2b578-3560-42c6-9927-2a791f395e19.ism/manifest",UriKind.Absolute);
playlistItem.IsAdaptiveStreaming = true;
myPlaylist.Items.Add(playlistItem);
SmoothPlayerStreaming.Playlist = myPlaylist;
<snip>
}
Using the above returns 404 not found in the player video playback window.
This is a valid URL and a valid Smooth Streaming Uri. Using this exact same Uri in a SSME control works fine.
What have I done wrong?
The ExpressionMediaPlayer class makes a hidden call to the ClientBin/SmoothStreaming.xap file. If you don't have it there - you should add it.
Here is the link to the blog post where you can download the xap file and source code of the expression player. Direct link
After you download the archive above, you can find this file at this path: EE4SP1SilverlightDefaultWithAudioVolume.zip\Templates\Silverlight Default -- with Audio Volume On Start\SmoothStreaming.xap
If it still doesn't work, you should replace the MediaPlayer.dll by projects from the archive. You need to add (Add -> Existing Project) 3 projects from the SharedV4SP1 folder: MediaPlayer, OfflineShared, PlugInMSSCtrl.
I've already tested your code in my application and it started to work after I have copied the xap file and replaced the dll-reference by existing projects.

Getting MP4 File Duration with DirectShow

I need to get the duration of an mp4 file, preferably as a double in seconds. I was using DirectShow (see code below), but it keeps throwing a particularly unhelpful error. I'm wondering if someone has an easy solution to this. (Seriously, who knew that getting that information would be so difficult)
public static void getDuration(string moviePath)
{
FilgraphManager m_objFilterGraph = null;
m_objFilterGraph = new FilgraphManager();
m_objFilterGraph.RenderFile(moviePath);
IMediaPosition m_objMediaPosition = null;
m_objMediaPosition = m_objFilterGraph as IMediaPosition;
Console.WriteLine(m_objMediaPosition.Duration);
}
Whenever I run this code, I get the error: "Exception from HRESULT: 0x80040265"
I also tried using this: Getting length of video
but it doesn't work either because I don't think that it works on MP4 files.
Seriously, I feel like there has to be a much easier way to do this.
Note: I would prefer to avoid using exe's like ffmpeg and then parsing the output to get the information.
You are approaching the problem correctly. You need to build a good pipeline starting from source .MP4 file and up to video and audio renderers. Then IMediaPosition.Duration will get you what you want. Currently you are getting VFW_E_UNSUPPORTED_STREAM because you cannot build the pipeline.
Note that there is no good support for MPEG-4 in DirectShow in clean Windows, you need a third party parser installed to add missing blocks. This is the likely cause of your problem. There are good Free DirectShow Mpeg-4 Filters available to fill this gap.
The code sample under the link Getting length of video is basically valid too, however it uses deprecated component which in additional make additional assumptions onto the media file in question. Provided that there is support for .MP4 in the system, IMediaPosition.Duration is to give you what you look for.
You can use get_Duration() from IMediaPosition interface.
This return a double value with the video duration in seconds.
Double Lenght;
m_FilterGraph = new FilterGraph()
//Configure the FilterGraph()
m_mediaPosition = m_FilterGraph as IMediaPosition;
m_mediaPosition.get_Duration(out Length);
Using Windows Media Player Component also, we can get the duration of the video.
I hope that following code snippet may help you guys :
using WMPLib;
// ...
var player = new WindowsMediaPlayer();
var clip = player.newMedia(filePath);
Console.WriteLine(TimeSpan.FromSeconds(clip.duration));
and don't forget to add the reference of wmp.dll which will be
present in System32 folder.

Make correct URI for WP Game Library

I am trying to create a library with sounds in it, but I cant get the URIs to work, if I use a online uri like
new Uri("http://www.archive.org/download/BrahmsViolinConcerto-Heifetz/03Iii.AllegroGiocosoMaNonTroppoVivace.mp3")
it works fine, so the issue is linking correctly to my folders in my project
My in my WP Game Librarys folder I have \Sounds\letters and in that folder is a sound named a.wma
My Method for loading this is
public void PlayLetter(string letter)
{
try
{
Initialize();
FrameworkDispatcher.Update();
var uri = new Uri(#"/Sounds/letters/" + letter + ".wma", UriKind.Relative);
var song = Song.FromUri("sound", uri);
MediaPlayer.Play(song);
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
}
}
And I of course give it string "a" as a parameter when it fails
I have also included the sound file in my project like
I just get a
A first chance exception of type 'System.InvalidOperationException' occurred in Microsoft.Xna.Framework.dll
But its an uri problem I am certain as I tried a online URI that worked just fine
Also I am in doubt of 2 things, is MediaPlayer the right thing to use in a game? And can a library play sounds (Or even contain them)
The typical thing in XNA would be to use a SoundEffectInstance:
http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.audio.soundeffectinstance.aspx
Unfortunately SoundEffectInstance only works with wav files. If you want to play back longer music files - you can use a MediaElement - but that allows for playback of a single compressed audio file at a time only. Another option might be to play compressed from the MediaLibrary using the MediaPlayer class. You could also save your own compressed audio file in the MediaLibrary to play it from there. See:
http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.media.medialibrary.songs.aspx

Categories

Resources