Why does PlaybackState remain at Playing after PlaybackStopped fires? - c#

I'm a little new to using NAudio so I'm surely missing something important, hence this question.
Starting with example/demo code, I put together a class as simple as I could make it to play MP3 or WAV files:
public class AudioPlayer : IDisposable
{
WaveStream _waveStream; // sound data stream
WaveChannel32 _waveChannel32; // sound channel ??
IWavePlayer _iWavePlayer; // sound output driver
public bool IsPlaying
{
get { return _iWavePlayer != null
&& _iWavePlayer.PlaybackState
!= Wave.PlaybackState.Stopped; }
}
public PlaybackState PlaybackState
{
get { return _iWavePlayer.PlaybackState; }
}
public void LoadMp3(byte[] mp3Bytes)
{
Load(CreateInputStreamFromMp3Bytes(mp3Bytes));
}
public void LoadFile(string filename)
{
Load(CreateInputStream(filename));
}
void Load(WaveStream waveStream)
{
if (_iWavePlayer != null)
Dispose();
_iWavePlayer = new WaveOut();
_iWavePlayer.PlaybackStopped +=
new EventHandler(_iWavePlayer_PlaybackStopped);
_waveStream = waveStream;
_waveChannel32 = new WaveChannel32(_waveStream);
_waveChannel32.PadWithZeroes = false;
_iWavePlayer.Init(_waveChannel32);
}
void _iWavePlayer_PlaybackStopped(object sender, EventArgs e)
{
Stop();
}
static WaveStream CreateInputStreamFromMp3Bytes(byte[] mp3Bytes)
{
return new Mp3FileReader(new MemoryStream(mp3Bytes), false);
}
static WaveStream CreateInputStream(string fileName)
{
if (fileName.EndsWith(".wav"))
return new WaveFileReader(fileName);
else if (fileName.EndsWith(".mp3"))
return new Mp3FileReader(fileName);
else
throw new InvalidOperationException("Unsupported extension");
}
public void Play()
{
_iWavePlayer.Play();
}
public void Stop()
{
if (_iWavePlayer != null
&& _iWavePlayer.PlaybackState != PlaybackState.Stopped) {
_iWavePlayer.Stop();
_waveStream.Position = 0;
}
}
public void Dispose()
{
Stop();
if (_iWavePlayer != null) {
_iWavePlayer.Dispose();
_iWavePlayer = null;
}
if (_waveChannel32 != null) {
_waveChannel32.Dispose();
_waveChannel32 = null;
}
if (_waveStream != null) {
_waveStream.Dispose();
_waveStream = null;
}
}
}
I'm using the code in question to play MP3 files (not WAVs).
It works ok for the most part, which is pretty awesome considering that I'll be able to replace MediaElements in my project. The issue I'm having is that the PlaybackState stays at Playing rather than changing to Stopped once PlaybackStopped fires. To go around that, I subscribe to PlaybackStopped and call Stop() from there.
Is it normal that I have to call Stop() like that, or is there is something I am missing here?

Looks like a bug. If you look at NAudio code WaveOut will not change PlaybackState in RaisePlaybackStoppedEvent. But DirectSoundOut will change it to stopped. A little crazy.

Try this:
In the Load method change this line:
_iWavePlayer = new WaveOut();
by this:
_iWavePlayer = new WaveOut(WaveCallbackInfo.FunctionCallback());

Related

Xamarin IOS: unable to play mp3 from internet

