Permalink
Browse files

Dependency service to stream in iOS and Android

- Add IStreaming interface
- Implement DependencyInjection in iOS
- Implement DependencyInjection in Android
  • Loading branch information...
ilanolkies committed May 23, 2018
1 parent 7c53084 commit f304be0708af39936c8a2e103d7b98b473db29da
@@ -1,6 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.ilanolkies.StreamingExample">
<uses-sdk android:minSdkVersion="15" />
<application android:label="StreamingExample">
</application>
</manifest>
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application android:label="StreamingExample"></application>
</manifest>
@@ -117,6 +117,7 @@
<Compile Include="MainActivity.cs" />
<Compile Include="Resources\Resource.designer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="StreamingService.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Properties\AndroidManifest.xml" />
@@ -0,0 +1,46 @@
using StreamingExample.Droid;
using Xamarin.Forms;
using Android.Media;

[assembly: Dependency(typeof(StreamingService))]
namespace StreamingExample.Droid
{
public class StreamingService : IStreaming
{
MediaPlayer player;
string dataSource = "rtsp://your.domain.com/stream.stream";

bool isPrepared;

public void Play()
{
if (!isPrepared)
{
if (player == null)
player = new MediaPlayer();
else
player.Reset();

player.SetDataSource(dataSource);
player.PrepareAsync();
}

player.Prepared += (sender, args) =>
{
player.Start();
isPrepared = true;
};
}

public void Pause()
{
player.Pause();
}

public void Stop()
{
player.Stop();
isPrepared = false;
}
}
}
@@ -1,6 +1,11 @@
# XamarinRadioStreamingExample
## Setup
On _/iOS_
Set your streaming domain in _StreamingService.cs_. Change `dataSource`.
Allow transport in _Info.plist_ secure domains (change your.domain.com).

On Android
Set your streaming domain in _StreamingService.cs_. Change `dataSource`.

## Resources
- Icons: [FlatIcon](https://www.flaticon.com/packs/music)
- Streaming: [Radio Zonica](http://radiozonica.com.ar)
@@ -0,0 +1,11 @@
using System.Threading.Tasks;

namespace StreamingExample
{
public interface IStreaming
{
void Play();
void Pause();
void Stop();
}
}
@@ -40,6 +40,7 @@
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="StreamingViewModel.cs" />
<Compile Include="IStreaming.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="Xamarin.Forms.Core">
@@ -1,5 +1,6 @@
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Xamarin.Forms;

namespace StreamingExample
{
@@ -30,16 +31,19 @@ bool IsPlaying

public void Play()
{
DependencyService.Get<IStreaming>().Play();
IsPlaying = true;
}

public void Pause()
{
DependencyService.Get<IStreaming>().Pause();
IsPlaying = false;
}

public void Stop()
{
DependencyService.Get<IStreaming>().Stop();
IsPlaying = false;
}
}
@@ -42,5 +42,24 @@
</array>
<key>XSAppIconAssets</key>
<string>Assets.xcassets/AppIcon.appiconset</string>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>your.domain.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSTemporaryExceptionMinimumTLSVersion</key>
<string>TLSv1.1</string>
</dict>
</dict>
</dict>
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
</array>
</dict>
</plist>
@@ -119,6 +119,7 @@
<ItemGroup>
<Compile Include="Main.cs" />
<Compile Include="AppDelegate.cs" />
<Compile Include="StreamingService.cs" />
</ItemGroup>
<ItemGroup>
<BundleResource Include="Resources\stop.png" />
@@ -0,0 +1,36 @@
using StreamingExample.iOS;
using Xamarin.Forms;
using AVFoundation;
using Foundation;

[assembly: Dependency(typeof(StreamingService))]
namespace StreamingExample.iOS
{
public class StreamingService : IStreaming
{
AVPlayer player;
bool isPrepared;
string dataSource = "http://your.domain.com/stream";

public void Play()
{
AVAudioSession.SharedInstance().SetCategory(AVAudioSessionCategory.Playback);
if (!isPrepared || player == null)
player = AVPlayer.FromUrl(NSUrl.FromString(dataSource));

isPrepared = true;
player.Play();
}

public void Pause()
{
player.Pause();
}

public void Stop()
{
player.Dispose();
isPrepared = false;
}
}
}

0 comments on commit f304be0

Please sign in to comment.