internal override void LoadMusic(SoundMusic music)
        {
            loadTime.Restart();
            try
            {
                using (var javaFileStream = new FileInputStream(music.FileName))
                    mediaPlayer.SetDataSource(javaFileStream.FD, music.StartPosition, music.Length);

                mediaPlayer.PrepareAsync();

                CurrentMusic = music;
            }
            catch (IOException)
            {
                // this can happen namely if too many files are already opened (should not throw an exception)
                Logger.Warning("The audio file '{0}' could not be opened", music.FileName);
            }
            catch (SecurityException)
            {
                throw new InvalidOperationException("The sound file is not accessible anymore.");
            }
            catch (IllegalArgumentException e)
            {
                throw new AudioSystemInternalException("Error during the SetDataSouce: "+e);
            }
        }
Beispiel #2
0
        private void LoadNewMusic(SoundMusic lastPlayRequestMusicInstance)
        {
            if(audioPlayer != null)
                throw new AudioSystemInternalException("Tried to create a new AudioPlayer but the current instance was not freed.");

            currentMusic = lastPlayRequestMusicInstance;

            currentMusicDataTypeIsUnsupported = false;

            NSError loadError;

            // TODO: Avoid allocating twice the music size (i.e. by using NSData.FromBytesNoCopy on currentMusic.Stream.GetBuffer())
            currentMusic.Stream.Position = 0;
            audioPlayer = AVAudioPlayer.FromData(NSData.FromStream(currentMusic.Stream), out loadError);

            if (loadError != null)
            {
                if (loadError.Code == (int) AudioFileError.UnsupportedFileType || loadError.Code == (int) AudioFileError.UnsupportedDataFormat)
                {
                    currentMusicDataTypeIsUnsupported = true;
                    musicMediaEvents.Enqueue(new SoundMusicEventNotification(SoundMusicEvent.MetaDataLoaded, null));
                    return;
                }
                throw new AudioSystemInternalException("Music loading failed and failure was not handled. [Error="+loadError.LocalizedDescription+"].");
            }

            if (audioPlayer == null) // both audioPlayer and loadError are null (happened before when url was not correct) 
                throw new AudioSystemInternalException("Music loading failed and failure was not handled. [Unspecified Error].");

            audioPlayer.DecoderError += OnAudioPlayerDecoderError;
            audioPlayer.FinishedPlaying += OnAudioPlayerFinishedPlaying;

            if (!audioPlayer.PrepareToPlay())
            {
                // this happens sometimes when we put the application on background when starting to play.
                var currentMusicName = currentMusic.Name;
                currentMusic.SetStateToStopped();
                ResetMusicPlayer();

                Logger.Warning("The music '{0}' failed to prepare to play.", currentMusicName);
            }
            else
            {
                musicMediaEvents.Enqueue(new SoundMusicEventNotification(SoundMusicEvent.MetaDataLoaded, null));
                musicMediaEvents.Enqueue(new SoundMusicEventNotification(SoundMusicEvent.ReadyToBePlayed, null));
            }
        }
Beispiel #3
0
        /// <summary>
        /// Create and Load a sound music from an input file.
        /// </summary>
        /// <param name="engine">The audio engine in which to load the soundMusic</param>
        /// <param name="stream">The stream.</param>
        /// <returns>A new instance of soundMusic ready to be played</returns>
        /// <exception cref="System.ArgumentNullException">engine
        /// or
        /// filename</exception>
        /// <exception cref="System.ObjectDisposedException">The AudioEngine in which to create the voice is disposed.</exception>
        /// <exception cref="System.ArgumentException">engine or stream</exception>
        /// <exception cref="ObjectDisposedException">The AudioEngine in which to create the voice is disposed.</exception>
        /// <exception cref="ArgumentNullException">File ' + filename + ' does not exist.</exception>
        /// <remarks>On all platform the wav format is supported.
        /// For compressed formats, it is the task of the build engine to automatically adapt the original files to the best hardware specific format.</remarks>
        public static SoundMusic Load(AudioEngine engine, Stream stream)
        {
            if(engine == null)
                throw new ArgumentNullException("engine");

            if (stream == null)
                throw new ArgumentNullException("stream");

            if(engine.IsDisposed)
                throw new ObjectDisposedException("The AudioEngine in which to create the voice is disposed.");

            // TODO: Not portable on WindowsStore

            var ret = new SoundMusic();
            ret.AttachEngine(engine);
            ret.Load(stream);

            return ret;
        }
