public void PlayAudio(string path) { try { path = System.IO.Path.Combine(System.IO.Path.GetTempPath(), path); // this line is added to place file in iOS temp directory, may not be required in Halza app if (player != null) { //Stop and dispose of any background music player.Stop(); player.Dispose(); } // Initialize background music NSUrl songURL = new NSUrl("Sounds/" + path); NSError err; player = new AVAudioPlayer(songURL, "acc", out err); player.FinishedPlaying += delegate { player = null; }; //player.NumberOfLoops = 0; player.Play(); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
public void PlayBackgroundMusic(string filename) { NSUrl songURL; // Music enabled? if (!MusicOn) { return; } // Any existing background music? if (backgroundMusic != null) { //Stop and dispose of any background music backgroundMusic.Stop(); backgroundMusic.Dispose(); } // Initialize background music songURL = new NSUrl("Son/" + filename); NSError err; backgroundMusic = new AVAudioPlayer(songURL, "mp3", out err); backgroundMusic.Volume = MusicVolume; backgroundMusic.FinishedPlaying += delegate { // backgroundMusic.Dispose(); backgroundMusic = null; }; backgroundMusic.NumberOfLoops = -1; backgroundMusic.Play(); backgroundSong = filename; }
private void Player_FinishedPlaying(object sender, AVStatusEventArgs e) { Player.Stop(); Player.Dispose(); mStatus = PlayStatus.Completed; PlayMp3Completed(this, e); }
public void Play() { NSUrl songURL; // Any existing background music? if (backgroundMusic != null) { //Stop and dispose of any background music backgroundMusic.Stop(); backgroundMusic.Dispose(); } // Initialize background music songURL = new NSUrl(filePath); NSError err; backgroundMusic = new AVAudioPlayer(songURL, "wav", out err); backgroundMusic.Volume = MusicVolume; backgroundMusic.FinishedPlaying += delegate { // backgroundMusic.Dispose(); backgroundMusic = null; }; backgroundMusic.NumberOfLoops = -1; backgroundMusic.Play(); }
public OperationResult <double> Initialize(string audioFilePath) { if (string.IsNullOrEmpty(audioFilePath) || !File.Exists(audioFilePath)) { return(OperationResult <double> .AsFailure("Invalid audio file path specified")); } var audioSession = AVAudioSession.SharedInstance(); var error = audioSession.SetCategory(AVAudioSessionCategory.Playback); if (error != null) { return(OperationResult <double> .AsFailure(error.LocalizedDescription)); } error = audioSession.SetActive(true); if (error != null) { return(OperationResult <double> .AsFailure(error.LocalizedDescription)); } NSUrl audioUrl = NSUrl.FromFilename(audioFilePath); if (_player != null) { _player.FinishedPlaying -= DidFinishPlaying; _player.Dispose(); _player = null; } _player = AVAudioPlayer.FromUrl(audioUrl); _player.NumberOfLoops = 0; _player.FinishedPlaying += DidFinishPlaying; return(OperationResult <double> .AsSuccess(_player.Duration)); }
public async Task <bool> PlayBackgroundMusic(string filename) { // Music enabled? if (!MusicOn || _backgroundMusicLoading) { return(false); } _backgroundMusicLoading = true; // Any existing background music? if (_backgroundMusic != null) { //Stop and dispose of any background music _backgroundMusic.Stop(); _backgroundMusic.Dispose(); } _backgroundSong = filename; // Initialize background music _backgroundMusic = await NewSound(filename, BackgroundMusicVolume, true); _backgroundMusicLoading = false; return(true); }
/// <summary> /// 播放音效 /// </summary> /// <param name="fileName"></param> public void PlaySoundEffect(string fileName) { if (mIsEffectsOn == false) { return; } if (mPlayer_SoundEffect != null) // Stop and dispose of any background music { mPlayer_SoundEffect.Stop(); mPlayer_SoundEffect.Dispose(); } // Initialize background music NSUrl songURL = new NSUrl("raw/{0}".FormatWith(fileName)); NSError err = null; mPlayer_SoundEffect = new AVAudioPlayer(songURL, "mp3", out err); mPlayer_SoundEffect.Volume = mBackgroundMusicVolume; mPlayer_SoundEffect.FinishedPlaying += delegate { mPlayer_SoundEffect = null; }; mPlayer_SoundEffect.NumberOfLoops = 0; mPlayer_SoundEffect.Play(); }
/// <summary> /// 播放背景音 /// </summary> /// <param name="fileName"></param> public void PlayBackgroundMusic(string fileName) { if (mIsBackgroundMusicOn == false) { return; } if (mPlayer_BackgroundMusic != null) // Stop and dispose of any background music { mPlayer_BackgroundMusic.Stop(); mPlayer_BackgroundMusic.Dispose(); } // Initialize background music NSUrl songURL = new NSUrl("raw/{0}".FormatWith(fileName)); NSError err = null; mPlayer_BackgroundMusic = new AVAudioPlayer(songURL, "mp3", out err); mPlayer_BackgroundMusic.Volume = mBackgroundMusicVolume; mPlayer_BackgroundMusic.FinishedPlaying += delegate { mPlayer_BackgroundMusic = null; }; mPlayer_BackgroundMusic.NumberOfLoops = -1; // 循环播放背景音 mPlayer_BackgroundMusic.Play(); }
protected override void DisposeManagedResources() { if (soundEffect != null) { soundEffect.Dispose(); } }
///<Summary> /// Load wave or mp3 audio file as a stream ///</Summary> public bool Load(Stream audioStream) { var data = NSData.FromStream(audioStream); Stop(); player?.Dispose(); player = AVAudioPlayer.FromData(data); if (player != null) { player.FinishedPlaying += OnPlaybackEnded; } return((player == null) ? false : true); }
public void StartPlayEffect(string title) { try { if (effect != null) { effect.Stop(); effect.Dispose(); effect = null; } var url = NSUrl.FromFilename(title + ".mp3"); NSError _err = null; effect = new AVAudioPlayer( url, AVFileType.MpegLayer3, out _err ); effect.NumberOfLoops = 0; effect.PrepareToPlay(); effect.Play(); } catch { //ignore } }
public void PlaySound(string fileName) { NSUrl songUrl; //Music enabled? if (!MusicOn) { return; } if (audio != null) { audio.Stop(); audio.Dispose(); } songUrl = new NSUrl("Sounds/" + fileName); NSError err; audio = new AVAudioPlayer(songUrl, "mp3", out err) { Volume = MusicVolume }; audio.FinishedPlaying += delegate { audio = null; }; audio.NumberOfLoops = 0; audio.Play(); }
public void PlayRecording(string path) { if (_audioPlayer != null) { _audioPlayer.Stop(); _audioPlayer.Dispose(); } if (File.Exists(Constants.INITIAL_AUDIO_FILE_PATH)) { _url = NSUrl.FromFilename(Constants.INITIAL_AUDIO_FILE_PATH); _audioPlayer = AVAudioPlayer.FromUrl(_url, out _error); _audioPlayer.Play(); _audioPlayer.FinishedPlaying += PlayCompletion; } if (File.Exists(path) && !File.Exists(Constants.INITIAL_AUDIO_FILE_PATH)) { try { ObjCRuntime.Class.ThrowOnInitFailure = false; _url = NSUrl.FromString(path); _audioPlayer = AVAudioPlayer.FromUrl(_url); _audioPlayer.Play(); _audioPlayer.FinishedPlaying += PlayCompletion; } catch (Exception e) { throw new Exception(e.Message); } } }
void Dispose(bool disposing) { if (!disposed) { if (disposing) { #if WINDOWS_MEDIA_SESSION if (_topology != null) { _topology.Dispose(); _topology = null; } #elif !WINDOWS_MEDIA_ENGINE if (_sound != null) { #if IOS _sound.FinishedPlaying -= OnFinishedPlaying; #endif _sound.Dispose(); _sound = null; } #endif } disposed = true; } }
public void PlaySound(string filename) { NSUrl songURL; // Music enabled? if (!EffectsOn) { return; } // Any existing sound effect? if (soundEffect != null) { //Stop and dispose of any sound effect soundEffect.Stop(); soundEffect.Dispose(); } // Initialize background music songURL = new NSUrl("Son/" + filename); NSError err; soundEffect = new AVAudioPlayer(songURL, "mp3", out err); soundEffect.Volume = EffectsVolume; soundEffect.FinishedPlaying += delegate { soundEffect = null; }; soundEffect.NumberOfLoops = 0; soundEffect.Play(); }
protected override void DisposeManagedResources() { if (music != null) { music.Dispose(); } }
public void Stop() { if (player != null) { player.Stop(); player.Dispose(); } }
public override bool Unload() { if (player != null) { player.Dispose(); } return(true); }
public void Dispose() { if (_player != null) { _player.Dispose(); _player = null; } }
public void StopSound() { // Any existing sound effect? if (soundEffect != null) { //Stop and dispose of any sound effect soundEffect.Stop(); soundEffect.Dispose(); } }
private void ResetMusicPlayer() { currentMusic = null; if (audioPlayer != null) { audioPlayer.Dispose(); audioPlayer = null; } }
public void StopMusic() { if (backgroundMusic != null) { backgroundMusic.Stop(); backgroundMusic.Dispose(); backgroundMusic = null; } Song = null; }
public bool Load(Stream audioStream) { var data = NSData.FromStream(audioStream); Stop(); player?.Dispose(); player = AVAudioPlayer.FromData(data); return((player == null) ? false : true); }
public void Stop() { if (_player == null) { return; } _player.Stop(); _player.Dispose(); _player = null; }
public void Dispose() { if (Player != null) { Player.DecoderError -= Player_DecoderError; Player.FinishedPlaying -= Player_FinishedPlaying; } Player?.Dispose(); Player = null; }
void DeletePlayer() { Stop(); if (player != null) { player.FinishedPlaying -= OnPlaybackEnded; player.Dispose(); player = null; } }
public void Play(string path) { if (player != null) { player.Stop(); player.Dispose(); } url = new NSUrl(path); player = AVAudioPlayer.FromUrl(url); player.Play(); }
private void PlatformDispose(bool disposing) { if (_sound == null) { return; } _sound.FinishedPlaying -= OnFinishedPlaying; _sound.Dispose(); _sound = null; }
private void StopPlayer() { if (player != null) { if (player.Playing) { player.Stop(); } player.Dispose(); player = null; } }
public void Prepare(string assetName, HandlePrepare prepareHandler) { if (mAudioPlayer != null) { mAudioPlayer.Stop(); mAudioPlayer.Dispose(); } bool result = false; int extStartDotIndex = assetName.LastIndexOf('.'); if (extStartDotIndex > 0) { string fileName = assetName.Substring(0, extStartDotIndex); string extName = assetName.Substring(extStartDotIndex); NSDataAsset assetData = new NSDataAsset(fileName); NSError error = null; if (assetData != null) { AVAudioSession.SharedInstance().SetCategory(AVAudioSessionCategory.Playback); AVAudioSession.SharedInstance().SetActive(true); mAudioPlayer = new AVAudioPlayer(assetData.Data, extName, out error); SetLooping(mLoop); SetVolume(mLeftVolume, mRightVolume); } result = error == null; if (result) { result = mAudioPlayer.PrepareToPlay(); } } prepareHandler?.Invoke(assetName, result); }
void RenderAudioAsync () { CFUrl sourceUrl = CFUrl.FromFile (NSBundle.MainBundle.PathForResource ("composeaudio", "mp3")); CFUrl destinationUrl = CFUrl.FromFile (System.IO.Path.Combine (System.Environment.GetFolderPath (Environment.SpecialFolder.Personal), "output.caf")); if (busy) return; busy = true; SetCaption ("Rendering..."); var thread = new System.Threading.Thread (() => { try { RenderAudio (sourceUrl, destinationUrl); } catch (Exception ex) { Console.WriteLine (ex); } BeginInvokeOnMainThread (() => { SetCaption ("Playing..."); using (var playUrl = new NSUrl (destinationUrl.Handle)) { player = AVAudioPlayer.FromUrl (playUrl); } player.Play (); player.FinishedPlaying += (sender, e) => { BeginInvokeOnMainThread (() => { player.Dispose (); player = null; SetCaption ("Render audio"); busy = false; }); }; }); }); thread.IsBackground = true; thread.Start (); }