I read a lot of documentation regarding this problem, but I don't seen any answer.
I have a Xamarin form app that play mp3 sample music from the Groove music service.
On Android, everyhting workd fine.
On Ios, I'm not able to play the sound. I get the URL and it's not possible to ear anything (I was able to play a local mp3)
I see this problem occured also for people using SWIFT : How to play MP3 From URL in iOS
I also found several exemple on the Xamarin forum but no sound neither: https://forums.xamarin.com/discussion/19883/how-do-i-get-the-avplayer-to-play-an-mp3-from-a-remote-url
Here is the code I use:
public class AudioService : IAudio
{
protected string FileName = string.Empty;
protected AVPlayer Player;
public bool IsPlaying { get; private set; }
public void Init(string fileName)
{
//FileName = fileName;
string second = "http://progdownload.zune.net/145/119/034/170/audio.mp3?rid=0b80911a-ba3b-42ec-b17f-c242ba087024-s4-nl-BE-music-asset-location";
FileName = second;
QueueFile();
}
public async void PlayStream(string uri)
{
Init(uri);
System.Diagnostics.Debug.WriteLine("Enter in function");
Player.Play();
System.Diagnostics.Debug.WriteLine("Play sound");
System.Diagnostics.Debug.WriteLine(FileName);
IsPlaying = true;
System.Diagnostics.Debug.WriteLine(IsPlaying);
}
public void Pause()
{
IsPlaying = false;
Player.Pause();
}
public void Stop()
{
IsPlaying = false;
if (Player == null) return;
Player.Dispose();
Player = null;
}
public bool HasFile
{
get { return Player != null; }
}
private void QueueFile()
{
if (string.IsNullOrWhiteSpace(FileName))
{
throw new Exception("No file specified to play");
}
using (var url = NSUrl.FromString(FileName))
{
var test = AVAsset.FromUrl(url);
var playerItem = AVPlayerItem.FromAsset(test);
// if Player is null, we're creating a new instance and seeking to the spot required
// otherwise we simply resume from where we left off.
if (Player == null)
{
Player = AVPlayer.FromPlayerItem(playerItem);
if (Player == null)
{
// todo: what should we do if the file doesn't exist?
return;
}
}
}
}}
(IAudio just implements playstream and stop)
If you click on the url, your browser will be able to play music
http://progdownload.zune.net/145/119/034/170/audio.mp3?rid=0b80911a-ba3b-42ec-b17f-c242ba087024-s4-nl-BE-music-asset-location
Does any one was able to play mp3 from internet
The answer in the info.list file. You need to add this to your info.list:
<key>NSAppTransportSecurity</key><dict>
<key>NSAllowsArbitraryLoads</key>
<true/></dict>
I had the same issue and resolved it.
I've made some cleaning for your code :)
This is what we got, including the detecting the end of playing
public class AudioService
{
#region Members
protected AVPlayer Player;
#endregion
#region Properties
public bool IsPlaying { get; private set; }
public bool HasFile => Player != null;
public Action MediaEnded { get; set; }
#endregion
#region Public Methods
public void PlayStream(string uri)
{
try
{
if (Player != null)
Player.Dispose();
Player = null;
QueueFile(uri);
Player.Play();
IsPlaying = true;
}
catch (Exception ex)
{
IsPlaying = false;
Crashes.TrackError(ex);
}
}
public void Pause()
{
IsPlaying = false;
Player.Pause();
}
public void Stop()
{
IsPlaying = false;
if (Player == null)
return;
Player.Dispose();
Player = null;
}
public void QueueFile(string fileName)
{
if (string.IsNullOrWhiteSpace(fileName))
{
return;
}
using (var url = NSUrl.FromString(fileName))
{
var asset = AVAsset.FromUrl(url);
var playerItem = AVPlayerItem.FromAsset(asset);
if (Player == null)
{
Player = AVPlayer.FromPlayerItem(playerItem);
if (Player == null)
{
return;
}
else
{
NSNotificationCenter.DefaultCenter.AddObserver(AVPlayerItem.DidPlayToEndTimeNotification, OnPlayerDidFinishPlaying, NSObject.FromObject(playerItem));
}
}
}
}
#endregion
#region Private Methods
private void OnPlayerDidFinishPlaying(NSNotification notification)
{
IsPlaying = false;
MediaEnded?.Invoke();
}
#endregion

Design solution for working with multiple instantiations at the same time

I don't know if my title is correct. But here's what I want to know.
I have a Download class that returns certain events and has a couple of methods. Each instance of Download class can download a single file. And all those events and methods are related to the file being downloaded.
As it's a multi file downloader, multiple instantiations are required when more than a single file needs to be downloaded.
Each download has a download id, but that is not supplied to the Download class, to keep it independent from the other classes.
Now getting all the info from each instance of the file download and being able to control a single download, is the problem. How do I know which download is which?
Any solutions? Or design patterns you could recommend? I've hit a roadblock.
Download class:
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Threading;
namespace Mackerel_Download_Manager
{
public class Download
{
public event EventHandler<DownloadStatusChangedEventArgs> ResumablityChanged;
public event EventHandler<DownloadProgressChangedEventArgs> ProgressChanged;
public event EventHandler Completed;
public bool stop = true; // by default stop is true
public bool paused = false;
SemaphoreSlim pauseLock = new SemaphoreSlim(1);
string filename;
public void DownloadFile(Uri DownloadLink, string Path)
{
filename = System.IO.Path.GetFileName(Path);
stop = false; // always set this bool to false, everytime this method is called
var fileInfo = new FileInfo(Path);
long existingLength = 0;
if (fileInfo.Exists)
existingLength = fileInfo.Length;
var request = (HttpWebRequest)HttpWebRequest.Create(DownloadLink);
request.Proxy = null;
request.AddRange(existingLength);
try
{
using (var response = (HttpWebResponse)request.GetResponse())
{
long fileSize = existingLength + response.ContentLength; //response.ContentLength gives me the size that is remaining to be downloaded
bool downloadResumable; // need it for not sending any progress
if ((int)response.StatusCode == 206) //same as: response.StatusCode == HttpStatusCode.PartialContent
{
//Console.WriteLine("Resumable");
downloadResumable = true;
}
else // sometimes a server that supports partial content will lose its ability to send partial content(weird behavior) and thus the download will lose its resumability
{
if (existingLength > 0)
{
if (ResumeUnsupportedWarning() == false) // warn and ask for confirmation to continue if the half downloaded file is unresumable
{
return;
}
}
existingLength = 0;
downloadResumable = false;
}
OnResumabilityChanged(new DownloadStatusChangedEventArgs(downloadResumable));
using (var saveFileStream = fileInfo.Open(downloadResumable ? FileMode.Append : FileMode.Create, FileAccess.Write))
using (var stream = response.GetResponseStream())
{
byte[] downBuffer = new byte[4096];
int byteSize = 0;
long totalReceived = byteSize + existingLength;
var sw = Stopwatch.StartNew();
while (!stop && (byteSize = stream.Read(downBuffer, 0, downBuffer.Length)) > 0)
{
saveFileStream.Write(downBuffer, 0, byteSize);
totalReceived += byteSize;
float currentSpeed = totalReceived / (float)sw.Elapsed.TotalSeconds;
OnProgressChanged(new DownloadProgressChangedEventArgs(totalReceived, fileSize, (long)currentSpeed));
pauseLock.Wait();
pauseLock.Release();
}
sw.Stop();
}
}
if (!stop) OnCompleted(EventArgs.Empty);
}
catch (WebException e)
{
System.Windows.MessageBox.Show(e.Message, filename);
}
}
public void pause()
{
if (!paused)
{
paused = true;
// Note this cannot block for more than a moment
// since the download thread doesn't keep the lock held
pauseLock.Wait();
}
}
public void unpause()
{
if (paused)
{
paused = false;
pauseLock.Release();
}
}
public void StopDownload()
{
stop = true;
this.unpause(); // stop waiting on lock if needed
}
public bool ResumeUnsupportedWarning()
{
var messageBoxResult = System.Windows.MessageBox.Show("When trying to resume the download , Mackerel got a response from the server that it doesn't support resuming the download. It's possible that it's a temporary error of the server, and you will be able to resume the file at a later time, but at this time Mackerel can download this file from the beginning.\n\nDo you want to download this file from the beginning?", filename, System.Windows.MessageBoxButton.YesNo);
if (messageBoxResult == System.Windows.MessageBoxResult.Yes)
{
return true;
}
else
{
return false;
}
}
protected virtual void OnResumabilityChanged(DownloadStatusChangedEventArgs e)
{
var handler = ResumablityChanged;
if (handler != null)
{
handler(this, e);
}
}
protected virtual void OnProgressChanged(DownloadProgressChangedEventArgs e)
{
var handler = ProgressChanged;
if (handler != null)
{
handler(this, e);
}
}
protected virtual void OnCompleted(EventArgs e)
{
var handler = Completed;
if (handler != null)
{
handler(this, e);
}
}
}
public class DownloadStatusChangedEventArgs : EventArgs
{
public DownloadStatusChangedEventArgs(bool canResume)
{
ResumeSupported = canResume;
}
public bool ResumeSupported { get; private set; }
}
public class DownloadProgressChangedEventArgs : EventArgs
{
public DownloadProgressChangedEventArgs(long totalReceived, long fileSize, long currentSpeed)
{
BytesReceived = totalReceived;
TotalBytesToReceive = fileSize;
CurrentSpeed = currentSpeed;
}
public long BytesReceived { get; private set; }
public long TotalBytesToReceive { get; private set; }
public float ProgressPercentage
{
get
{
return ((float)BytesReceived / (float)TotalBytesToReceive) * 100;
}
}
public float CurrentSpeed { get; private set; } // in bytes
public TimeSpan TimeLeft
{
get
{
var bytesRemainingtoBeReceived = TotalBytesToReceive - BytesReceived;
return TimeSpan.FromSeconds(bytesRemainingtoBeReceived / CurrentSpeed);
}
}
}
}
Download class is instantiated inside a Mackerel class, that starts the download for the given downloads.
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
namespace Mackerel_Download_Manager
{
public static class Mackerel
{
//Main Menu functions
public static void ResumeDownload(string[] DownloadIDs)
{
foreach (var DownloadID in DownloadIDs)
{
var itemToResume = Downloads.DownloadEntries.Where(download => download.DownloadID == DownloadID).FirstOrDefault();
if (itemToResume.Running == false)
{
itemToResume.Running = true;
var download = new Download();
download.DownloadFile(itemToResume.DownloadLink, itemToResume.SaveTo);
var window = new Dialogs.DownloadProgress(itemToResume);
window.Show();
double progress = 0;
itemToResume.Status = string.Format("{0:0.00}%", progress);
Downloads.DownloadEntries.CollectionChanged += delegate
{
if (!itemToResume.Running) window.Close();
};
}
}
}
public static void StopDownload(string[] DownloadIDs)
{
foreach (var DownloadID in DownloadIDs)
{
var itemToStop = Downloads.DownloadEntries.Where(download => download.DownloadID == DownloadID).FirstOrDefault();
if (itemToStop.Running == true)
itemToStop.Running = false;
}
}
public static void StopAllDownloads()
{
foreach (var itemToStop in Downloads.DownloadEntries.Where(download => download.Running == true))
itemToStop.Running = false;
}
public static void RemoveDownload(string[] DownloadIDs) // this method is able to delete multiple downloads
{
foreach (var DownloadID in DownloadIDs)
{
// delete from the download list
var selectedDownload = Downloads.DownloadEntries.Where(download => download.DownloadID == DownloadID).FirstOrDefault();
var selectedDownloadIndex = Downloads.DownloadEntries.IndexOf(selectedDownload);
Downloads.DownloadEntries.RemoveAt(selectedDownloadIndex);
//delete from the harddrive
if (File.Exists(selectedDownload.SaveTo))
File.Delete(selectedDownload.SaveTo);
}
Downloads.Serialize(); // save current state of object
}
public static void RemoveCompletedDownloads() // this method just removes all completed downloads from Mackerel's download list (it doesn't delete them from the hard drive)
{
foreach (var itemToRemove in Downloads.DownloadEntries.Where(download => download.Status == "Complete").ToList())
{
Downloads.DownloadEntries.Remove(itemToRemove);
}
}
// Context Menu
public static void OpenDownloadProperties(string DownloadID) // Open "Download Properties" for the given download ID
{
var DownloadProperties = new Dialogs.Context_Menu.DownloadProperties(DownloadID);
DownloadProperties.Owner = Application.Current.MainWindow; // so that this dialog centers to its parent window, as its window is set to WindowStartupLocation="CenterOwner"
DownloadProperties.ShowDialog();
}
}
}
Full source code is here: https://github.com/Expenzor/mackerel-download-manager
Sounds like your making the download itself an object but are using "downloader" as the name instead. I would maybe suggest an array of downloaded objects or similar. The downloader class can have a method which creates a new object - perhaps a download object. Using an array or linked list etc will give you the opportunity to address the object and call them independently - know which is which.
Posting your code would help as well.

Xamarin - MediaPicker not showing the videoPicker for iOS

I tried doing this in my main app I'm writing but can't seem to get it to work. So I created a Single View Application to try it out and figure it out but I still seem to be stuck. He's what i got.
public partial class HelloViewController : UIViewController
{
partial class VideoPickerController : UIViewController
{
MediaPicker videoPicker;
readonly TaskScheduler uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();
public VideoPickerController (IntPtr handle) : base (handle)
{
}
public override void DidReceiveMemoryWarning ()
{
// Releases the view if it doesn't have a superview.
base.DidReceiveMemoryWarning ();
// Release any cached data, images, etc that aren't in use.
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
var picker = new MediaPicker ();
PresentViewController (picker.GetPickPhotoUI (), true, null);
}
}
}
Basically all I get is a white screen and nothing happens. Thanks!
I don't think you need need to to manually present it. This is code that I used and it automatically presents the UI for me. This code is not in it's own viewcontroller, it is just in whatever viewcontroller that I have the button it to take the pic. There are similar methods for the photo album and for taking videos.
Here is a link to the github page where I have this sample app hosted.
var picker = new Xamarin.Media.MediaPicker();
btnCamera.Hidden = !picker.IsCameraAvailable;
btnCamera.TouchUpInside += async (sender, e) =>
{
try
{
MediaFile file =
await picker.TakePhotoAsync(new StoreCameraMediaOptions());
processImage(file);
}
catch { }
};
btnPhoto.Hidden = !picker.PhotosSupported;
btnPhoto.TouchUpInside += async (sender, e) =>
{
try
{
MediaFile file = await picker.PickPhotoAsync();
processImage(file);
}
catch { }
};
private void processImage(MediaFile file)
{
if (file != null)
{
viewModel.Image = file.GetStream();
viewModel.ImagePath = file.Path;
setImage();
}
}
private void setImage()
{
try
{
System.Diagnostics.Debug.WriteLine(viewModel.ImagePath);
if (!string.IsNullOrEmpty(viewModel.ImagePath) &&
System.IO.File.Exists(viewModel.ImagePath))
{
imgImage.Image = new UIImage(NSData.FromFile(viewModel.ImagePath));
}
else if (viewModel.Image != null && viewModel.Image.Length != 0)
{
imgImage.Image = new UIImage(NSData.FromStream(viewModel.Image));
}
}
//just don't load image
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
}

C# read write lock on list

Desription:
I am adding some data to list every second. After every 10 seconds, the data is saved to the database and then I clear the list. If I stop the timer, I am saving the data remaining in the list and then clearing the list and then stopping the timer.
In the above code, Let's say when I stop the timer after 11 seconds, The Class1s list should have only 1 data, but I see there are 11 datas. Can you guys tell what I am doing wrong here? Maybe my use of lock is incorrect or my code is totally incorrect
public class Class1Singleton
{
private static Class1Singleton Class1Singleton;
private static List<Class1> Class1s;
private static Timer saveClass1Timer;
private static readonly object lock1 = new object();
private Class1Singleton()
{
}
public static Class1Singleton getInstance()
{
if (Class1Singleton == null) {
try
{
Class1Singleton = new Class1Singleton();
}
catch (Exception e){}
}
return Class1Singleton;
}
public void StartTimer()
{
if (saveClass1Timer == null)
{
saveClass1Timer = new Timer(10000);
//saveClass1Timer.Interval = 10000;
saveClass1Timer.Elapsed += new ElapsedEventHandler(SaveClass1);
saveClass1Timer.Enabled = true;
}
}
public void SaveClass1(object sender, ElapsedEventArgs e)
{
try {
lock (lock1)
{
new Class1Repository().InsertAllClass1(Class1s);
ClearWorkoutList();
}
}
catch (Exception ex){}
}
public void InsertClass1(List<Class1> Class1)
{
if (Class1s == null)
{
Class1s = new List<Class1>(Class1);
}
else
{
lock (lock1)
{
Class1s.AddRange(Class1);
}
}
}
public void ClearWorkoutList()
{
if (Class1s != null)
{
Class1s.Clear();
}
}
public void StopTimer()
{
if (Class1s != null && Class1s.Count > 0)
{
lock (lock1)
{
new Class1Repository().InsertAllClass1(Class1s);
ClearWorkoutList();
}
}
if (saveClass1Timer != null && saveClass1Timer.Enabled == true)
{
saveClass1Timer.Stop();
}
}
}
I've made a few changes to your code (see comments):
public class Class1Singleton
{
// This is the way Jon Skeet recommends implementing a singleton in C#
// See http://csharpindepth.com/Articles/General/Singleton.aspx
static readonly Class1Singleton instance = new Class1Singleton();
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
static Class1Singleton() { }
// There's only one instance of Class1Singleton so there's
// no advantage in making the members static
private List<Class1> Class1s;
private Timer saveClass1Timer;
private readonly object lock1 = new object();
Class1Singleton()
{
}
public static Class1Singleton Instance
{
get { return instance; }
}
public void StartTimer()
{
// If you're using this class in a multi-thread environment,
// all methods that access the list or timer should be locked
lock (lock1)
{
if (saveClass1Timer == null)
{
saveClass1Timer = new Timer(10000);
//saveClass1Timer.Interval = 10000;
saveClass1Timer.Elapsed += new ElapsedEventHandler(SaveClass1);
saveClass1Timer.Enabled = true;
}
}
}
// SaveClass1 doesn't need to be public
private void SaveClass1(object sender, ElapsedEventArgs e)
{
lock (lock1)
{
SaveWorkoutList();
ClearWorkoutList();
}
}
private void SaveWorkoutList()
{
//new Class1Repository().InsertAllClass1(Class1s);
}
public void InsertClass1(List<Class1> Class1)
{
lock (lock1)
{
if (Class1s == null)
Class1s = new List<Class1>(Class1);
else
Class1s.AddRange(Class1);
}
}
private void ClearWorkoutList()
{
if (Class1s != null)
{
Class1s.Clear();
}
}
public void StopTimer()
{
lock (lock1)
{
if (Class1s != null && Class1s.Count > 0)
{
SaveWorkoutList();
ClearWorkoutList();
}
if (saveClass1Timer != null && saveClass1Timer.Enabled == true)
{
saveClass1Timer.Stop();
}
}
}
}

WaveOut versus WaveOutEvent

I'm using NAudio with
WaveOutEvent Klangwiedergabegeraet;
private void Play(string Dateiname)
{
Klangwiedergabegeraet = new WaveOutEvent();
Klangwiedergabegeraet.DeviceNumber = comboBox1.SelectedIndex;
ISampleProvider StueckchenHalter = null;
StueckchenHalter = CreateInputStreamS(Dateiname);
Klangwiedergabegeraet.Init(new SampleToWaveProvider(StueckchenHalter));
Klangwiedergabegeraet.Play();
}
private void Cancel()
{
if (Klangwiedergabegeraet != null)
{
Klangwiedergabegeraet.Stop();
Klangwiedergabegeraet.Dispose();
}
}
When running Cancel(), it won't stop. When I used
WaveOut Klangwiedergabegeraet;
private void Play(string Dateiname)
{
Klangwiedergabegeraet = new WaveOut();
...
}
private void Cancel()
{
if (Klangwiedergabegeraet != null)
{
Klangwiedergabegeraet.Stop();
Klangwiedergabegeraet.Dispose();
}
}
It worked. Why is this and what to change?
P.S I am using WaveOutEvent instead of WaveOut because WaveOut does not have the property DeviceNumber.
If you are running on a non-GUI thread, then WaveOutEvent is the recommended way to do things, not WaveOut as that will choose function callbacks which can be unreliable on some soundcards. Also, WaveOut does have a DeviceNumber property.

Categories

Resources