Skip to content

Commit

Permalink
Dependency service to stream in iOS and Android
Browse files Browse the repository at this point in the history
- Add IStreaming interface
- Implement DependencyInjection in iOS
- Implement DependencyInjection in Android
  • Loading branch information
ilanolkies committed May 23, 2018
1 parent 7c53084 commit f304be0
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 6 deletions.
10 changes: 6 additions & 4 deletions Droid/Properties/AndroidManifest.xml
@@ -1,6 +1,8 @@
<?xml version="1.0" encoding="utf-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"> <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" /> <uses-sdk android:minSdkVersion="15" />
<application android:label="StreamingExample"> <uses-permission android:name="android.permission.INTERNET" />
</application> <uses-permission android:name="android.permission.WAKE_LOCK" />
</manifest> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application android:label="StreamingExample"></application>
</manifest>
1 change: 1 addition & 0 deletions Droid/StreamingExample.Droid.csproj
Expand Up @@ -117,6 +117,7 @@
<Compile Include="MainActivity.cs" /> <Compile Include="MainActivity.cs" />
<Compile Include="Resources\Resource.designer.cs" /> <Compile Include="Resources\Resource.designer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="StreamingService.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="Properties\AndroidManifest.xml" /> <None Include="Properties\AndroidManifest.xml" />
Expand Down
46 changes: 46 additions & 0 deletions Droid/StreamingService.cs
@@ -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;
}
}
}
7 changes: 6 additions & 1 deletion README.md
@@ -1,6 +1,11 @@
# XamarinRadioStreamingExample # 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 ## Resources
- Icons: [FlatIcon](https://www.flaticon.com/packs/music) - Icons: [FlatIcon](https://www.flaticon.com/packs/music)
- Streaming: [Radio Zonica](http://radiozonica.com.ar)
11 changes: 11 additions & 0 deletions StreamingExample/IStreaming.cs
@@ -0,0 +1,11 @@
using System.Threading.Tasks;

namespace StreamingExample
{
public interface IStreaming
{
void Play();
void Pause();
void Stop();
}
}
1 change: 1 addition & 0 deletions StreamingExample/StreamingExample.csproj
Expand Up @@ -40,6 +40,7 @@
</Compile> </Compile>
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="StreamingViewModel.cs" /> <Compile Include="StreamingViewModel.cs" />
<Compile Include="IStreaming.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Reference Include="Xamarin.Forms.Core"> <Reference Include="Xamarin.Forms.Core">
Expand Down
6 changes: 5 additions & 1 deletion StreamingExample/StreamingViewModel.cs
@@ -1,5 +1,6 @@
using System.ComponentModel; using System.ComponentModel;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using Xamarin.Forms;


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


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


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

public void Stop() public void Stop()
{ {
DependencyService.Get<IStreaming>().Stop();
IsPlaying = false; IsPlaying = false;
} }
} }
Expand Down
19 changes: 19 additions & 0 deletions iOS/Info.plist
Expand Up @@ -42,5 +42,24 @@
</array> </array>
<key>XSAppIconAssets</key> <key>XSAppIconAssets</key>
<string>Assets.xcassets/AppIcon.appiconset</string> <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> </dict>
</plist> </plist>
1 change: 1 addition & 0 deletions iOS/StreamingExample.iOS.csproj
Expand Up @@ -119,6 +119,7 @@
<ItemGroup> <ItemGroup>
<Compile Include="Main.cs" /> <Compile Include="Main.cs" />
<Compile Include="AppDelegate.cs" /> <Compile Include="AppDelegate.cs" />
<Compile Include="StreamingService.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<BundleResource Include="Resources\stop.png" /> <BundleResource Include="Resources\stop.png" />
Expand Down
36 changes: 36 additions & 0 deletions iOS/StreamingService.cs
@@ -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.