Beispiel #4
0
 /// <summary>
 /// Load a music to be played via <see cref="StartMusic"/>.
 /// </summary>
 /// <param name="music">Music to be played</param>
 internal abstract void LoadMusic(SoundMusic music);
Beispiel #5
0
 private void QueueLastPendingRequests(Dictionary<SoundMusicAction, bool> requestAftPlay, SoundMusic requester)
 {
     lock (musicActionRequests)
     {
         foreach (var action in requestAftPlay.Where(x => x.Value).Select(x => x.Key))
             musicActionRequests.Enqueue(new SoundMusicActionRequest(requester, action));
     }
 }
 internal override void LoadMusic(SoundMusic music)
 {
     CurrentMusic = music;
     
     mediaEngineEx.SetSourceFromByteStream(new ByteStream(CurrentMusic.Stream), "MP3");
 }
Beispiel #7
0
        internal override void PlayImpl()
        {
            AudioEngine.SubmitMusicActionRequest(new SoundMusicActionRequest(this, SoundMusicAction.Play));

            // Actual Playing is happening during the Audio Engine update
            // but we can not wait this long to update the PlayState of the currently playing SoundMusic
            // after this call to Play, PlayState of the previous playing music should directly be set to Stopped
            // this is why we use here the static field PreviousPlayingInstance
            lock (PreviousPlayingInstanceLock) // protection again possible future multithreading.
            {
                if (previousPlayingInstance != this)
                {
                    if (previousPlayingInstance != null)
                        previousPlayingInstance.SetStateToStopped();

                    previousPlayingInstance = this;
                }
            }
        }
Beispiel #8
0
 /// <inheritDoc/>
 internal override void LoadMusic(SoundMusic music)
 {
     throw new NotImplementedException();
 }
        /// <summary>
        /// Load a new music into the media session. That is create a new session and a new topology and set the topology of the session.
        /// </summary>
        /// <param name="music"></param>
        private void LoadNewMusic(SoundMusic music)
        {
            if (currentMusic != null || mediaSession != null)
                throw new AudioSystemInternalException("State of the audio engine invalid at the entry of LoadNewMusic.");

            music.Stream.Position = 0;
            mediaInputByteStream = new ByteStream(music.Stream);
            topology = CreateTopology(mediaInputByteStream, out mediaSource);
            MediaFactory.CreateMediaSession(null, out mediaSession);
            mediaSessionCallback = new MediaSessionCallback(mediaSession, OnMediaSessionEvent);
            mediaSession.SetTopology(SessionSetTopologyFlags.None, topology);

            currentMusic = music;
        }
Beispiel #10
0
 public SoundMusicActionRequest(SoundMusic requester, SoundMusicAction request)
 {
     Requester = requester;
     RequestedAction = request;
 }
Beispiel #11
0
 /// <summary>
 /// Load a music to be played via <see cref="StartMusic"/>.
 /// </summary>
 /// <param name="music">Music to be played</param>
 internal abstract void LoadMusic(SoundMusic music);
Beispiel #12
0
 private void QueueLastPendingRequests(Dictionary <SoundMusicAction, bool> requestAftPlay, SoundMusic requester)
 {
     lock (musicActionRequests)
     {
         foreach (var action in requestAftPlay.Where(x => x.Value).Select(x => x.Key))
         {
             musicActionRequests.Enqueue(new SoundMusicActionRequest(requester, action));
         }
     }
 }
 private void LoadNewMusic(SoundMusic lastPlayRequestMusicInstance)
 {
     currentMusic = lastPlayRequestMusicInstance;
     
     mediaEngineEx.SetSourceFromByteStream(new ByteStream(currentMusic.Stream), "MP3");
 }
        internal override void LoadMusic(SoundMusic music)
        {
            CurrentMusic = music;

            mediaEngineEx.SetSourceFromByteStream(new ByteStream(CurrentMusic.Stream), "MP3");
        }