Windows Phone 7 - Playing a specific portion of sound using MediaElement - silverlight

I'm trying to play a specific portion of a mp3 file using MediaElement. I understand that I can use the Position property to move to a specific time in the sound and start playing from there. But I would also like to restrict the duration when it plays. For example if a given mp3 file is 30 seconds long, i would specifically like to play the sound from 5th second to 10th second and want the playing to stop.
I thought about using a separate background thread or a timer callback that will measure the time and then wakeup to stop the mediaelement. But I was wondering if the MediaElement itself supports one such a way to specify the duration of the time to play.

You can set Markers. Once a Marker is reached, the MarkerReached event is fired and you can stop the sound MediaElement there.

Related

How to know how long the animation last and how to stop gif at the end in Maple

How do I know how long an animation lasts and how do I stop the gif at the end?
I created a GIF from export tool (Maple) but I don't know how long the animation is, or how to make GIF run in real time. I made a gif for showing how something falls down from an altitude. I need to show some figures like t(time) , h(height), v(speed) while it falls down.
Commands that generate animations in Maple often have an option that controls the default number frames. For example, the plots:-animate uses 25 frames. Moreover, animations that are shown in the Maple GUI can be controlled by the animation toolbar, which sets animations to run at 10 frames per second (fps) by default. When you use the right-click menu to export your animation to a GIF file, the exporter will take the current setting for fps into consideration and produce an animation that is 25 frames / 10 frames per second = 2.5 seconds long. Changing this value in the animation toolbar will result in shorter or longer animations accordingly.
From a couple of quick tests, exporting the animation programmatically does not respect the choice of fps, so in this case you may need to play with the number of frames in order to compensate for the default 10 fps setting.
With respect to stopping the gif, as far as I know, there is no way to control this using Maple. I believe that the only solution is to use a GIF editing program to manually turn off the 'loop' option.

Rendering video using WriteableBitmap causes choppy animation

So here's my setup:
Camera images coming in at 1920x1080 # 25 FPS
Writing image data to WriteableBitmap on UI thread (simple copy, no processing)
Two Image controls in two different windows on two different monitors has their Source property set to the WriteableBitmap
Some generic UI stuff goes over the camera images
This works great, uses about 4% CPU on an old laptop (8 logical processors). The video is as smooth as can be. However, my UI has some animations (stuff moving around). When the camera display is running, those animations gets choppy.
Right now, the camera image is in Gray8 format, so it will be converted (I guess when calling WritePixels?). I also tried forcing one of the animations to 25 FPS too, no change.
Where should I start to resolve this? Is the issue that I'm locking the bitmap for too long, or is there something else going on? From what I can see locking the bitmap will cause the render thread to block, so moving that to another thread seems pointless. And it does feel like that somewhat defeats the purpose of WriteableBitmap.
This is always going to be tricky because you're capturing at 25FPS whilst WPF tries to update at 60. It's difficult to offer any meaninful advice without seeing a testable project but I'd probably start by doing the updates in a CompositionTarget.Rendering handler.

WP7 gapless music from MediaPlayer when using mediaElement

I'm trying to make a text reader for my WP7.
I'm using a mediaElement in my reader.
at first every time the app read something, the MediaPlayer stopped, as I later discovered that is how it is designed.
Then I tried to try and trick the system, by using
mediaElement1.Stop();
mediaElement1.SetSource(isolatedStorageFileStream);
FrameworkDispatcher.Update();
MediaPlayer.Pause();
mediaElement1.Play();
if (mpState != MediaState.Stopped)
{ MediaPlayer.Resume(); }
now comes the wierd part,
when I use the reader once, it reads and the music seems to run without any pause, as I wanted, but when I use the reader for the second time, the MediaPlayer stops to play.
when I've added some breakpoints, I've noticed that after the first read, the MediaPlayer.state is paused, but it still plays.
I have some kind of a media player in my app, so I have some listBoxes that are filters, I want to read to selected filer (artist, album, etc.). so basically using the reader happens when selected item is changed. I'm using both MediaPlayer and MediaElement because if i won't pause the mediaPlayer, it will stop, and I will lose the position of the playing song. and because I want the reader to play gapless I'm using a mediaElement.
I'm lost...
Try looking at this (Music + Videos Hub Sample) http://go.microsoft.com/fwlink/?LinkID=203588 which I think you already use (using the media libraries on the phone), but try combining it with this (Silverlight Sound Sample) http://go.microsoft.com/fwlink/?LinkID=207868, which shows how to use XNA to play sounds in parallel.
If you carefully mix & match these 2 examples, you can achieve what you want, I’ve just tried it myself. Simply copied all that’s needed from example 1 into example 2, and it works just fine: the background music plays, and the birds happily chirp without harming one another.
Let me know if you have problems with it.
BTW - over here: http://msdn.microsoft.com/en-us/library/ff431744(v=vs.92).aspx you have many working examples, for many different things. I took both examples above from this link.
Eyal

Smooth Video Transitions for WPF

I'm using VB.net 2010 and WPF 4. I need to have a smooth transition between two videos played on the mediaelement. I absolutely cannot use anything that requires me to use a winhost in the WPF window, as that will make my project impossible (since the video is full screen, and the controls are over the video)
Basically, I need for the video to play through, and then smoothly go to another video specified in code behind. I cannot splice the two videos together - they must be separate.
How do I have the videos transition smoothly, with no "blink"?
I'm guessing without testing here. You're probably going to need some CPU cores and a good video card.
If you have the memory, use two MediaElements.
Queue up both videos, one on each element.
Set the opacity of the second one to completely transparent. They're UIElements so this should work...
Use timers of some kind keyed from the start of playback on the first one so that you get an event a couple of seconds before playback ends.
With that event delegate, start the video in the second MediaElement, animate the first one's opacity to zero while simultaneously animating the second one to fully opaque.
If you need to do it again, set up the timer again and make sure your delegate animates things the other way.

SpeedRatio & MediaTimeline

I'm trying to be able to affect SpeedRatio on a MediaElement whilst having the media play in a continous loop.
This is possible through code behind; I can reset the position of the media once it has ended, but that creates a seam in the playback.
For seamless playback, I use a MediaTimeline, but when I use I media timeline, I can't change the SpeedRatio.
Has anyone got a different approach to looping playback in a mediaElement, or handling SpeedRatio?
UPDATE:
If I stop the timeline, change the timeline's SpeedRatio, I can produce the result I'm looking for. The only remaining problem is getting the timeline to start from the same position that it was stopped at. Pausing the timeline does not allow for the SpeedRatio to be changed.
I still welcome any alternatives.
My MediaUriElement in my open-source project has a "Loop" property that can provide seemless looping. Get it from the source because it's the newest.
The only solution I've found for this is to use XNA to control audio. It's got a lot more responsiveness.

Resources