I m using MediaElement to play a web video. When I left the page I noticed in the Task Manager that my app was still using 10% of network and didn't drop till it finished downloading video.
I tried doing the following but no luck.
//open link;
mediaElement.Source = welcomeVideoURL;
//when I leave the page OnNavigatedFrom()
mediaElement.Stop();
mediaElement.ClearValue(MediaElement.SourceProperty);
mediaElement.Source = null;
Also tried to set the source to a dummy link but still no luck.
I thought that opening the Link as a Stream and use mediaElement.SetSource() could work but I haven't found anything on that...maybe I m not searching correct.
Thank you.
Found this MediaElementWithHttpClient in some other question in a comment made by #kiewic. I can manage the stream and download process and easily dispose it.
HttpRandomAccessStream videoStream = await HttpRandomAccessStream.CreateAsync(new Windows.Web.Http.HttpClient(), videoUrl);
mediaElement.SetSource(videoStream, videoStream.ContentType);
Related
I am beginning to learn how to use the LibVLCSharp library. Right now I am trying to play a streaming video that comes to me in multicast UDP, 224.XX.XX.XX:PORT. The problem is that said video comes to me without format.
I get to reproduce it in cmd with:
vlc udp://#224.XX.XX.XX:PORT --demux=mp4v --rawvid-fps=12
This is mi code:
public void PlayURLFile(string file)
{
var media = new Media(_libVLC, "udp://#224.XX.XX.XX:XXXXX");
media.AddOption(":demux=mp4v");
media.AddOption(":rawvid-fps=12");
_mp.Play(media);
isPlaying = true;
}
When executing it does not show me any error.
The videoview I have to show the video shows me the black screen.
I understand that the problem may be that I am not entering AddOption correctly or that the options are different. But after fighting with the code and looking at documentation, I can't find an answer that is clarifying.
Can someone help me?
Greetings and thank you.
Give the following options in the LibVLC ctor instead.
new LibVLC("--demux=mp4v", "--rawvid-fps=12");
What I want to do is to let user record his voice. The recording would be stored in local storage. User should be able to play the recording or replace it with a new one. And that's where I am having trouble.
Scenario A: there is no recording yet, this works fine... when user presses record button, these steps are taken to record the audio and then set it as source for media element.
// user starts recording by pressing a button
// create file with replace option
var audioStorageFile = await folder.CreateFileAsync(desiredName, CreationCollisionOption.ReplaceIfExists);
await mediaCapture.StartRecordToStorageFileAsync(audioEncodingProperties, audioStorageFile);
// user stops recording...
await mediaCapture.StopRecordAsync();
//...
audioStream = await audioStorageFile.OpenAsync(Windows.Storage.FileAccessMode.Read);
MyMediaElement.SetSource(audioStream, audioStorageFile.ContentType);
I can repeat this proccess as many times as I want and the old audio file is always replaced with the new one.
Scenario B: Recording already exists when I navigate to the page so I want to load it to the MediaElement right away (OnNavigatedTo event) like this
var audioStorageFile = await ApplicationData.Current.LocalFolder.GetFileAsync(desiredName);
audioStream = await audioStorageFile.OpenAsync(Windows.Storage.FileAccessMode.Read);
MyMediaElement.SetSource(audioStream, audioStorageFile.ContentType);
So user navigates to the page and file is already loaded to the MediaElement. The problem starts when user wants to replace the old recording. This piece of code is called before the code from Scenario A:
MyMediaElement.Stop();
MyMediaElement.Source = null;
When it reaches the line
var audioStorageFile = await folder.CreateFileAsync(desiredName, CreationCollisionOption.ReplaceIfExists);
UnauthorizedAccessException is thrown: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).
It seems clear that the reason for the exception being thrown is that the file I'm trying to replace is in use. But I don't understand why and how to avoid it? Can anyone spot what I'm doing wrong? Thanks.
Add .Source = null before you create the file
MyMediaElement.Source = null;
var audioStorageFile = await folder.CreateFileAsync(desiredName, CreationCollisionOption.ReplaceIfExists);
If you're not in the habit of closing/disposing your Streams after you are done...you going to have a bad time.
To make sure dispose gets called, get in the habit of using using
Set your MediaElement like so
var audioStorageFile = await ApplicationData.Current.LocalFolder.GetFileAsync("your_wav.wav");
using(Windows.Storage.Streams.IRandomAccessStream audio = await audioStorageFile.OpenAsync(FileAccessMode.Read))
{
this.myMed.SetSource(audio, audioStorageFile.ContentType);
}
Using the above methods, I was able to record over "your_wav.wav" no matter if it was already set as the Source of the MediaElement.
Ok, turns out that the problem actualy was that I was setting the source of the MediaElement before the MediaElement was added to the visual tree. I moved the code to set the media source from OnNavigatedTo to MediaElement.Loaded event and the problem was solved. I found the solution thanks to this question.
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.
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.
Cant display the video in Silverlight.Which i saved in sql server and retrieve it from database as byte[] and subsequently convert it as Stream and put as SetSource of Media element.But cant display anything.
Plz help me.
Like this:
MediaElement SoundClip = new MediaElement();
SoundClip.SetSource(stream);
SoundClip.AutoPlay = false;
SoundClip.Width = 500;
SoundClip.Height = 500;
SoundClip.Stretch = Stretch.Fill;
this.LayoutRoot.Children.Add(SoundClip);
SoundClip.Play();
But does not work.
EDIT:
It is in wmv format.
I could not even play a wmv/wma file from local drive.Is there any issue with the PC i m using.It just runs the code but does play it.does not show any error.
Any suggestion?
What format is the media?
Can it be played directly as a WMV, WMA, MP3, or H.264 file?
Is the media file completely downloaded?
Monitor the HTTP traffic using Fiddler to see what's happening
Attach a MediaElement.MediaFailed event to the SoundClip to see the error (if any)