예제 #1
0
        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);
            }
        }
예제 #2
0
        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;
        }
예제 #3
0
 private void Player_FinishedPlaying(object sender, AVStatusEventArgs e)
 {
     Player.Stop();
     Player.Dispose();
     mStatus = PlayStatus.Completed;
     PlayMp3Completed(this, e);
 }
예제 #4
0
        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();
        }
예제 #5
0
        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));
        }
예제 #6
0
        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);
        }
예제 #7
0
        /// <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();
        }
예제 #8
0
        /// <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();
        }
예제 #9
0
 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);
        }
예제 #11
0
파일: DeviceService.cs 프로젝트: uemegu/FNO
        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
            }
        }
예제 #12
0
        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();
        }
예제 #13
0
        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);
                }
            }
        }
예제 #14
0
        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;
            }
        }
예제 #15
0
        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();
     }
 }
예제 #17
0
 public void Stop()
 {
     if (player != null)
     {
         player.Stop();
         player.Dispose();
     }
 }
 public override bool Unload()
 {
     if (player != null)
     {
         player.Dispose();
     }
     return(true);
 }
예제 #19
0
 public void Dispose()
 {
     if (_player != null)
     {
         _player.Dispose();
         _player = null;
     }
 }
예제 #20
0
 public void StopSound()
 {
     // Any existing sound effect?
     if (soundEffect != null)
     {
         //Stop and dispose of any sound effect
         soundEffect.Stop();
         soundEffect.Dispose();
     }
 }
예제 #21
0
        private void ResetMusicPlayer()
        {
            currentMusic = null;

            if (audioPlayer != null)
            {
                audioPlayer.Dispose();
                audioPlayer = null;
            }
        }
예제 #22
0
 public void StopMusic()
 {
     if (backgroundMusic != null)
     {
         backgroundMusic.Stop();
         backgroundMusic.Dispose();
         backgroundMusic = null;
     }
     Song = null;
 }
예제 #23
0
        public bool Load(Stream audioStream)
        {
            var data = NSData.FromStream(audioStream);

            Stop();
            player?.Dispose();

            player = AVAudioPlayer.FromData(data);

            return((player == null) ? false : true);
        }
예제 #24
0
        public void Stop()
        {
            if (_player == null)
            {
                return;
            }

            _player.Stop();
            _player.Dispose();
            _player = null;
        }
예제 #25
0
        public void Dispose()
        {
            if (Player != null)
            {
                Player.DecoderError    -= Player_DecoderError;
                Player.FinishedPlaying -= Player_FinishedPlaying;
            }

            Player?.Dispose();
            Player = null;
        }
예제 #26
0
        void DeletePlayer()
        {
            Stop();

            if (player != null)
            {
                player.FinishedPlaying -= OnPlaybackEnded;
                player.Dispose();
                player = null;
            }
        }
예제 #27
0
        public void Play(string path)
        {
            if (player != null)
            {
                player.Stop();
                player.Dispose();
            }

            url    = new NSUrl(path);
            player = AVAudioPlayer.FromUrl(url);
            player.Play();
        }
예제 #28
0
        private void PlatformDispose(bool disposing)
        {
            if (_sound == null)
            {
                return;
            }

            _sound.FinishedPlaying -= OnFinishedPlaying;
            _sound.Dispose();

            _sound = null;
        }
예제 #29
0
파일: DeviceService.cs 프로젝트: uemegu/FNO
 private void StopPlayer()
 {
     if (player != null)
     {
         if (player.Playing)
         {
             player.Stop();
         }
         player.Dispose();
         player = null;
     }
 }
예제 #30
0
        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);
        }
예제 #31
0
		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 ();
		}