Commit dce768ca authored by Martin Finkel's avatar Martin Finkel

Fix chromecast sample and bump libvlc.ios version

parent 52de23ad
......@@ -154,7 +154,7 @@
<Version>0.0.2</Version>
</PackageReference>
<PackageReference Include="VideoLAN.LibVLC.iOS">
<Version>3.0.0-alpha2</Version>
<Version>3.1.5-alpha</Version>
</PackageReference>
<PackageReference Include="Xamarin.Forms" Version="3.3.0.871608-pre2" />
</ItemGroup>
......
using LibVLCSharp.Shared;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using static System.Diagnostics.Debug;
using Xamarin.Forms;
using System;
namespace Chromecast
{
......@@ -28,7 +31,7 @@ namespace Chromecast
// start chromecast discovery
DiscoverChromecasts();
// hold on a bit
// hold on a bit at first to give libvlc time to find the chromecast
await Task.Delay(5000);
// start casting if any renderer found
......@@ -42,24 +45,29 @@ namespace Chromecast
private void StartCasting()
{
// abort casting if no renderer items were found
if (!_rendererItems.Any()) return;
if (!_rendererItems.Any())
{
WriteLine("No renderer items found. Abort casting...");
return;
}
// create new media
var media = new Media(_libVLC,
"http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4",
Media.FromType.FromLocation);
// create the mediaplayer from the media
_mediaPlayer = new MediaPlayer(media);
// create the mediaplayer
_mediaPlayer = new MediaPlayer(_libVLC);
// set the previously discovered renderer item (chromecast) on the mediaplayer
// if you set it to null, it will start to render normally (i.e. locally) again
_mediaPlayer.SetRenderer(_rendererItems.First());
// start the playback
_mediaPlayer.Play(media);
}
private void DiscoverChromecasts()
void DiscoverChromecasts()
{
// load native libvlc libraries
Core.Initialize();
......@@ -67,33 +75,36 @@ namespace Chromecast
// create core libvlc object
_libVLC = new LibVLC();
string rendererName;
// choose the correct service discovery protocol depending on the host platform
// Apple platforms use the Bonjour protocol
if (Device.RuntimePlatform == Device.iOS)
rendererName = _libVLC.RendererList.Select(renderer => renderer.Name).FirstOrDefault(name => name.Equals("Bonjour_renderer"));
else if (Device.RuntimePlatform == Device.Android)
rendererName = _libVLC.RendererList.Select(renderer => renderer.Name).FirstOrDefault(name => name.Equals("microdns_renderer"));
else throw new PlatformNotSupportedException("Only Android and iOS are currently supported in this sample");
// create a renderer discoverer
_rendererDiscoverer = new RendererDiscoverer(_libVLC, _libVLC.RendererList[0].Name);
_rendererDiscoverer = new RendererDiscoverer(_libVLC, rendererName);
// register callback when a new renderer is found
_rendererDiscoverer.ItemAdded += RendererDiscoverer_ItemAdded;
// start discovery on the local network
_rendererDiscoverer.Start();
}
private void _libVLC_Log(object sender, LogEventArgs e)
{
Debug.WriteLine(e.Message);
var r = _rendererDiscoverer.Start();
}
/// <summary>
/// Raised when a renderer has been discovered or has been removed
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void RendererDiscoverer_ItemAdded(object sender, RendererDiscovererItemAddedEventArgs e)
void RendererDiscoverer_ItemAdded(object sender, RendererDiscovererItemAddedEventArgs e)
{
Debug.WriteLine($"New item discovered: {e.RendererItem.Name} of type {e.RendererItem.Type}");
WriteLine($"New item discovered: {e.RendererItem.Name} of type {e.RendererItem.Type}");
if (e.RendererItem.CanRenderVideo)
Debug.WriteLine("Can render video");
WriteLine("Can render video");
if (e.RendererItem.CanRenderAudio)
Debug.WriteLine("Can render audio");
WriteLine("Can render audio");
// add newly found renderer item to local collection
_rendererItems.Add(e.RendererItem);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment