My audio player on Android doesn't work - Xamarin - c#

I program C# Android app using Xamarin. I wrote this code:
protected MediaPlayer player;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.layout1);
this.Window.AddFlags(WindowManagerFlags.Fullscreen);
player = new MediaPlayer();
player.Reset();
var fileDescriptor = Assets.OpenFd("MySound.mp3");
player.SetDataSource(fileDescriptor.FileDescriptor);
player.Prepare();
player.Start();
}
MySound.mp3 file is direct in the Assets folder. When I run app there is an error:
Java.IO.IOException
Message=Prepare failed.: status=0x1
in the line with player.Prepare();
What is wrong? Why it's no working?

This seems to be a common exception with MediaPlayer.Prepare() on Android devices. A quick search turned up this:
https://social.msdn.microsoft.com/Forums/en-US/15f9c371-1b8d-4927-9555-f40e2829c377/mediaplayer-prepare-failed-stauts0x1?forum=xamarinandroid
Quote by Anonymous:
Hi, I wasted a lot of time, trying to find a solution to this issue on
my side too. So I post here, just in case it is helping some people.
My situation : I wanted to load an audio file from my assets (so not
registered in my resources). I am using a similar code as Ike Nwaogu,
except that I am using an AssetFileDescriptor to open my file (in my
activity class code, so I have access to "Assets") :
string path = "Audio/myfile.ogg";
Android.Content.Res.AssetFileDescriptor afd = Assets.OpenFd(path);
MediaPlayer soundPlayer = new MediaPlayer();
if (afd != null)
{
soundPlayer.Reset();
soundPlayer.SetDataSource(afd.FileDescriptor);
soundPlayer.Prepare();
soundPlayer.Enabled = true;
afd.Close();
}
I was failing on the Prepare(). I tried to add the access to external
storage permissions (but it did make sense since it
was loaded from my assets directly, I tried just in case).
Just by chance, by seeing other people samples on the forums, I added
the afd.StartOffset, afd.DeclaredLength to the parameters:
soundPlayer.SetDataSource(afd.FileDescriptor, afd.StartOffset,
afd.DeclaredLength);
and it worked ... I don't know if it just luck and if is going to fail
again later or if there is a bug in API ...
According to various sources, using getStartOffset and getLength when setting the DataSource should fix this:
player.setDataSource(fileDescriptor.getFileDescriptor(), fileDescriptor.getStartOffset(), fileDescriptor.getLength());
Alternatively, here are a few more ideas:
Android MediaPlayer throwing "Prepare failed.: status=0x1" on 2.1, works on 2.2
https://forum.processing.org/two/discussion/6722/andriod-mediaplayer-prepare-failed-status-0x1

Related

CSCore - not working for a specific device

i'm using CScore to play an mp3 file,
first, i have this on my public partial public MMDevice SelfDevice;
next, i get the default playback device with this code:-
MMDeviceEnumerator enumerator = new MMDeviceEnumerator();
SelfDevice = enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Console);
now selfDevice has the default playback device.
then, i run this code to play an mp3 file
if (CSCore.SoundOut.WasapiOut.IsSupportedOnCurrentPlatform)
_soundOutToSelf = new CSCore.SoundOut.WasapiOut() { Device = SelfDevice };
else
_soundOutToSelf = new DirectSoundOut();
var source = CodecFactory.Instance.GetCodec(pathToMP3())
.Loop()
.ChangeSampleRate(32000)
.ToSampleSource()
.AppendSource(Equalizer.Create10BandEqualizer)
.ToWaveSource();
_soundOutToSelf.Initialize(source);
_soundOutToSelf.Play();
_soundOutToSelf.Volume = 1;
MessageBox.Show(SelfDevice.FriendlyName);
it works, but only when i set my HDMI Audio as my default playback device, it plays no music when my Speakers are on default, also the messagebox is returning the right playback device name, so there's no problem with the device variable. what's the problem here?
also, i tried to make a new project and run the code there and it worked without any problem, so i reverted to an older version to the project when it was working, but now it's not working either.
i deleted the debug folder and the problem still occurs.
EDIT: i found out i can fix the problem by changing the assemblyname of the program, but i still don't know why windows is blocking my program?
changing the assemblyname or create a new project fixes the problem, still don't know what's the real cause.

