I am using VSTS 2008 with C# to develop Silverlight application embedded in web page of an ASP.Net web application. I have embedded in XAML a MediaElement item. My question is, I want to embed the page a Silverlight media player, which could let end user to control the MediaElement item manually to -- play/pause/stop/rewind/forward. Are there any references samples?
thanks in advance,
George
EDIT1: add more accurate requirements,
Actually, I want to control play manually, which means I want to handle the player play/pause/stop/rewind/forward events and add my code for the event handlers to control the MediaElement and do something else.
EDIT2: My needs are, I want to play two overlapped video. Screen as background video and camera as foreground video (place at right bottom corner). Here is my modification of code, my current issue is, only background video is played, foreground right bottom video is never played. Does anyone have any ideas why?
BTW: my modified code and current work is based on http://www.codeplex.com/sl2videoplayer
http://www.yourfilehost.com/media.php?cat=other&file=sl2videoplayer_24325_new.zip
Here is a brief description of my major modified code,
mediaControls.xaml.cs
private MediaElement _media = null;
private MediaElement _camera = null;
public MediaElement Camera
{
set
{
_camera = value;
}
}
void btnPlay_Checked(object sender, RoutedEventArgs e)
{
_camera.Play();
_media.Play();
OnPlayClicked();
}
Page.xaml
<MediaElement HorizontalAlignment="Stretch" Margin="0,0,0,0" x:Name="mediaPlayer" Stretch="Uniform" VerticalAlignment="Stretch" AutoPlay="false"/>
<MediaElement Width="100" Height="100" x:Name="cameraPlayer" AutoPlay="false" HorizontalAlignment="Right" VerticalAlignment="Bottom"/>
Page.xaml.cs
cameraPlayer.Source = App.Current.Resources["c"] as Uri;
App.xaml.cs (Application_Startup function)
else if (item.Key.ToLower() == "c")
{
FormatUri(e.InitParams["c"].ToString(), "c", false);
}
default.html
<param name="initParams" value="cc=true,markers=true,markerpath=markers_movie21.xml,m=http://localhost/screen.wmv,c=http://localhost/camera.wmv" />
Oh baby have I got the media player for you: Sl2 Video Player. MSPL open sourced and awesome.
To add the ability to control the player pragmatically, add ScriptableMembers.
You'll see the registration statement already in the code:
HtmlPage.RegisterScriptableObject("Page", page);
Now look at an example ScriptableMember:
[ScriptableMember]
public void SeekPlayback(string time)
{
TimeSpan tsTime = TimeSpan.Parse(time);
mediaControls.Seek(tsTime);
}
already exists in the code. Add more methods to do what you want to have happen. Then you can call the methods from managed code in another SL player:
HtmlElement videoPlugin = HtmlPage.Document.GetElementById("VideoPlayer");
if (videoPlugin != null)
{
ScriptObject mediaPlayer = (ScriptObject)((ScriptObject)videoPlugin.GetProperty("Content")).GetProperty("Page");
mediaPlayer.Invoke("SeekPlayback", TimeSpan.FromSeconds(seconds).ToString());
}
or from javascript:
var sl = document.getElementById("VideoPlayer");
var content = sl.Content.Page;
content.SeekPlayback('55');
If they are two seperate xap packages, there will be no way for the two to communicate since Silverlight sandboxes both individually.
SL2videoplayer says it supports streaming video. But when I try giving a media services broadcast url (OnDemand and Live) to init param 'm' nothing showed up. In the init param example page also a remote wmv file being played is shown.
Also are there any known issues of using this with SL 3?
Related
Recently I have started messing around with the VLC ActiveX plugin trying to play youtube videos in a WindowsFormApplication and I have run into some issues that I couldn't find any mention of or solution to. For simplicity I made a new project to demonstrate my problems:
private void button1_Click(object sender, EventArgs e)
{
Player.playlist.play();
}
private void button2_Click(object sender, EventArgs e)
{
url = textBox1.Text;
Player.playlist.add(url);
listBox1.Items.Add(url);
}
2 buttons, play and add to playlist.
Problem 1:
Audio cuts out a few seconds before the end of the video.
Problem 2:
I have been unable to get the control to play the next video in the playlist. It just reaches the end of the first video. If I click the play button again, it plays the first video. One thing I thought might be causing it was the AutoPlay property but it's set to true.
Problem 3:
The Player.playlist.next function, like most thing doesn't have a description and does not let me go to the next video in the playlist.
Problem 4:
The AutoLoop property does not work, assuming that it's supposed to get the control to loop a video.
Problems number 2 and 3 make me think that the songs aren't being added to the playlist correctly, but again I was unable to find any way to confirm or solve that issue.
Using VisualStudio 2015, VLC plugin version, as stated in the control properties, 3.0.1 Vetinari.
(edit) after testing I know that the videos are added to the playlist, but still it won't autoplay the next in playlist at the end.
Using the MediaElement control and MediaCapture class, I am trying to record microphone input and play audio at the same time.
As soon as I start recording, whatever track is playing mutes. I don't think it stops, because it continues playing after the recording has stopped. I have added hooks into several events on the MediaElement and none are being fired. For example, CurrentStateChanged, MediaEnded, MediaFailed etc.
Recording code:
public async void InitializeMediaCapture()
{
_mediaCaptureManager = new MediaCapture();
var settings = new MediaCaptureInitializationSettings();
settings.StreamingCaptureMode = StreamingCaptureMode.Audio;
settings.MediaCategory = MediaCategory.Other;
await _mediaCaptureManager.InitializeAsync(settings);
}
private async void CaptureAudio()
{
_recordStorageFile = await KnownFolders.VideosLibrary.CreateFileAsync(fileName, CreationCollisionOption.GenerateUniqueName);
var recordProfile = MediaEncodingProfile.CreateM4a(AudioEncodingQuality.Auto);
await _mediaCaptureManager.StartRecordToStorageFileAsync(recordProfile, this._recordStorageFile);
//audio playback stops on preceding line of code
}
I use .Play() on the MediaElement to play the audio, with the control in my XAML and the audio source set there.
<MediaElement x:Name="playbackElement"
Source="ms-appx:///Audio/Song.mp3"
AutoPlay="False" />
I have also tried playing the audio as BackgroundAudio, but that didn't work either. Any ideas?
1.Never set a MediaElement's Source in XAML, unless that XAML is on a page that you navigate to after asking the user for consent.
2.Check to see if background music is playing and then set the source (in code).
Note: If you set the source and then immediately call Play(), the Play() will have no affect since the MediaElement will still be in the "Opening" state, instead set "AutoPlay = true" (works from code)
Here's a Reference
Here is my xaml code:
<MediaElement x:Name="beepSound" Source="/Sounds/beep.mp3" AutoPlay="False" Visibility="Collapsed"/>
C# code:
private void ButtonClick(object sender, RoutedEventArgs e)
{
if (beepSound.CurrentState == System.Windows.Media.MediaElementState.Playing)
beepSound.Pause();
else
beepSound.Play();
}
This code works perfectly. But after I resume the app (by pressing start button and then back again into the app) the sound doesn't play. What causes this behavior? Is there anything wrong with my code?
There is nothing wrong in your code
Its just that, Media Element stops working in the background. CurrentState of the media elements gives a "Closed" when we get back to the app after pressing the start button.
You need to use a player that plays the sound even when the app goes in the background(start key press/lock key press). And BackgroundAudioPlayer follows over your requirement.
I am not very much aware with how it works but I can suggest you with some links at this point of time.
Please have a look at the BackgroundAudiolayer
and also its namespace.
And a Sample
Enjoy!
After a bit of research I found out that, after the app resumes it loses its source information. So you have to set the source again. Here is how I did it.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
beepSound.Source = new Uri("/Sounds/beep.mp3", UriKind.Relative);
}
i currently have a music application, and I'm trying to ad lead bolts inapp ad wall they don't give you any help on this what so ever. all they put is
Place this link in a webview of your app or an iframe of your mobile web page where you want your LeadBolt app wall to appear.
http://ad.leadboltads.net/show_app_wall?section_id=97**
im a novice when it comes to programming
I've asked about and I've researched for hours and i haven't been able to find a solution all i can get is mixed answers
all i am really after is a simple timed popup with a web browser in it with an iframe which calls that address so its runs the ad until its closed then pops up again after an x amount of time, is this the best way to solve this or is there another way
i don't mean to be rude but could who ever answers, answer with detail because like i said I'm a novice thanks in advance
EDIT*
i have just found this on there site
Add the following line in your XAML file
webview height="50" width="320" x:name="webview">
Then in your xaml.cs file add the following code to load a HTML Banner or HTML App Wall
webview.NavigateToString("http://ad.leadboltads.net/show_app_ad.js?section_id=xxxxxxxxx\">");
// For App Wall use the following code:
// webview.Navigate(new Uri("http://ad.leadboltads.net/show_app_wall?section_id=xxxxxxxx"));
my new question is how do i get it to appear every so often, above everything else until user closes it
Thanks for posting your question. Our tech team at LeadBolt has supplied the following suggestions to help you out.
If you require further assistance I would encourage you to contact your Account Manager directly, or email our Support team.
To Add a simple auto-refreshing HTML Banner:
1. In your xaml file, add a WebView object like so: <WebView x:Name="banner" Width="320" Height="50" />
2. Add the following code in your xaml.cs file. Please note this is just a sample code, please modify the class and method names as suited to your App’s xaml.cs file:
namespace HTMLAdDemo
{
public sealed partial class MainPage : Page
{
private DispatcherTimer timer = new DispatcherTimer();
String bannerHtml = “<html><body style=\”margin:0;padding:0\”><script type=\"text/javascript\" src=\"http://ad.leadboltads.net/show_app_ad.js?section_id=YOUR_LB_SECTION_ID\"></script></body></html>”;
public MainPage()
{
this.InitializeComponent();
banner.NavigateToString(bannerHtml);
// now setup timer to refresh banner every 30s
timer.Tick += (sender, e) => { bannerWebview.NavigateToString(bannerHtml); };
timer.Interval = new TimeSpan(00, 0, 30); //change this number to modify the refresh time
timer.Start();
}
// Your other App Specific functions here
}
}
To Add an App Wall with a close button. Please Note: this is just a sample code, actual implementation may vary in your App to suit your App’s requirement (i.e Colours, Text and Style need to be modified based on your App’s Requirements):
1. In your xaml file, add the following objects:
<WebView x:Name="appWall" Width="400" Height="300" />
<Button x:Name="closeButton" Width="400" Height="40" Content="Close" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Click="Button_Click"/>
2. Add the following code in your xaml.cs file. Please note this is just a sample code, please modify the class and method names as suited to your App’s xaml.cs file
namespace HTMLAdDemo
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
appWall.Navigate(new Uri("http://ad.leadboltads.net/show_app_wall?section_id=YOUR_LB_SECTION_ID"));
}
private void Button_Click(object sender, RoutedEventArgs e)
{
appWall.Visibility = Visibility.Collapsed;
closeButton.Visibility = Visibility.Collapsed;
}
// Your other App Specific functions here
}
}
Kind Regards,
LeadBolt Support
In my application, I use UI MediaElement. But when i click the Windows key, the music stops.
I tried using:
MediaControl.PlayPressed += MediaControl_PlayPressed;
MediaControl.PausePressed += MediaControl_PausePressed;
MediaControl.PlayPauseTogglePressed += MediaControl_PlayPauseTogglePressed;
MediaControl.StopPressed += MediaControl_StopPressed;
I set source MediaElement:
media.Source = new Uri("http://stream-hq.mp3.zdn.vn/fsgggsfdlwjglwjAAAAA/2a3f830202ea6d29bc7c5a5146401566/4ff5620a/2011/12/27/a/4/a4fcc199a184a93cfeb0fe35642c53bf.mp3", UriKind.RelativeOrAbsolute);
Please help me!
For a Metro/WinRT app to play audio in background, the app needs the following:
A MediaElement control that:
Is in a XAML page.
The AudioCategory property set to BackgroundCapableMedia (as in Armando's answer). There are other values for games or communication systems as needed. See the Audio Playback in a Metro Application for information on what the different options mean.
Use the MediaControl object to capture at least the following. Other events and properties can be handled if desired but the following are required for background playback to function.
PlayPressed
StopPressed
PlayPauseTogglePressed
PausePressed
Add audio to the list of support background tasks in the applications manifest. The manifest is usually called Package.appxmanifest. Select it in the Solution Explorer, go to the Declarations tab and check "Audio" as shown:
See the Transport Controls Guide for more info about capturing hardware buttons (e.g. play/pause on the keyboard) and the quickstart guide for creating a media player for more info.
This would be my first answer. Make sure you set AudioCategory="BackgroundCapableMedia" in your XAML like this:
<MediaElement x:Name="backgroundMusic"
AutoPlay="True"
AudioCategory="BackgroundCapableMedia"
Source="mms://betafm.santafe-conicet.gov.ar:1175">
</MediaElement>
Hope it helps!