Play audio url using xamarin MediaPlayer - c#

Why xamarin MediaPlayer (on Xamarin.Android) can play audio as a stream from a link like this (mediaUrl1) :
https://ia800806.us.archive.org/15/items/Mp3Playlist_555/AaronNeville-CrazyLove.mp3
But can't do it from a link like this (mediaUrl2):
http://api-streaming.youscribe.com/v1/products/2919465/documents/3214936/audio/stream
private MediaPlayer player;
//..
player = new MediaPlayer();
player.SetAudioStreamType(Stream.Music);
//..
await player.SetDataSourceAsync(ApplicationContext, Android.Net.Uri.Parse(mediaUrl));
//..
player.PrepareAsync();
//..
Is there a way to play the link above (mediaUrl2) without (of course) downloading the file first?
Here is the full source of the sample i am using. Any help would be appreciated.

http://api-streaming.youscribe.com/v1/products/2919465/documents/3214936/audio/stream
That is an HTTP mpga stream and is not directly supported by any of the Android APIs that I know of and thus is not supported by MediaPlayer (consult the Android Support Media Formats for further reading).
You can review the logcat output of your MediaPlayer code and you will see output like:
[MediaPlayerNative] start called in state 4, mPlayer(0x8efb7240)
[MediaPlayerNative] error (-38, 0)
[MediaPlayer] Error (-38,0)
[MediaHTTPConnection] readAt 1161613 / 32768 => java.net.ProtocolException
[MediaHTTPConnection] readAt 1161613 / 32768 => java.net.ProtocolException
[MediaPlayerNative] error (1, -2147483648)
[MediaPlayer] Error (1,-2147483648)
Google's Android ExoPlayer can stream that media format properly.
This is a really simple and very crude example of ExoPlayer, but it will show you that it does play that stream:
ExoPlayer Example:
var mediaUrl = "http://api-streaming.youscribe.com/v1/products/2919465/documents/3214936/audio/stream";
var mediaUri = Android.Net.Uri.Parse(mediaUrl);
var userAgent = Util.GetUserAgent(this, "ExoPlayerDemo");
var defaultHttpDataSourceFactory = new DefaultHttpDataSourceFactory(userAgent);
var defaultDataSourceFactory = new DefaultDataSourceFactory(this, null, defaultHttpDataSourceFactory);
var extractorMediaSource = new ExtractorMediaSource(mediaUri, defaultDataSourceFactory, new DefaultExtractorsFactory(), null, null);
var defaultBandwidthMeter = new DefaultBandwidthMeter();
var adaptiveTrackSelectionFactory = new AdaptiveTrackSelection.Factory(defaultBandwidthMeter);
var defaultTrackSelector = new DefaultTrackSelector(adaptiveTrackSelectionFactory);
exoPlayer = ExoPlayerFactory.NewSimpleInstance(this, defaultTrackSelector);
exoPlayer.Prepare(extractorMediaSource);
exoPlayer.PlayWhenReady = true;
Note: exoPlayer is a class-level variable of SimpleExoPlayer type
Note: this is using the Xamarin.Android binding libraries from the Xam.Plugins.Android.ExoPlayer package
ExoPlayer Docs:
https://developer.android.com/guide/topics/media/exoplayer

Related

Transcoding mxf video file with CopyAudio in Azure Media Services v3