I cannot get Xamarin to play small media files

Using an android scanner device, running KitKat.
Using Xamarin in Visual Studio Enterprise 2017, 15.9.9.
I need to generate a "Success" or "Error" sound, based on the content of the scanned barcode.
I have two files: "Success.mp3" and "Error.wav", neither of which will play.
Since this should be a very simple process, I am trying to use Android's MediaPlayer class, rather than add some NuGet package.
I am using Dependency Injection to properly run the Android code, since Xamarin does not have any sort or media API.
I instantiate Android's MediaPlayer as variable "player", and it does successfully instantiate, but as soon as I attempt to do anything with it, it throws a Null Exception error and the value of "player" displays as Null.
I have been experimenting with different ways to do this and have copies of the sound files stored both in the Assets folder, and the Resources/Raw folder (see below).
Here is my method:
public void PlaySound(string soundType) {
var filename =
global::Android.App.Application.Context.Assets.OpenFd(soundType);
if (player == null) {
MediaPlayer player = new MediaPlayer();
}
//This is where the error happens
player.SetDataSource(filename);
player.Prepare();
player.Start();
}
I have also tried the last three lines as the following, with the same result:
player.Prepared += (s, e) => {
player.Start();
};
player.SetDataSource(filename.FileDescriptor, filename.StartOffset,
filename.Length);
player.Prepare();
I have also attempted to utilize what so many people demonstrate as the way to do this, but it does not work for me. This is where the file must be stored in Resources/Raw:
player = MediaPlayer.Create(global::Android.App.Application.Context,
Resource.Raw.someFileName);
Whatever value that you use for "someFileName", all Visual Studio gives you is "'Resource.Raw' does not contain a definition for 'someFileName'".
Resource.designer.CS does contain entries for both files:
public const int Error = 2131230720;
public const int Success = 2131230721;
Expected results: sound, or some meaningful error message that puts me on the right path.
I am still relatively new to Xamarin and am probably missing something that would be obvious to veteran eyes. I have tried so many other things, most of which are not mentioned here, grasping for some straw. This should be simple, but is proving otherwise. Thank you for any help that you can provide.

Play audio from a stream in UWP

