public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId) { if (intent == null) { return(StartCommandResult.NotSticky); } if (intent.Action == ACTION_STOP) { Stop(); return(StartCommandResult.NotSticky); } else if (intent.Action == ACTION_START) { string host = intent.GetStringExtra(EXTRA_HOST); int port = intent.GetIntExtra(EXTRA_PORT, 1704); EBroadcastMode broadcastMode = (EBroadcastMode)intent.GetIntExtra(EXTRA_BROADCAST_MODE, (int)EBroadcastMode.Media); Intent stopIntent = new Intent(this, typeof(SnapclientService)); stopIntent.SetAction(ACTION_STOP); PendingIntent PiStop = PendingIntent.GetService(this, 0, stopIntent, 0); NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID) .SetSmallIcon(Resource.Drawable.ic_media_play) .SetTicker("Snap.Net.Broadcast started") .SetContentTitle("Snap.Net Broadcast") .SetContentText("Snap.Net.Broadcast is running...") .SetContentInfo(string.Format("{0}:{1}", host, port)) .SetStyle(new NotificationCompat.BigTextStyle().BigText("Snap.Net.Broadcast is running...")) .AddAction(Resource.Drawable.ic_media_stop, "Stop", PiStop); Intent resultIntent = new Intent(this, typeof(MainActivity)); TaskStackBuilder stackBuilder = TaskStackBuilder.Create(this); stackBuilder.AddParentStack(Class.FromType(typeof(MainActivity))); stackBuilder.AddNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.GetPendingIntent( 0, PendingIntentFlags.UpdateCurrent ); builder.SetContentIntent(resultPendingIntent); Notification notification = builder.Build(); StartForeground(NOTIFICATION_CHANNEL, notification); m_MediaProjection = m_MediaProjectionManager.GetMediaProjection((int)Result.Ok, intent.GetParcelableExtra(EXTRA_RESULT_DATA) as Intent); _Start(host, port, broadcastMode); return(StartCommandResult.Sticky); } return(StartCommandResult.NotSticky); }
public void Broadcast(string host, int port, EBroadcastMode broadcastMode) { m_BroadcastHost = host; m_BroadcastPort = port; m_BroadcastMode = broadcastMode; if (_HasRecordAudioPermission() == false) { ActivityCompat.RequestPermissions(this, new [] { Manifest.Permission.RecordAudio }, RECORD_AUDIO_PERMISSION_REQUEST_CODE); } else { _StartMediaProjectionRequest(); } }
public void Broadcast(string host, int port, EBroadcastMode broadcastMode) { }
public void Broadcast(string host, int port, EBroadcastMode broadcastMode) { MainActivity.Instance.Broadcast(host, port, broadcastMode); }
private void _Start(string host, int port, EBroadcastMode broadcastMode) { try { if (m_Running) { return; } Android.OS.Process.SetThreadPriority(ThreadPriority.UrgentAudio); PowerManager powerManager = (PowerManager)GetSystemService(PowerService); m_WakeLock = powerManager.NewWakeLock(WakeLockFlags.Partial, "snapdotnet:SnapcastPartialWakeLock"); m_WakeLock.Acquire(); WifiManager wifiManager = (WifiManager)GetSystemService(WifiService); m_WifiLock = wifiManager.CreateWifiLock(WifiMode.FullHighPerf, "snapdotnet:SnapcastWifiWakeLock"); m_WifiLock.Acquire(); m_Host = host; m_Port = port; m_BroadcastMode = broadcastMode; // actually connect here and start audio loop AudioPlaybackCaptureConfiguration config = new AudioPlaybackCaptureConfiguration.Builder(m_MediaProjection) .AddMatchingUsage(AudioUsageKind.Media) .Build(); AudioFormat audioFormat = new AudioFormat.Builder() .SetEncoding(Encoding.Pcm16bit) .SetSampleRate(48000) .SetChannelMask(ChannelOut.Stereo).Build(); AudioRecord.Builder builder = new AudioRecord.Builder() .SetAudioFormat(audioFormat) .SetBufferSizeInBytes(2048); if (m_BroadcastMode == EBroadcastMode.Media) { builder.SetAudioPlaybackCaptureConfig(config); } if (m_BroadcastMode == EBroadcastMode.Microphone) { builder.SetAudioSource(AudioSource.Mic); } m_AudioRecord = builder.Build(); m_ConnectionThread = new Thread(() => { m_ClientConnection = new ClientConnection(m_Host, m_Port); m_ClientConnection.OnConnected += _OnClientConnectionConnected; m_ClientConnection.Connect(); }); m_ConnectionThread.Start(); m_Running = true; if (m_SnapclientListener != null) { m_SnapclientListener.OnBroadcastStateChanged(this); } } catch (Exception e) { e.PrintStackTrace(); if (m_SnapclientListener != null) { m_SnapclientListener.OnError(this, e.Message, e); } _Stop(); } }