public void Play(int loopsCount) { if (SDL_mixer.Mix_PlayMusic(sdlMusic, loopsCount) < 0) { throw new InvalidOperationException($"Cant play music: {SDL.SDL_GetError()}"); } }
internal void Play() { if (INTERNAL_mixMusic == IntPtr.Zero) { return; } musicFinishedDelegate = OnFinishedPlaying; SDL_mixer.Mix_HookMusicFinished(musicFinishedDelegate); SDL_mixer.Mix_PlayMusic(INTERNAL_mixMusic, 0); PlayCount += 1; }
public override void play() { if (SDL_mixer.Mix_PlayingMusic() == 0) { if (SDL_mixer.Mix_PlayMusic(music, 0) == -1) { Console.WriteLine($"There was an issue playing the music. {SDL.SDL_GetError()}"); Environment.Exit(-1); } } }
private void LoadAndPrepareResources() { music_mp3 = SDL_mixer.Mix_LoadMUS("ringtone.mp3"); if (music_mp3 == IntPtr.Zero) { Console.WriteLine("Failed to load mp3! SDL_mixer error {0}", SDL.SDL_GetError()); } SDL_mixer.Mix_PlayMusic(music_mp3, -1); music_wav = SDL_mixer.Mix_LoadWAV("beat.wav"); if (music_wav == IntPtr.Zero) { Console.WriteLine("Failed to load wav! SDL_mixer error {0}", SDL.SDL_GetError()); } font = SDL_ttf.TTF_OpenFont("lazy.ttf", 64); if (font == IntPtr.Zero) { Console.WriteLine("Failed to load lazy font! SDL_ttf Error: {0}", SDL.SDL_GetError()); } //Render text surface var textColor = new SDL.SDL_Color(); var textSurface = SDL_ttf.TTF_RenderText_Solid(font, "Hello SDL2 World!", textColor); if (textSurface == IntPtr.Zero) { Console.WriteLine("Unable to render text surface! SDL_ttf Error: {0}", SDL.SDL_GetError()); } texture_text = SDL.SDL_CreateTextureFromSurface(renderer, textSurface); if (texture_text == IntPtr.Zero) { Console.WriteLine("Unable to create texture from rendered text! SDL Error: {0}", SDL.SDL_GetError()); } s_text = Marshal.PtrToStructure <SDL.SDL_Surface>(textSurface); IntPtr image_png = SDL_image.IMG_Load("transparent.png"); if (image_png == IntPtr.Zero) { Console.WriteLine("Unable to load image! SDL_image Error: {0}", SDL.SDL_GetError()); } s_png = Marshal.PtrToStructure <SDL.SDL_Surface>(image_png); SDL.SDL_SetColorKey(image_png, (int)SDL.SDL_bool.SDL_TRUE, SDL.SDL_MapRGB(s_png.format, 0, 0xFF, 0xFF)); texture_png = SDL.SDL_CreateTextureFromSurface(renderer, image_png); if (texture_png == IntPtr.Zero) { Console.WriteLine("Unable to create texture from png! SDL Error: {0}", SDL.SDL_GetError()); } }
public override void PlayMusic(Music music) { if (music != this.music) { this.music = music; if (music != null) { SDL_mixer.Mix_VolumeMusic(musicVolume); SDL_mixer.Mix_PlayMusic(music.GetChunk(), -1); Logger.Info("Sound", $"Playing music {music}"); } else { SDL_mixer.Mix_HaltMusic(); } } }
public void ToggleMusic() { if (UxConfig.MusicOn) { SDL_mixer.Mix_HaltMusic(); } else { if (MusicPointer.HasValue) { SDL_mixer.Mix_PlayMusic(MusicPointer.Value, -1); } else { LoadAndPlayMusic(); } } UxConfig.MusicOn = !UxConfig.MusicOn; }
// Sound methods (for general sounds) public void PlaySound(IMixerSound sound, int channel = -1, bool propagateErrors = false) { int ret = -1; if (sound is MixerSound) { ret = SDL_mixer.Mix_PlayChannel(channel, sound.GetPointer(), 0); } else if (sound is MixerMusic) { ret = SDL_mixer.Mix_PlayMusic(sound.GetPointer(), 1); } else { throw new MixerException("Cannot play unknown object"); } if (propagateErrors && ret == -1) { throw new MixerException(); } }
public int LoopSound(IMixerSound sound, int channel = -1) { int ret = -1; if (sound is MixerSound) { ret = SDL_mixer.Mix_PlayChannel(channel, sound.GetPointer(), -1); } else if (sound is MixerMusic) { ret = SDL_mixer.Mix_PlayMusic(sound.GetPointer(), -1); } else { throw new MixerException("Cannot play unknown object"); } if (ret == -1) { throw new MixerException(); } return(ret); }
static void Main(string[] args) { FFI.CreateContext("Little Polygon C# Demo", 3 * 320, 3 * 115, ""); // PLAY SOME MUSIC var mus = SDL_mixer.Mix_LoadMUS("song.mid"); if (mus != IntPtr.Zero) { SDL_mixer.Mix_PlayMusic(mus, -1); } // BASIC EVENT LOOP var window = SDL.SDL_GL_GetCurrentWindow(); while (!gDone) { HandleEvents(); FFI.ClearScreen(); SDL.SDL_GL_SwapWindow(window); } FFI.DestroyContext(); }
internal static void JouerMusique(String musique) { SDL_mixer.Mix_PlayMusic(musiques[musique], -1); }
static void Main(string[] args) { if (args.Length != 1) { Console.WriteLine("Please pass in a mp3 file"); } if (SDL.SDL_Init(SDL.SDL_INIT_EVERYTHING) < 0) { Console.WriteLine(SDL.SDL_GetError()); Environment.Exit(1); } if (SDL_mixer.Mix_Init(SDL_mixer.MIX_InitFlags.MIX_INIT_MP3) < 0) { Console.WriteLine(SDL.SDL_GetError()); Environment.Exit(1); } IntPtr window = SDL.SDL_CreateWindow("Yay", SDL.SDL_WINDOWPOS_CENTERED, SDL.SDL_WINDOWPOS_CENTERED, 1024, 600, SDL.SDL_WindowFlags.SDL_WINDOW_SHOWN); if (window == IntPtr.Zero) { Console.WriteLine(SDL.SDL_GetError()); Console.WriteLine("Sad: Window"); Environment.Exit(1); } IntPtr renderer = SDL.SDL_CreateRenderer(window, -1, SDL.SDL_RendererFlags.SDL_RENDERER_ACCELERATED | SDL.SDL_RendererFlags.SDL_RENDERER_PRESENTVSYNC); if (renderer == IntPtr.Zero) { Console.WriteLine(SDL.SDL_GetError()); Console.WriteLine("Sad: Renderer"); Environment.Exit(1); } if (SDL_mixer.Mix_OpenAudio(44100, SDL_mixer.MIX_DEFAULT_FORMAT, 2, 2048) < 0) { Console.WriteLine(SDL.SDL_GetError()); Console.WriteLine("Sad: Mixer"); Environment.Exit(1); } bool mp3Done = false; IntPtr joy = SDL_mixer.Mix_LoadMUS(args[0]); if (joy == IntPtr.Zero) { Console.WriteLine(SDL.SDL_GetError()); Console.WriteLine("Sad: Mix_LoadMUS"); Environment.Exit(1); } MPGImport.mpg123_init(); Int32 err = 0; GCHandle handleErr = GCHandle.Alloc(err, GCHandleType.Pinned); IntPtr ptrErr = GCHandle.ToIntPtr(handleErr); IntPtr handle_mpg = MPGImport.mpg123_new(null, ptrErr); int x = MPGImport.mpg123_open(handle_mpg, args[0]); handleErr.Free(); MPGImport.mpg123_format_none(handle_mpg); MPGImport.mpg123_format(handle_mpg, 44100, 2, (int)MPGImport.mpg123_enc_enum.MPG123_ENC_SIGNED_16); int FrameSize = MPGImport.mpg123_outblock(handle_mpg); byte[] Buffer = new byte[FrameSize]; int lengthSamples = MPGImport.mpg123_length(handle_mpg); if (SDL_mixer.Mix_PlayMusic(joy, 0) < 0) { Console.WriteLine(SDL.SDL_GetError()); Console.WriteLine("Sad: Mix_PlayMusic"); Environment.Exit(1); } object fun = new object(); List <Int16> LeftChannels = new List <Int16>(); List <Int16> RightChannels = new List <Int16>(); SDL_mixer.Mix_HookMusic((uData, stream, len) => { byte[] mixData = new byte[len]; byte[] managedStream = new byte[len]; Marshal.Copy(stream, managedStream, 0, len); IntPtr done = IntPtr.Zero; if (MPGImport.mpg123_read(handle_mpg, managedStream, len, out done) is int hello) { if (hello == -12) { mp3Done = true; } else if (hello == 0) { // foreach (var b in managedStream.Select((value, idx) => (value, idx)).Where((value, idx) => idx % 4 == 0)) { Int16 leftChannel; Int16 rightChannel; byte[] datum = new byte[2]; datum[0] = managedStream[0]; datum[1] = managedStream[1]; leftChannel = BitConverter.ToInt16(datum, 0); datum[0] = managedStream[2]; datum[1] = managedStream[3]; rightChannel = BitConverter.ToInt16(datum, 0); leftChannel = (Int16)Math.Sqrt(leftChannel); rightChannel = (Int16)Math.Sqrt(rightChannel); lock (fun) { LeftChannels.Add(leftChannel); RightChannels.Add(rightChannel); } // } } } Marshal.Copy(managedStream, 0, stream, len); }, IntPtr.Zero); SDL.SDL_Event e; bool quit = false; while (!quit) { while (SDL.SDL_WaitEventTimeout(out e, 10) != 0) { switch (e.type) { case SDL.SDL_EventType.SDL_KEYDOWN: switch (e.key.keysym.sym) { case SDL.SDL_Keycode.SDLK_ESCAPE: quit = true; break; } break; case SDL.SDL_EventType.SDL_QUIT: quit = true; break; } } if (mp3Done) { quit = true; } else { SDL.SDL_RenderClear(renderer); SDL.SDL_SetRenderDrawColor(renderer, 218, 112, 214, 255); // 40 is perfect.. ;) lock (fun) { foreach (var channel in LeftChannels.Select((value, idx) => (value, idx))) { if (channel.idx > 40) { continue; } SDL.SDL_Rect rect = new SDL.SDL_Rect() { x = channel.idx * 20, y = 300, w = 15, h = -channel.value }; SDL.SDL_RenderDrawRect(renderer, ref rect); } if (LeftChannels.Count > 40) { LeftChannels.RemoveAt(0); } foreach (var channel in RightChannels.Select((value, idx) => (value, idx))) { if (channel.idx > 40) { continue; } SDL.SDL_Rect rect = new SDL.SDL_Rect() { x = channel.idx * 20, y = 315, w = 15, h = channel.value }; SDL.SDL_RenderDrawRect(renderer, ref rect); } if (RightChannels.Count > 40) { RightChannels.RemoveAt(0); } } SDL.SDL_Rect r = new SDL.SDL_Rect() { x = 0, y = 300, w = 1024, h = 10 }; // SDL.SDL_RenderClear(renderer); // SDL.SDL_SetRenderDrawColor(renderer, 218, 112, 214, 255); SDL.SDL_RenderDrawRect(renderer, ref r); SDL.SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); SDL.SDL_RenderPresent(renderer); } } SDL_mixer.Mix_FreeMusic(joy); SDL.SDL_Quit(); }
public void Play(int Loops = 0) { Util.ThrowIfResultIsError(SDL_mixer.Mix_PlayMusic(myPtr, Loops)); }
[JSFunction(Name = "playMusic")] public void PlayMusic(string audioPath) { SDL_mixer.Mix_PlayMusic(Disaster.Assets.Music(audioPath), 1); }
static int Main(string[] args) { SDL.SDL_SetHint(SDL.SDL_HINT_WINDOWS_DISABLE_THREAD_NAMING, "1"); Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture; Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; //Start up SDL and create window var success = Init(); if (success == false) { Console.WriteLine("Failed to initialize!"); } else { //Load media success = LoadMedia(); if (success == false) { Console.WriteLine("Failed to load media!"); } else { //Main loop flag bool quit = false; //While application is running while (!quit) { //Event handler SDL.SDL_Event e; //Handle events on queue while (SDL.SDL_PollEvent(out e) != 0) { //User requests quit if (e.type == SDL.SDL_EventType.SDL_QUIT) { quit = true; } //Handle key press else if (e.type == SDL.SDL_EventType.SDL_KEYDOWN) { switch (e.key.keysym.sym) { //Play high sound effect case SDL.SDL_Keycode.SDLK_1: SDL_mixer.Mix_PlayChannel(-1, _High, 0); break; //Play medium sound effect case SDL.SDL_Keycode.SDLK_2: SDL_mixer.Mix_PlayChannel(-1, _Medium, 0); break; //Play low sound effect case SDL.SDL_Keycode.SDLK_3: SDL_mixer.Mix_PlayChannel(-1, _Low, 0); break; //Play scratch sound effect case SDL.SDL_Keycode.SDLK_4: SDL_mixer.Mix_PlayChannel(-1, _Scratch, 0); break; case SDL.SDL_Keycode.SDLK_9: //If there is no music playing if (SDL_mixer.Mix_PlayingMusic() == 0) { //Play the music SDL_mixer.Mix_PlayMusic(_Music, -1); } //If music is being played else { //If the music is paused if (SDL_mixer.Mix_PausedMusic() == 1) { //Resume the music SDL_mixer.Mix_ResumeMusic(); } //If the music is playing else { //Pause the music SDL_mixer.Mix_PauseMusic(); } } break; case SDL.SDL_Keycode.SDLK_0: //Stop the music SDL_mixer.Mix_HaltMusic(); break; } } } //Clear screen SDL.SDL_SetRenderDrawColor(Renderer, 0xFF, 0xFF, 0xFF, 0xFF); SDL.SDL_RenderClear(Renderer); //Render prompt _PromptTexture.Render(0, 0); //Update screen SDL.SDL_RenderPresent(Renderer); } } } //Free resources and close SDL Close(); if (success == false) { Console.ReadLine(); } return(0); }
public void Play() { SDL_mixer.Mix_PlayMusic(streamHandle, 1); }