I'm basically asking the same thing that was asked here, however, that question was asked 8 years ago and the answer is no longer applicable to UWP.
I have a audio stream with http://someurl.com/stream that streams in audio/ogg format. I would like to be able to play that from an UWP app.
I see the NAudio library recommended a lot (after all, it's used in the above example), however it's very larger and has fairly lackluster documentation and very few up-to-date examples (they used to have a streaming example, but from what I'm able to download off Codeplex, it was replaced with a regular local-file player example). I'm not experience enough to make sense of the little documentation and example code they do have.
I'm honestly not even sure where to begin. I've never handled a stream like this (or any stream). Maybe the NAudio library isn't the way to go?
Code would be appreciated, but even pointers to sources where I could read up on playing such stream would be very helpful as my google-fu has failed me.
Thank you.
EDIT:
private void PlayMedia() {
System.Uri manifestUri = new Uri("http://amssamples.streaming.mediaservices.windows.net/49b57c87-f5f3-48b3-ba22-c55cfdffa9cb/Sintel.ism/manifest(format=m3u8-aapl)");
var mediaPlayer = new Windows.Media.Playback.MediaPlayer();
~~~~~~~~~~~~ -> "'Media Player' does not contain a constructor that takes 0 arguments."
mediaPlayer.Source = MediaSource.CreateFromUri(manifestUri);
mediaPlayer.Play();
}
but I can't get the MediaPlayer class to work. It says for example the x.Play() doesn't exist.
You have not posted your code segment. So I could not locate the problem of Visual Studio alerting "doesn't exist" accurately. If you want to use "MediaPlayer" class please add Windows.Media.Core and Windows.Media.Playback namespace at first. And you could reference the following code implementing a basic mediaplayer.
using Windows.Media.Core;
using Windows.Media.Playback;
......
private void PlayMedia()
{
System.Uri manifestUri = new Uri("http://amssamples.streaming.mediaservices.windows.net/49b57c87-f5f3-48b3-ba22-c55cfdffa9cb/Sintel.ism/manifest(format=m3u8-aapl)");
var mediaPlayer = new MediaPlayer();
mediaPlayer.Source = MediaSource.CreateFromUri(manifestUri);
mediaPlayer.Play();
}
The error message of Media Player does not contain a constructor that takes 0 arguments is means that there is no constructor with no arguments in the MediaPlayer class. Please try use the full name of constructor with namespace.
var mediaPlayer = new Windows.Media.Playback.MediaPlayer();

Embedding Cardboard profile in Unity3D

I want to avoid users having to scan the QRCode with the Cardboard Viewer profile on it so that they can just place their device into the viewer and be ready. The viewer configuration is constant and I have a profile URL for it. Is it possible to load this profile at the application start up in Unity3D and if so how should I do it?
I tried setting it up by loading the following line of code at the Start of a Script attached to a camera but without any luck:
Cardboard.SDK.DefaultDeviceProfile = new Uri(SpecificVRViewerProfileUrl);
I'm using Unity 5.3.1f1, Cardboard 0.6, and Vuforia.
I haven't used the Unity SDK for cardboard, only java, but looking through the sample (https://github.com/googlesamples/cardboard-unity/blob/master/Cardboard/Scripts/Cardboard.cs) you might want to try getting a device reference using BaseVRDevice. It's a class also in the samples (https://github.com/googlesamples/cardboard-unity/blob/master/Cardboard/Scripts/VRDevices/BaseVRDevice.cs)
// The VR device that will be providing input data.
private static BaseVRDevice device;
device = BaseVRDevice.GetDevice();
device.Init();
and then setting the device profile with this instead of Cardboard.SDK.SetDefaultDeviceProfile
public Uri DefaultDeviceProfile = new Uri("your URL here");
if (DefaultDeviceProfile != null) {
device.SetDefaultDeviceProfile(DefaultDeviceProfile);
}

Easiest way to play SOUND with MONO on OSX

I am writing a simple timer app for OSX10.6 using Mono. How can I play an alarm sound (it may be a wav/mp3 file or something else)?
I tried several ways, unfortunately none did work:
NSSound seems to be not supported by Mono yet.
MonoMac.AppKit.NSSound alarm = new MonoMac.AppKit.NSSound("alarm.wav");
alarm.Play();
Using a SoundPlayer didn't work either:
System.Media.SoundPlayer player = new System.Media.SoundPlayer("alarm.wav");
player.PlaySync();
I was able to play a sound by opening a System.Diagnostics.Process and then use the OSX command line command afplay. Unfortunately, this command opens in a new terminal window, which is quite annoying in a GUI app.
I realize there are CoreAudio bindings in Mono. But I have not figured out how to use them to play a sound.
You're using the wrong NSSound constructor is all.
new MonoMac.AppKit.NSSound("alarm.wav") expects an NSData (implicitly cast from string), you want to use new NSSound(string, bool). Probably want to pass false for the second parameter.
I threw together a quicky test project (based on the default MonoMac project) to confirm this works:
public override void FinishedLaunching (NSObject notification)
{
mainWindowController = new MainWindowController ();
mainWindowController.Window.MakeKeyAndOrderFront (this);
// Only lines added
var sound = new NSSound("/Users/kevinmontrose/Desktop/bear_growl_y.wav", byRef: false);
sound.Play();
}

Categories

Resources