Xamarin Android App not streaming online radio stream - c#

So I'm trying to make a Android app for an online radio and no matter how much I use the guides on Xamarin, it's not streaming.
My code: http://pastebin.com/K20RX8Yk
The coding environment I am using is Xamarin.Android and I am coding in the Xamarin Studio. I've been using the Android emu running API 16.
I have already tried different path files but sadly that hasn't fixed it.

I would advice to use the MediaPlayer to stream audio on Android, as it is much easier to use then AudioTrack. Potentially AudioTrack could have better performance but you need to do quite some stuff yourself to get it working correctly.
Here is a blog post with example how to use MediaPlayer on Xamarin Android: http://blog.xamarin.com/background-audio-streaming-with-xamarin.android/
A quick example out of that is:
private const string Mp3 = #"http://167.88.113.131:8000/;stream.mp3";
private MediaPlayer player;
private void IntializePlayer()
{
player = new MediaPlayer();
//Tell our player to sream music
player.SetAudioStreamType(Stream.Music);
}
private async void Play()
{
if (player == null) {
IntializePlayer();
}
try {
await player.SetDataSourceAsync(ApplicationContext, Android.Net.Uri.Parse(Mp3));
player.PrepareAsync();
player.Start();
}
catch (Exception ex) {
//unable to start playback log error
Console.WriteLine("Unable to start playback: " + ex);
}
}

Related

How to read data from an NFC device using C# android?

I am trying to read data from an NFC credit card. The app is written using the .NET Maui framework. Right now I am trying to use the Plugin.NFC NuGet package because it seems to be the only one.
Here is the code I have so far:
private TagInfo tmpData;
private static void ReadTagEvent(ITagInfo data)
{
tmpData = (TagInfo)data;
}
public static NFC_READ_ERROR ReadNFC(ref TagInfo result, Page page)
{
if (!IsReady())
{
return NFC_READ_ERROR.NOT_ENABLED;
}
tmpData = null;
CrossNFC.Current.StartListening();
CrossNFC.Current.OnMessageReceived += ReadTagEvent;
page.DisplayAlert("Info", "Starting listening for NFC...", "Ok");
while (tmpData == null)
{
Thread.Sleep(50);
}
page.DisplayAlert("Info", "tmpData != null", "Ok");
CrossNFC.Current.StopListening();
result = tmpData;
if (tmpData.IsEmpty)
{
return NFC_READ_ERROR.NO_DATA;
}
return NFC_READ_ERROR.SUCCESS;
}
This runs and displays the first alert. But the result of tagInfo saved to tmpData is still null. Because ReadTagEvent is never triggered. After the Start listening is called. And the device is in close proximity to the android phone it beeps so I know that it is listening. I have confirmed that the device has data and is working properly by testing it on another app. The second alert is never displayed. By the way, this code is run inside a Thread in the app's Display Page. Any help and code would be appreciated. Thanks.
Add NFC Permissions in your AndroidManifest.xml
&ltuses-permission android:name="android.permission.NFC" />
&ltuses-feature android:name="android.hardware.nfc" android:required="false" />
Add the line CrossNFC.Init(this) in your OnCreate().
protected override void OnCreate(Bundle savedInstanceState)
{
CrossNFC.Init(this);
base.OnCreate(savedInstanceState);
[...]
}
3. Add the line CrossNFC.OnResume() in your OnResume()
// Plugin NFC: Restart NFC listening on resume (needed for Android 10+)
4. Add the line CrossNFC.OnNewIntent(intent) in your OnNewIntent()
// Plugin NFC: Tag Discovery Interception
Plugin.NFC https://github.com/franckbour/Plugin.NFC

how do i get Android.Media.SetPreferredDevice() to work

So making a mobile application that works on UWP, IOS and Android but since not all librarys work on every platform I'm using the library based on what device is used by
if (Device.RuntimePlatform == Device.Android) { }
And I'm currently only working on the Android part of the application.
I'm using Android.Media to play a single audio file out of multiple speakers. And to do that I'm using a Picker that has the available audio output devices. This part works.
But I'm getting a error while trying to select the PreferredDevice:
Java.Lang.NoSuchMethodError: 'no non-static method "Landroid/media/MediaPlayer;.setPreferredDevice(Landroid/media/AudioDeviceInfo;)Z"'
The code line that is giving the error is:
mediaPlayer1.SetPreferredDevice(audioDeviceInfo);
the full method that is being run is:
newoutput.SelectedIndexChanged += (changed, args) =>
{
Context context = Android.App.Application.Context;
AudioManager audioMan = (AudioManager)context.GetSystemService(Context.AudioService);
AudioDeviceInfo audioDeviceInfo = audioMan.GetDevices(GetDevicesTargets.Outputs)[newoutput.SelectedIndex];
mediaPlayer1.SetPreferredDevice(audioDeviceInfo);
};
I can't find many examples that use the method and they don't usually go with a mediaplayer that is created by button press.
You can use this code
private AudioDeviceInfo findAudioDevice(int deviceType) {
AudioManager manager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
AudioDeviceInfo[] adis = manager.getDevices(GET_DEVICES_OUTPUTS);
for (AudioDeviceInfo adi : adis) {
if (adi.getType() == deviceType) {
return adi;
}
}
return null;
}
Then set your input:
audioRecord.setPreferredDevice(findAudioDevice([newoutput.SelectedIndex]));