We are using Azure Media Services V3 API to transcode video files of various input formats into mp4 output. In case we have a mxf input file we receive the following exception when trying to transcode video with audio codec 'CopyAudio'): Azure Media ReEncode error message: An error has occurred. Stage: ApplyEncodeCommand. Code: 0x00000001.
This is the same issue as mentioned here (Copy audio codec throws exception when transcoding mxf video file), but for the v2 API of Azure Media Services
The answer given there is indeed the solution for Azure Media Services v2.
I'm having trouble to port it to the v3 API though. In code we are creating an instance of StandardEncoderPreset (Microsoft.Azure.Management.Media.Models.StandardEncoderPreset) and try to use the CopyAudio codec. Currently I am unable to figure out how to specify the MOVFormat there.
StandardEncoderPreset preset = new StandardEncoderPreset(
codecs: new List<Codec>()
{
new H264Video
{
KeyFrameInterval = TimeSpan.FromSeconds(2),
SceneChangeDetection = true,
//PreserveResolutionAfterRotation = true,
Layers = new[]
{
new H264Layer
{
Profile = H264VideoProfile.Auto,
Level = "Auto",
Bitrate = bitrate,
MaxBitrate = bitrate,
BufferWindow = TimeSpan.FromSeconds(5),
Width = width.ToString(),
Height = height.ToString(),
BFrames = 3,
ReferenceFrames = 3,
FrameRate = "0/1",
AdaptiveBFrame = true
}
}
}, new CopyAudio()
},
// Specify the format for the output files - one for video+audio, and another for the thumbnails
formats: new List<Format>()
{
new Mp4Format()
{
FilenamePattern = "{Basename}_" + width + "x" + height +"_{Bitrate}.mp4"
}
}
With the preset configured like that I get the same error as mentioned in the original post. CopyAudio only has a property 'Label'.
Also been thinking we need to specify an extra format in the list of 'Formats' but I can't find a MOVFormat (or PCMFormat) class.
Our v3 APIs do not yet support writing to MOV output file format. You would need to go with v2 APIs for such Jobs.

libVLCSharp fail to create MediaList

I'am playing arround with libVLCSharp and I found a wired behavior. Actually I have no problem creating a media and playing it with MediaPlayer. However when I try to create a Media from MediaList it breaks with the followig message:
Failed to perform instanciation on the native side. Make sure you
installed the correct VideoLAN.LibVLC.[YourPlatform] package in your
platform specific project
What I wanted to achive is video merging using ":sout=#gather" pipe.
My code is very basic :
Core.Initialize();
using (var libvlc = new LibVLC())
using (var mediaPlayer = new MediaPlayer(libvlc))
{
Media media1 = new Media(libvlc, #"C:\Temp\SampleVideo.mp4");
Media media2 = new Media(libvlc, #"C:\Temp\SampleVideo.mp4");
MediaList list = new MediaList(libvlc);
list.AddMedia(media1);
list.AddMedia(media2);
Media mediaList = new Media(list); <-- Error here
...
}
I have 2 nuget packages used in my project:
LibVLCSharp v3.0.2 June 12
VideoLAN.LibVLC.Windows v3.0.7 June 10
You want to be using SetMedia for this, not AddMedia.
Associate media instance with this media list instance.
https://www.videolan.org/developers/vlc/doc/doxygen/html/group__libvlc__media__list.html#ga96a38e5aabb5781c2f1932d332363eef
Core.Initialize();
using(var libVLC = new LibVLC())
{
var media1 = new Media(libVLC, "https://download.blender.org/peach/bigbuckbunny_movies/BigBuckBunny_320x180.mp4", FromType.FromLocation);
var mediaList = new MediaList(libVLC);
mediaList.SetMedia(media1);
var media2 = new Media(mediaList);
}

Mirror the image shown in preview zxing when scanning with front camera

I have this small scanner app built in UWP using zxing.net.mobile and cannot find a way to mirror image shown in preview while using front camera.
At the moment everything is working, just it is really hard to aim the QR code to the camera, because movement is inverse, not like in the mirror.
I couldn't find anything on internet, all results involve android or xamarin which is not what I look for. If any code or pics are required please ask. Any help will be appreciated
var options = new ZXing.Mobile.MobileBarcodeScanningOptions { TryHarder = true, AutoRotate = true, InitialDelayBeforeAnalyzingFrames = 3000, UseFrontCameraIfAvailable = true};
options.PossibleFormats = new List<BarcodeFormat>() {
BarcodeFormat.QR_CODE };
var scanner = new ZXing.Mobile.MobileBarcodeScanner
{
TopText = "Turēt, lai noskenētu."
//BottomText = "Tally Scan"
};
var result = await scanner.Scan(options);

Playing Music from resources C#

I want to play some background music inside the launcher for my new project. But I get an error.
private void playlooping()
{
SoundPlayer.PlayLooping(system.Resources.Bgm);
}
//Bgm is the name of the song.
For some reason I get the error
no overload for method 'PlayLooping' Takes 1 arguments
As I mentioned in the comment, PlayLooping does not take any arguments. So you need to specify the sound you want to play somewhere else. This is done either by setting it via the SoundPlayer-Constructor like this:
// via string path
var soundPlayer = new SoundPlayer(#"C:\somePath\someFile.wav");
// via stream
var soundPlayer = new SoundPlayer(musicStream);
So if your resource is a string you should be good to go with:
var soundPlayer = new SoundPlayer(system.Resources.Bgm);
Alternative is to set the string-path or stream after instantiating your SoundPlayer via the Site- or Stream-Properties of your SoundPlayer object:
var soundPlayer = new SoundPlayer();
// via path
soundPlayer.Site = #"C:\path\test.wav";
// via stream
soundPlayer.Stream = someStream;
After setting this up correclty you should be good calling PlayLooping. So your final code should be looking like this (works for me in a test windows forms application, with the soundfile simply put in my debug folder):
var soundPlayer = new SoundPlayer("test.wav");
soundPlayer.PlayLooping();
You can find the full documentation on the SoundPlayer here: https://msdn.microsoft.com/en-us/library/system.media.soundplayer(v=vs.110).aspx

Storing RTSP to a file location

I am able to stream an rtsp on windows 7 64 bit machine through C# Winform application. This is the library i used - VLCDotNet and here is the code sample to play the RTSP stream:
LocationMedia media = new LocationMedia(#"rtsp://192.168.137.73:554/live.sdp");
vlcControl1.Media = media;
vlcControl1.Play();
I would like to store the streams to a file in my PC on a button click and stop the same with another button. How do i achieve this?
Here is the code:
Vlc.DotNet.Core.Medias.MediaBase media1
= new Vlc.DotNet.Core.Medias.PathMedia("rtsp://192.168.137.73:554/live.sdp");
media.AddOption(":sout=#transcode{vcodec=theo,vb=800,
scale=1,acodec=flac,ab=128,channels=2,samplerate=44100}:std{access=file,mux=ogg,
dst=D:\\123.mp4}");
VlcControl control = new VlcControl();
control.Media = media;
control.Play();
VlcContext.StartupOptions.IgnoreConfig = true;
VlcContext.StartupOptions.LogOptions.LogInFile = true;
VlcContext.StartupOptions.LogOptions.ShowLoggerConsole = true;
VlcContext.StartupOptions.LogOptions.Verbosity = VlcLogVerbosities.Debug;
// Disable showing the movie file name as an overlay
// VlcContext.StartupOptions.AddOption("--no-video-title-show");
// VlcContext.StartupOptions.AddOption("--no-audio");
VlcContext.StartupOptions.AddOption("--rtsp-tcp"); //this line was important to make this work
As of Vlc.DotNet.Core 2.1.62, The way to do this is use the extra opts param of the .Play on the vlc control.
var opts = new string[] { #":sout=file/ogg:C:\video.ogg" };
vlc.MediaPlayer.Play(new Uri(videoURI), opts);
`

Categories