Playing background audio and video both at same ti

2020-02-02 03:15发布

问题:

I need to develop windows phone application. In that in the page i need to run background audio and video continuously (in repeat mode).

For Audio, i found this example.

Now how do i add background video in the page? In the page i need to show some textboxes and buttons and in the background video and audio both will play.

Both audio and video file will be included in the application i.e. no steaming is needed.

回答1:

I actually did this for a work assignment of mine a few months back. I found out that Silverlight for WP7 did not allow me to have two MediaElements playing at the same time. What I did was create a Windows Phone Silverlight and XNA Application. (Now Silverlight for WP7 finally created a background audio player so you can do it that way if you would like)

Your application can be entirely in Silverlight, but then you can use the XNA referenced (also you can have access to an update loop which is really nice)

XNA has a SoundEffect and SoundEffectInstance class

Then to load a sound just do the following:

Globals in you wrapper class Sound.cs

private SoundEffect Sound = null;
private SoundEffectInstance Instance = null;

Playing a Sound Effect in Sound.cs

Sound = ContentManager.Load<SoundEffect>(fileName); //ContentManager will have to be instantiated from wherever you create it.
Instance = Sound.CreateInstance();
Instance.Play();

Then just use the Silverlight MediaElement to play your video file.

XAML

<MediaElement x:Name="VideoPlayer" AutoPlay="False" Width="320" Height="220"/>

CodeBehind

VideoPlayer.Source = = new Uri("fileName", UriKind.Relative);
VideoPlayer.Play();


回答2:

You can achieve this by filling a Silverlight element using a VideoBrush which is pointing to your video playing in an invisible MediaElement. That would allow the video to play and other UI elements to be shown on top or around it.

See VideoBrush documentation on MSDN which contains an example of how to do this.

However be aware that only one MediaElement can exist at a time. So if you need to play separate audio and video at the same time, you will need to play the audio using the XNA SoundEffect class.



回答3:

If you only care about playing the audio when your application is visible - then you don't need to use background audio and just use a MediaElement to play it back. If you want to have both audio and video together though - you might need to use XNA for audio as Paul mentioned OR you could display your video as a sequence of images/sprites instead of playing back a regular video file.

Note that either of these is going to be rather bad for the battery, so it might only make sense if you are making a night stand kind of app that runs when the phone is connected to a power supply.