EmguCV capture rtsp ip camera stream ffmpeg

I am working with IP camera and EmguCV to detect person in an area.
Everything is work normally with Highgui capture source.
But with Arlo camera which use FFMPEG format, I am unable to get stream without any exception.
Here is the code I have tried:
if (capture == null)
{
try
{
capture = new Capture("rtsp://<IP>:8554/live");
capture.ImageGrabbed += Capture_ImageGrabbed;
capture.Grab();
capture.Start();
}
catch (NullReferenceException exception)
{
MessageBox.Show(exception.Message);
}
catch (TypeInitializationException exc)
{
MessageBox.Show(
"Attention: You have to copy all the assemblies and native libraries from an official release of EmguCV to the directory of the demo." +
Environment.NewLine + Environment.NewLine + exc);
}
}
and here is Capture_ImageGrabbed event which is not fired.
private void Capture_ImageGrabbed(object sender, EventArgs e)
{
Mat image = new Mat();
capture.Retrieve(image);
picCAM.Image = image.Bitmap;
}
Thanks so much for your help!
This might be old, but until now I'm also facing the same issue, even using the newest version (which is version 3.3). Probably not the best solution, but after took some trial and error, what I did is to downgrade to version 2.4.2. You can download here.
I also recently submitted this issue in github. This is probably a bug.

Receiving data via bluetooth to c# program

I'm using inTheHand library (32feet.NET).
I have a device with bluetooth enabled and I want to connect with the device to my computer and than send data from the device to computer. I then want to catch that information with my program and process it.
The device will send me 3 variables (all 3 float).
How do I catch that information with bluetooth? I never worked with bluetooth on a computer before.
I tried like this post said:
Pair bluetooth devices to a computer with 32feet .NET Bluetooth library
But don't know what I'm doing, so I can't make it work.
I'm using Visual Studio 2017 and Windows 10. I heard there are problems for Windows 10 and authenticating bluetooth devices.
Thanks for all the help!
UPDATE:
static string domaciaddress = "MY_ADDRESS";
static string tujadress = "DEVICE_ADDRESS";
//reciever
private static BluetoothEndPoint EP = new BluetoothEndPoint(BluetoothAddress.Parse(domaciaddress), BluetoothService.BluetoothBase);
private static BluetoothClient BC = new BluetoothClient(EP);
//sender
private static BluetoothDeviceInfo BTDevice = new BluetoothDeviceInfo(BluetoothAddress.Parse(tujadress));
private static NetworkStream stream = null;
static void neke231(string[] args)
{
string paircode = "paircode";
if (BluetoothSecurity.PairRequest(BTDevice.DeviceAddress, paircode))
{
Console.WriteLine("PairRequest: OK");
if (BTDevice.Authenticated)
{
Console.WriteLine("Authenticated: OK");
BC.SetPin(paircode);
BC.BeginConnect(BTDevice.DeviceAddress, BluetoothService.SerialPort, new AsyncCallback(Connect), BTDevice);
}
else
{
Console.WriteLine("Authenticated: No");
}
}
else
{
Console.WriteLine("PairRequest: No");
}
Console.ReadLine();
}
I now connect to my bluetooth like this. But I still don't know how to get those 3 floats that my device sends, and save it here in float, so I can later in program use them.
EDIT:
This code in fact doesn't work exactly... I don't know why, but it won't connect to android phone. When I run program instead of getting what I write into console, I get only BluetoothGetDeviceInfo returned: 0x80070057

Can't open a mpg-File for capture using EmguCV with C#

I'm trying to open a mpg-File using emguCV. I use the following code:
if (instance == null)
{
lock (m_lock)
{
try
{
instance = new Capture(0); // capture from camera works fine if a camera is connected
}
catch (NullReferenceException)
{
String sFileName = #"C:\tmp\MarkerMovie.mpg";
try
{
if (File.Exists(sFileName))
{
instance = new Capture(sFileName); // here the exception is thrown
}
else
{
MessageBox.Show("No Camera and no Video-File found");
}
}
catch (NullReferenceException)
{
MessageBox.Show("Couldn't open Video: "+sFileName);
}
}
}
}
If a webcam is connected everything works fine, but when I unplug the webcam the line instance = new Capture(sFileName); throws a NullReferenceException:
Message = "Unable to create capture from C:\tmp\MarkerMovie.mpg"
I debugged and found the reason is in the constructor of capture. The following command always returns a Null-Pointer:
_ptr = CvInvoke.cvCreateFileCapture(fileName);
I could open the same video using C++ using this code:
cap = cvCaptureFromFile("C:\\tmp\\MarkerMovie.mpg");
I'm new to openCV, so I'm not sure which information you need to help me. I installed emguCV yesterday from http://sourceforge.net/projects/emgucv/ on a Windows XP computer. The installer-version is x86_2.3.0.1416. I included opencv_core231.dll, opencv_highgui231.dll and opencv_imgproc231.dll to my project.
Does somebody know how I can make this code working?
Let me know if you need more information.
Thanks.
I had the same problem and adding the opencv_ffmpeg.dll to the project seems to solve it. (Found in the bin directory in the Emgu CV directory) I have tried it on the project that you posted and it seems to be working too. Hope it helps.
With EmguCV in C#, you need to put the opencv_ffmpeg.dll, for example: opencv_ffmpeg2410.dll, be careful if you have x86 or 64 bits

Categories

Resources