├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── audioplayer ├── audioplayer.hpp ├── internal │ ├── audiodevices.hpp │ └── callbacks.hpp └── main.cpp ├── example ├── .gitignore ├── .metadata ├── README.md ├── lib │ └── main.dart ├── linux │ ├── .gitignore │ ├── CMakeLists.txt │ ├── flutter │ │ ├── CMakeLists.txt │ │ ├── generated_plugin_registrant.cc │ │ ├── generated_plugin_registrant.h │ │ └── generated_plugins.cmake │ ├── main.cc │ ├── my_application.cc │ └── my_application.h ├── pubspec.lock ├── pubspec.yaml └── windows │ ├── .gitignore │ ├── CMakeLists.txt │ ├── flutter │ ├── .template_version │ ├── CMakeLists.txt │ ├── generated_plugin_registrant.cc │ ├── generated_plugin_registrant.h │ └── generated_plugins.cmake │ └── runner │ ├── CMakeLists.txt │ ├── Runner.rc │ ├── flutter_window.cpp │ ├── flutter_window.h │ ├── main.cpp │ ├── resource.h │ ├── resources │ └── app_icon.ico │ ├── run_loop.cpp │ ├── run_loop.h │ ├── runner.exe.manifest │ ├── utils.cpp │ ├── utils.h │ ├── win32_window.cpp │ └── win32_window.h ├── flutter_audio_desktop.iml ├── lib ├── flutter_audio_desktop.dart └── source │ ├── core │ ├── channel.dart │ ├── devices.dart │ └── events.dart │ ├── main.dart │ └── types │ ├── audio.dart │ └── source.dart ├── linux ├── CMakeLists.txt ├── flutter_audio_desktop_plugin.cc └── include │ └── flutter_audio_desktop │ ├── flutter_audio_desktop_plugin.h │ └── flutter_types.hpp ├── pubspec.lock ├── pubspec.yaml └── windows ├── .gitignore ├── CMakeLists.txt ├── flutter_audio_desktop_plugin.cpp └── include └── flutter_audio_desktop ├── flutter_audio_desktop_plugin.h └── flutter_types.hpp /.gitignore: -------------------------------------------------------------------------------- 1 | # vscode 2 | 3 | .vscode 4 | 5 | # audio 6 | 7 | *.mp3 8 | 9 | # dart 10 | 11 | .dart_tool 12 | .packages 13 | 14 | # flutter 15 | 16 | .flutter-plugins 17 | .flutter-plugins-dependencies 18 | 19 | # miniaudio: https://github.com/mackron/miniaudio 20 | # Get miniaudio.h & miniaudio_engine.h from the link above & place at the following location. 21 | audioplayer/miniaudio/* -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## 0.1.0 2 | 3 | - Now you can listen to playback events using `stream` (a broadcast stream) inside `AudioPlayer`. This was a great problem in earlier version as one always needs to trigger UI updates whenever playback progresses, ends etc. 4 | - One of the big problems in earlier version was that there was no way to detect if an audio playback has ended after completion. Resulting in issues like #25 & #26. Now `audio.isCompleted` stores `bool` if an audio has ended, same can be accessed from `stream`. 5 | - Added a new `Queue` class to play audio files sequentially, without having to deal with things like `audio.isCompleted` etc. manually. 6 | - Added methods to deal with `Queue` or repeat playback etc. 7 | - Now you can provide any random `id` while creating new instance of `AudioPlayer`, this was a big problem earlier as new `id` had to be consecutive to earlier one. 8 | - Now you can access same instance of `AudioPlayer` even if you make new constructor, by providing same `id`. 9 | - Now asset files can be played & loaded into `AudioPlayer` using `load` method. 10 | - `AudioSource` class has two static methods 11 | - `AudioSource.fromFile` to load an audio file. 12 | - `AudioSource.fromAsset` to load an audio asset. 13 | - Now audio field stores `Audio` object, inside the AudioPlayer class & contains following fields to get information about current playback. 14 | - `file`: Current loaded `File`. 15 | - `isPlaying` : Whether file is playing. 16 | - `isCompleted`: Whether file is ended playing. 17 | - By default once playback is ended, `stop` method is called & `AudioPlayer` is reverted to initial configuration. 18 | - `isStopped`: Whether file is loaded. 19 | - `position`: Position of current playback in `Duration`. 20 | - `duration`: Duration of current file in `Duration`. 21 | - Now contructor of `AudioPlayer` no longer calls async methods, which could result in false assertions. 22 | - Now `ma_resource_manager` is used from `miniaudio_engine` with `MA_DATA_SOURCE_FLAG_STREAM` flag. 23 | - This will improve general performance during playback, as whole file will not be loaded into memory. 24 | - Structure of code improved & separated into various files & classes. 25 | - Now device handling is present in an entirely separate class `AudioDevices`. 26 | - Improvements to how methods are identified & called in method channel. `flutter_types.hpp` improves code readability. 27 | - Other bugs that randomly caused termination after false assertions are also fixed to an extent. 28 | - Removed wave & noise APIs temporarily. Apologies to everyone & [MichealReed](https://github.com/MichealReed). 29 | 30 | ## 0.0.9 31 | 32 | - Missed 33 | 34 | ## 0.0.8 35 | 36 | **Multiple player instances, wave & noise methods** 37 | 38 | - Now multiple AudioPlayer instances can be made, by providing optional id parameter to the constructor. 39 | - Added methods for playing waves. 40 | - Added methods for playing noise. 41 | 42 | ## 0.0.7 43 | 44 | **Initial Playback Device Changing Support** 45 | 46 | - Added setDevice method to AudioPlayer class. 47 | 48 | ## 0.0.6 49 | 50 | **Microsoft Windows Support** 51 | 52 | - Plugin is now capable of playing audio files on Windows. 53 | 54 | ## 0.0.5 55 | 56 | **A Little Fix** 57 | 58 | - pub package now has miniaudio in it. 59 | 60 | ## 0.0.4 61 | 62 | **Final Improvements** 63 | 64 | - Now plugin uses MethodChannel instead of dart:ffi for calling native methods. 65 | - Any additional setup is not required anymore. 66 | 67 | ## 0.0.3 68 | 69 | **First Public Release** 70 | 71 | - Added docstrings. 72 | - Improved dart usage. 73 | - Fixed wrong sample rate. 74 | - Now Dart code is asynchronous. 75 | 76 | 77 | ## 0.0.2 78 | 79 | **Now Fully Open Source** 80 | 81 | Changed native code to use [miniaudio](https://github.com/mackron/miniaudio) 82 | 83 | 84 | ## 0.0.1 85 | 86 | **Initial Release** 87 | 88 | Supports audio playback on Linux. 89 | 90 | Added mandatory audio playback functions like: 91 | - Loading audio file 92 | - Playing 93 | - Pausing 94 | - Getting duration of an audio file. 95 | - Seeking 96 | - Changing volume 97 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Hitesh Kumar Saini 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ✒ [libwinmedia](https://github.com/harmonoid/libwinmedia) is sequel to this project. 2 | #### It provides network playback, better format support, control & features. 3 |

4 |

5 |

6 |

7 |

8 |

9 |

10 | #### An audio playback library for Flutter Desktop. 11 | 12 | Feel free to open issue anytime. 13 | 14 | 15 | ## Installing 16 | 17 | Mention in your pubspec.yaml: 18 | 19 | ```yaml 20 | dependencies: 21 | ... 22 | flutter_audio_desktop: ^0.1.0 23 | ``` 24 | 25 | ## Using 26 | 27 | ```dart 28 | // Create new instance. 29 | var audioPlayer = new AudioPlayer(id: 0) 30 | ..stream.listen( 31 | (Audio audio) { 32 | // Listen to playback events. 33 | }, 34 | ); 35 | // Load audio source 36 | audioPlayer.load( 37 | new AudioSource.fromFile( 38 | new File('/home/alexmercerind/music.mp3'), 39 | ), 40 | ); 41 | // Start playback. 42 | audioPlayer.play(); 43 | // Get audio duration. 44 | audioPlayer.audio.duration; 45 | // Change playback volume. 46 | audioPlayer.setVolume(0.5); 47 | // Change playback position. 48 | audioPlayer.setPosition(Duration(seconds: 10)); 49 | // Get playback position. 50 | audioPlayer.audio.position; 51 | Timer(Duration(seconds: 10), () { 52 | // Pause playback. 53 | audioPlayer.pause(); 54 | } 55 | // Few other things. 56 | audioPlayer.audio.file; 57 | audioPlayer.audio.isPlaying; 58 | audioPlayer.audio.isCompleted; 59 | audioPlayer.audio.isStopped; 60 | 61 | ``` 62 | 63 | Other classes & methods are documented in their docstrings very well. 64 | 65 | See [this](https://github.com/alexmercerind/flutter_audio_desktop/blob/master/example/lib/main.dart) example for a better overview. 66 | 67 | #### Windows 68 | 69 | 70 | 71 | #### Linux 72 | 73 | 74 | 75 | ## Support 76 | 77 | If you want to be kind to me, then consider buying me a coffee. 78 | 79 | 80 | 81 | Thankyou! 82 | 83 | 84 | ## Progress 85 | 86 | |Platform |Status | 87 | |--------------------|----------------------------------------------------------| 88 | |Linux |Working | 89 | |Microsoft Windows |Working | 90 | |MacOS |[Learn More](https://www.youtube.com/watch?v=dQw4w9WgXcQ) | 91 | 92 | 93 | ## License 94 | 95 | I don't want to put any restrictions on how you distribute your Flutter Desktop apps, so this library comes under very permissive software, MIT license. 96 | 97 | Since, other libraries like [libvlcpp](https://github.com/videolan/libvlcpp) or [libvlc](https://www.videolan.org/vlc/libvlc.html) come under GPL & LGPL licenses respectively, so there will be many restrictions if I plan to use them. 98 | 99 | Thus, this project uses [miniaudio](https://github.com/mackron/miniaudio) and [miniaudio_engine](https://github.com/mackron/miniaudio) from [David Reid](https://github.com/mackron) under MIT license. 100 | 101 | 102 | ## Acknowledgments 103 | 104 | - [David Reid](https://github.com/mackron) for his amazing single header libraries [miniaudio](https://github.com/mackron/miniaudio) and [miniaudio_engine](https://github.com/mackron/miniaudio). 105 | - Thanks to [MichealReed](https://github.com/MichealReed) for his support to the project. 106 | -------------------------------------------------------------------------------- /audioplayer/audioplayer.hpp: -------------------------------------------------------------------------------- 1 | #define MINIAUDIO_IMPLEMENTATION 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "miniaudio/miniaudio.h" 8 | #include "miniaudio/miniaudio_engine.h" 9 | 10 | #include "internal/audiodevices.hpp" 11 | #include "internal/callbacks.hpp" 12 | 13 | 14 | const std::string __title__ = "flutter_audio_desktop"; 15 | const std::string __author__ = "alexmercerind"; 16 | const std::string __license__ = "MIT"; 17 | const std::string __version__ = "0.0.9"; 18 | 19 | 20 | class AudioPlayerInternal { 21 | protected: 22 | std::vector audioDevices; 23 | AudioDevice* preferredAudioDevice; 24 | AudioDevice* defaultAudioDevice; 25 | ma_device device; 26 | ma_resource_manager resourceManager; 27 | ma_resource_manager_data_source dataSource; 28 | bool isLoaded = false; 29 | 30 | void initialize() { 31 | this->audioDevices = AudioDevices::getAll(); 32 | this->defaultAudioDevice = AudioDevices::getDefault(); 33 | this->initDevice( 34 | this->preferredAudioDevice == nullptr ? this->defaultAudioDevice: this->preferredAudioDevice 35 | ); 36 | this->initResourceManager(); 37 | } 38 | 39 | void uninitialize() { 40 | ma_device_uninit(&this->device); 41 | ma_resource_manager_data_source_uninit(&this->dataSource); 42 | ma_resource_manager_uninit(&this->resourceManager); 43 | ma_context_uninit(&deviceContext); 44 | } 45 | 46 | private: 47 | ma_device_config deviceConfig; 48 | ma_resource_manager_config resourceManagerConfig; 49 | 50 | void initDevice(AudioDevice* audioDevice) { 51 | this->deviceConfig = ma_device_config_init(ma_device_type_playback); 52 | this->deviceConfig.dataCallback = dataCallbackStream; 53 | this->deviceConfig.playback.pDeviceID = &audioDevice->info.id; 54 | this->deviceConfig.pUserData = &this->dataSource; 55 | ma_device_init(&deviceContext, &this->deviceConfig, &this->device); 56 | } 57 | 58 | void initResourceManager() { 59 | this->resourceManagerConfig = ma_resource_manager_config_init(); 60 | this->resourceManagerConfig.decodedFormat = this->device.playback.format; 61 | this->resourceManagerConfig.decodedChannels = this->device.playback.channels; 62 | this->resourceManagerConfig.decodedSampleRate = this->device.sampleRate; 63 | ma_resource_manager_init(&resourceManagerConfig, &resourceManager); 64 | } 65 | }; 66 | 67 | 68 | class AudioPlayer: protected AudioPlayerInternal { 69 | public: 70 | AudioPlayer(AudioDevice* audioDevice) { 71 | this->preferredAudioDevice = audioDevice; 72 | }; 73 | 74 | void play(bool isLooping = false) { 75 | if (this->isLoaded) { 76 | ma_resource_manager_data_source_set_looping(&this->dataSource, isLooping); 77 | ma_device_start(&this->device); 78 | } 79 | } 80 | 81 | void pause() { 82 | ma_device_stop(&this->device); 83 | } 84 | 85 | void load(std::string filePath) { 86 | this->isLoaded = true; 87 | this->initialize(); 88 | ma_resource_manager_data_source_init( 89 | &this->resourceManager, 90 | filePath.c_str(), 91 | MA_DATA_SOURCE_FLAG_STREAM, 92 | NULL, 93 | &this->dataSource 94 | ); 95 | } 96 | 97 | void stop() { 98 | if (this->isLoaded) { 99 | this->uninitialize(); 100 | } 101 | this->isLoaded = false; 102 | } 103 | 104 | void setVolume(float volume) { 105 | if (this->isLoaded) { 106 | ma_device_set_master_volume(&this->device, volume); 107 | } 108 | } 109 | 110 | void setPosition(int durationMilliseconds) { 111 | if (this->isLoaded) { 112 | uint32_t durationPCMFrame = ma_calculate_buffer_size_in_frames_from_milliseconds(durationMilliseconds, this->device.sampleRate); 113 | ma_resource_manager_data_source_seek_to_pcm_frame(&this->dataSource, durationPCMFrame); 114 | } 115 | } 116 | 117 | int getDuration() { 118 | if (this->isLoaded) { 119 | unsigned long long durationPCMFrame; 120 | ma_resource_manager_data_source_get_length_in_pcm_frames( 121 | &this->dataSource, 122 | &durationPCMFrame 123 | ); 124 | uint32_t duration = ma_calculate_buffer_size_in_milliseconds_from_frames( 125 | static_cast(durationPCMFrame), 126 | this->device.sampleRate 127 | ); 128 | return static_cast(duration); 129 | } 130 | else return 0; 131 | } 132 | 133 | int getPosition() { 134 | if (this->isLoaded) { 135 | unsigned long long positionPCMFrame; 136 | ma_resource_manager_data_source_get_cursor_in_pcm_frames( 137 | &this->dataSource, 138 | &positionPCMFrame 139 | ); 140 | uint32_t position = ma_calculate_buffer_size_in_milliseconds_from_frames( 141 | static_cast(positionPCMFrame), 142 | this->device.sampleRate 143 | ); 144 | return static_cast(position); 145 | } 146 | else return 0; 147 | } 148 | }; 149 | -------------------------------------------------------------------------------- /audioplayer/internal/audiodevices.hpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "../miniaudio/miniaudio.h" 6 | 7 | 8 | class AudioDevice { 9 | public: 10 | int id; 11 | ma_device_info info; 12 | 13 | AudioDevice(int index, ma_device_info info) { 14 | this->id = index; 15 | this->info = info; 16 | } 17 | }; 18 | 19 | ma_context deviceContext; 20 | 21 | class AudioDevices { 22 | public: 23 | static std::vector getAll() { 24 | std::vector audioDevices; 25 | bool success = true; 26 | if (ma_context_init(NULL, 0, NULL, &deviceContext) != 0) { 27 | success = false; 28 | } 29 | ma_device_info *playbackDeviceInfos; 30 | unsigned int playbackDeviceCount; 31 | if (ma_context_get_devices(&deviceContext, &playbackDeviceInfos, &playbackDeviceCount, NULL, NULL) != 0) { 32 | success = false; 33 | } 34 | for (uint32_t index = 0; index < playbackDeviceCount; index++) { 35 | AudioDevice* device = new AudioDevice( 36 | index, 37 | playbackDeviceInfos[index] 38 | ); 39 | audioDevices.push_back(device); 40 | } 41 | if (!success) { 42 | throw std::string("EXCEPTION: Audio devices could not be found."); 43 | } 44 | return audioDevices; 45 | } 46 | 47 | static AudioDevice* getDefault() { 48 | std::vector audioDevices = AudioDevices::getAll(); 49 | AudioDevice* defaultAudioDevice = audioDevices[0]; 50 | for (AudioDevice* audioDevice: audioDevices) { 51 | if (audioDevice->info.isDefault) { 52 | defaultAudioDevice = audioDevice; 53 | break; 54 | }; 55 | } 56 | return defaultAudioDevice; 57 | } 58 | 59 | static std::map getAllMap() { 60 | std::map devices; 61 | for (AudioDevice* device: AudioDevices::getAll()) { 62 | devices[std::to_string(device->id)] = device->info.name; 63 | } 64 | devices["default"] = AudioDevices::getDefault()->info.name; 65 | return devices; 66 | } 67 | }; 68 | 69 | 70 | enum PlaybackState { 71 | deviceFind, 72 | deviceInit, 73 | resourceManagerInit, 74 | dataSourceInit, 75 | deviceStart, 76 | }; 77 | -------------------------------------------------------------------------------- /audioplayer/internal/callbacks.hpp: -------------------------------------------------------------------------------- 1 | #include "../miniaudio/miniaudio.h" 2 | 3 | 4 | void dataCallbackStream(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount) { 5 | ma_data_source_read_pcm_frames((ma_data_source*)pDevice->pUserData, pOutput, frameCount, NULL, MA_TRUE); 6 | (void)pInput; 7 | } 8 | 9 | void dataCallbackWave(ma_device *pDevice, void *pOutput, const void *pInput, ma_uint32 frameCount) { 10 | ma_waveform *pSineWave; 11 | pSineWave = (ma_waveform *)pDevice->pUserData; 12 | ma_waveform_read_pcm_frames(pSineWave, pOutput, frameCount); 13 | (void)pInput; 14 | } 15 | 16 | void dataCallbackNoise(ma_device *pDevice, void *pOutput, const void *pInput, ma_uint32 frameCount) { 17 | ma_noise *pNoise; 18 | pNoise = (ma_noise *)pDevice->pUserData; 19 | ma_noise_read_pcm_frames(pNoise, pOutput, frameCount); 20 | (void)pInput; 21 | } 22 | -------------------------------------------------------------------------------- /audioplayer/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "audioplayer.hpp" 5 | 6 | 7 | class AudioPlayers { 8 | public: 9 | AudioPlayers() { 10 | this->audioDevices = AudioDevices::getAll(); 11 | } 12 | 13 | AudioPlayer* get(int id, std::string deviceId = "default") { 14 | if (this->audioPlayers.find(id) == this->audioPlayers.end()) { 15 | AudioDevice* preferredAudioDevice = nullptr; 16 | if (deviceId != "default") { 17 | for (AudioDevice* audioDevice: this->audioDevices) { 18 | if (std::to_string(audioDevice->id) == deviceId) { 19 | preferredAudioDevice = audioDevice; 20 | break; 21 | }; 22 | } 23 | } 24 | this->audioPlayers[id] = new AudioPlayer(preferredAudioDevice); 25 | } 26 | return this->audioPlayers[id]; 27 | } 28 | 29 | private: 30 | std::map audioPlayers; 31 | std::vector audioDevices; 32 | }; 33 | 34 | 35 | AudioPlayers* audioPlayers = new AudioPlayers(); 36 | 37 | 38 | int main(int argc, const char **argv) { 39 | try { 40 | AudioDevices::getDefault(); 41 | AudioPlayer* audioPlayer = audioPlayers->get(0); 42 | audioPlayer->load(argv[1]); 43 | audioPlayer->play(); 44 | std::cin.get(); 45 | } 46 | catch (std::string exception) { 47 | std::cout << exception << std::endl; 48 | } 49 | return 0; 50 | } -------------------------------------------------------------------------------- /example/.gitignore: -------------------------------------------------------------------------------- 1 | # Miscellaneous 2 | *.class 3 | *.log 4 | *.pyc 5 | *.swp 6 | .DS_Store 7 | .atom/ 8 | .buildlog/ 9 | .history 10 | .svn/ 11 | 12 | # IntelliJ related 13 | *.iml 14 | *.ipr 15 | *.iws 16 | .idea/ 17 | 18 | # The .vscode folder contains launch configuration and tasks you configure in 19 | # VS Code which you may wish to be included in version control, so this line 20 | # is commented out by default. 21 | #.vscode/ 22 | 23 | # Flutter/Dart/Pub related 24 | **/doc/api/ 25 | **/ios/Flutter/.last_build_id 26 | .dart_tool/ 27 | .flutter-plugins 28 | .flutter-plugins-dependencies 29 | .packages 30 | .pub-cache/ 31 | .pub/ 32 | /build/ 33 | 34 | # Web related 35 | lib/generated_plugin_registrant.dart 36 | 37 | # Symbolication related 38 | app.*.symbols 39 | 40 | # Obfuscation related 41 | app.*.map.json 42 | -------------------------------------------------------------------------------- /example/.metadata: -------------------------------------------------------------------------------- 1 | # This file tracks properties of this Flutter project. 2 | # Used by Flutter tool to assess capabilities and perform upgrades etc. 3 | # 4 | # This file should be version controlled and should not be manually edited. 5 | 6 | version: 7 | revision: ce40de69b7b4f89c66d19c8dbd3bd86ae30f1bc6 8 | channel: dev 9 | 10 | project_type: app 11 | -------------------------------------------------------------------------------- /example/README.md: -------------------------------------------------------------------------------- 1 | # [flutter_audio_desktop_example](https://github.com/alexmercerind/flutter_audio_desktop) 2 | 3 | Demonstrates how to use the flutter_audio_desktop plugin. -------------------------------------------------------------------------------- /example/lib/main.dart: -------------------------------------------------------------------------------- 1 | import 'dart:io'; 2 | import 'package:flutter/material.dart'; 3 | import 'package:flutter_audio_desktop/flutter_audio_desktop.dart'; 4 | 5 | class Player extends StatefulWidget { 6 | Player({Key key}) : super(key: key); 7 | PlayerState createState() => PlayerState(); 8 | } 9 | 10 | class PlayerState extends State { 11 | AudioDevice defaultDevice; 12 | List allDevices; 13 | AudioPlayer audioPlayer; 14 | File file; 15 | bool isPlaying = false; 16 | bool isStopped = true; 17 | bool isCompleted = false; 18 | Duration position = Duration.zero; 19 | Duration duration = Duration.zero; 20 | double volume = 1.0; 21 | TextEditingController textController = new TextEditingController(); 22 | 23 | @override 24 | void didChangeDependencies() async { 25 | super.didChangeDependencies(); 26 | // Create AudioPlayer object by providing any id. 27 | // You can change playback device by providing device. 28 | this.audioPlayer = new AudioPlayer(id: 0) 29 | // Listen to AudioPlayer events. 30 | ..stream.listen( 31 | (Audio audio) { 32 | this.setState(() { 33 | this.file = audio.file; 34 | this.isPlaying = audio.isPlaying; 35 | this.isStopped = audio.isStopped; 36 | this.isCompleted = audio.isCompleted; 37 | this.position = audio.position; 38 | this.duration = audio.duration; 39 | }); 40 | }, 41 | ); 42 | // Get default & all devices to initialize in AudioPlayer. 43 | // Here we are just showing it to the user. 44 | this.defaultDevice = await AudioDevices.defaultDevice; 45 | this.allDevices = await AudioDevices.allDevices; 46 | } 47 | 48 | // Get AudioPlayer events without stream. 49 | void updatePlaybackState() { 50 | this.setState(() { 51 | this.file = this.audioPlayer.audio.file; 52 | this.isPlaying = this.audioPlayer.audio.isPlaying; 53 | this.isStopped = this.audioPlayer.audio.isStopped; 54 | this.isCompleted = this.audioPlayer.audio.isCompleted; 55 | this.position = this.audioPlayer.audio.position; 56 | this.duration = this.audioPlayer.audio.duration; 57 | }); 58 | } 59 | 60 | String getDurationString(Duration duration) { 61 | int minutes = duration.inSeconds ~/ 60; 62 | String seconds = duration.inSeconds - (minutes * 60) > 9 63 | ? '${duration.inSeconds - (minutes * 60)}' 64 | : '0${duration.inSeconds - (minutes * 60)}'; 65 | return '$minutes:$seconds'; 66 | } 67 | 68 | Widget build(BuildContext context) { 69 | return Scaffold( 70 | appBar: AppBar( 71 | centerTitle: true, 72 | title: Text('flutter_audio_desktop'), 73 | ), 74 | body: ListView( 75 | padding: EdgeInsets.only( 76 | top: 8.0, 77 | bottom: 8.0, 78 | left: 8.0, 79 | right: 8.0, 80 | ), 81 | children: [ 82 | Card( 83 | elevation: 2.0, 84 | color: Colors.white, 85 | margin: EdgeInsets.all(8.0), 86 | child: Padding( 87 | padding: EdgeInsets.only(left: 16.0, right: 16.0, bottom: 12.0), 88 | child: Column( 89 | children: [ 90 | SubHeader('File Loading'), 91 | Row( 92 | children: [ 93 | Expanded( 94 | child: TextField( 95 | controller: this.textController, 96 | autofocus: true, 97 | decoration: InputDecoration( 98 | labelText: 'File Location', 99 | ), 100 | ), 101 | ), 102 | Padding( 103 | padding: EdgeInsets.symmetric(horizontal: 16.0), 104 | child: IconButton( 105 | icon: Icon(Icons.check), 106 | iconSize: 32.0, 107 | color: Colors.blue, 108 | onPressed: () async { 109 | // Load AudioSource. 110 | await this.audioPlayer.load( 111 | AudioSource.fromFile( 112 | new File(this.textController.text), 113 | ), 114 | ); 115 | this.updatePlaybackState(); 116 | }, 117 | ), 118 | ), 119 | ], 120 | ), 121 | SubHeader('Playback Setters/Getters'), 122 | Row( 123 | mainAxisAlignment: MainAxisAlignment.center, 124 | children: [ 125 | Padding( 126 | padding: EdgeInsets.all(18.0), 127 | child: IconButton( 128 | icon: Icon(Icons.play_arrow), 129 | iconSize: 32.0, 130 | color: Colors.blue, 131 | onPressed: this.isStopped 132 | ? null 133 | : () async { 134 | await this.audioPlayer.play(); 135 | this.updatePlaybackState(); 136 | }, 137 | ), 138 | ), 139 | Padding( 140 | padding: EdgeInsets.all(18.0), 141 | child: IconButton( 142 | icon: Icon(Icons.pause), 143 | iconSize: 32.0, 144 | color: Colors.blue, 145 | onPressed: this.isStopped 146 | ? null 147 | : () async { 148 | await this.audioPlayer.pause(); 149 | this.updatePlaybackState(); 150 | }, 151 | ), 152 | ), 153 | Padding( 154 | padding: EdgeInsets.all(18.0), 155 | child: IconButton( 156 | icon: Icon(Icons.stop), 157 | iconSize: 32.0, 158 | color: Colors.blue, 159 | onPressed: this.isStopped 160 | ? null 161 | : () async { 162 | await this.audioPlayer.stop(); 163 | this.updatePlaybackState(); 164 | }, 165 | ), 166 | ), 167 | Slider( 168 | value: this.volume, 169 | min: 0.0, 170 | max: 1.0, 171 | onChanged: this.isStopped 172 | ? null 173 | : (double volume) async { 174 | this.volume = volume; 175 | // Change Volume. 176 | await this.audioPlayer.setVolume(this.volume); 177 | this.updatePlaybackState(); 178 | }, 179 | ), 180 | ], 181 | ), 182 | SubHeader('Position & Duration Setters/Getters'), 183 | Row( 184 | children: [ 185 | Text(this.getDurationString(this.position)), 186 | Expanded( 187 | child: Slider( 188 | value: this.position.inMilliseconds.toDouble(), 189 | min: 0.0, 190 | max: this.duration.inMilliseconds.toDouble(), 191 | onChanged: this.isStopped 192 | ? null 193 | : (double position) async { 194 | // Get or set playback position. 195 | await this.audioPlayer.setPosition( 196 | Duration( 197 | milliseconds: position.toInt()), 198 | ); 199 | }, 200 | ), 201 | ), 202 | Text(this.getDurationString(this.duration)), 203 | ], 204 | ), 205 | ], 206 | ), 207 | ), 208 | ), 209 | Card( 210 | elevation: 2.0, 211 | color: Colors.white, 212 | margin: EdgeInsets.all(8.0), 213 | child: Padding( 214 | padding: EdgeInsets.only(left: 16.0, right: 16.0, bottom: 12.0), 215 | child: Column( 216 | children: [ 217 | SubHeader('Playback State'), 218 | Table( 219 | children: [ 220 | TableRow(children: [ 221 | Text( 222 | 'audio.file', 223 | style: TextStyle( 224 | fontSize: 16, 225 | ), 226 | ), 227 | Text( 228 | '${this.file}', 229 | style: TextStyle( 230 | fontSize: 16, 231 | ), 232 | ), 233 | ]), 234 | TableRow(children: [ 235 | Text( 236 | 'audio.isPlaying', 237 | style: TextStyle( 238 | fontSize: 16, 239 | ), 240 | ), 241 | Text( 242 | '${this.isPlaying}', 243 | style: TextStyle( 244 | fontSize: 16, 245 | ), 246 | ), 247 | ]), 248 | TableRow(children: [ 249 | Text( 250 | 'audio.isStopped', 251 | style: TextStyle( 252 | fontSize: 16, 253 | ), 254 | ), 255 | Text( 256 | '${this.isStopped}', 257 | style: TextStyle( 258 | fontSize: 16, 259 | ), 260 | ), 261 | ]), 262 | TableRow(children: [ 263 | Text( 264 | 'audio.isCompleted', 265 | style: TextStyle( 266 | fontSize: 16, 267 | ), 268 | ), 269 | Text( 270 | '${this.isCompleted}', 271 | style: TextStyle( 272 | fontSize: 16, 273 | ), 274 | ), 275 | ]), 276 | TableRow(children: [ 277 | Text( 278 | 'audio.position', 279 | style: TextStyle( 280 | fontSize: 16, 281 | ), 282 | ), 283 | Text( 284 | '${this.position}', 285 | style: TextStyle( 286 | fontSize: 16, 287 | ), 288 | ), 289 | ]), 290 | TableRow(children: [ 291 | Text( 292 | 'audio.position', 293 | style: TextStyle( 294 | fontSize: 16, 295 | ), 296 | ), 297 | Text( 298 | '${this.duration}', 299 | style: TextStyle( 300 | fontSize: 16, 301 | ), 302 | ), 303 | ]), 304 | ], 305 | ), 306 | ], 307 | ), 308 | ), 309 | ), 310 | Card( 311 | elevation: 2.0, 312 | color: Colors.white, 313 | margin: EdgeInsets.all(8.0), 314 | child: Padding( 315 | padding: EdgeInsets.only(left: 16.0, right: 16.0, bottom: 12.0), 316 | child: Column( 317 | children: [ 318 | SubHeader('Default Device'), 319 | ListTile( 320 | title: Text('${this.defaultDevice?.name}'), 321 | subtitle: Text('${this.defaultDevice?.id}'), 322 | ), 323 | SubHeader('All Devices'), 324 | ] + 325 | ((this.allDevices != null) 326 | ? this.allDevices.map((AudioDevice device) { 327 | return ListTile( 328 | title: Text('${device?.name}'), 329 | subtitle: Text('${device?.id}'), 330 | ); 331 | }).toList() 332 | : []), 333 | ), 334 | ), 335 | ), 336 | ], 337 | ), 338 | ); 339 | } 340 | } 341 | 342 | class MyApp extends StatelessWidget { 343 | @override 344 | Widget build(BuildContext context) { 345 | return MaterialApp( 346 | home: Player(), 347 | ); 348 | } 349 | } 350 | 351 | void main() => runApp(MyApp()); 352 | 353 | class SubHeader extends StatelessWidget { 354 | final String text; 355 | 356 | const SubHeader(this.text, {Key key}) : super(key: key); 357 | 358 | @override 359 | Widget build(BuildContext context) { 360 | return Container( 361 | alignment: Alignment.centerLeft, 362 | height: 56.0, 363 | child: Text( 364 | text, 365 | style: TextStyle( 366 | fontSize: 14, 367 | color: Colors.black.withOpacity(0.67), 368 | ), 369 | ), 370 | ); 371 | } 372 | } 373 | -------------------------------------------------------------------------------- /example/linux/.gitignore: -------------------------------------------------------------------------------- 1 | flutter/ephemeral 2 | -------------------------------------------------------------------------------- /example/linux/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | project(runner LANGUAGES CXX) 3 | 4 | set(BINARY_NAME "flutter_audio_desktop_example") 5 | set(APPLICATION_ID "com.alexmercerind.flutter_audio_desktop") 6 | 7 | cmake_policy(SET CMP0063 NEW) 8 | 9 | set(CMAKE_INSTALL_RPATH "$ORIGIN/lib") 10 | 11 | # Configure build options. 12 | if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) 13 | set(CMAKE_BUILD_TYPE "Debug" CACHE 14 | STRING "Flutter build mode" FORCE) 15 | set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS 16 | "Debug" "Profile" "Release") 17 | endif() 18 | 19 | # Compilation settings that should be applied to most targets. 20 | function(APPLY_STANDARD_SETTINGS TARGET) 21 | target_compile_features(${TARGET} PUBLIC cxx_std_14) 22 | target_compile_options(${TARGET} PRIVATE -Wall -Werror) 23 | target_compile_options(${TARGET} PRIVATE "$<$>:-O3>") 24 | target_compile_definitions(${TARGET} PRIVATE "$<$>:NDEBUG>") 25 | endfunction() 26 | 27 | set(FLUTTER_MANAGED_DIR "${CMAKE_CURRENT_SOURCE_DIR}/flutter") 28 | 29 | # Flutter library and tool build rules. 30 | add_subdirectory(${FLUTTER_MANAGED_DIR}) 31 | 32 | # System-level dependencies. 33 | find_package(PkgConfig REQUIRED) 34 | pkg_check_modules(GTK REQUIRED IMPORTED_TARGET gtk+-3.0) 35 | 36 | add_definitions(-DAPPLICATION_ID="${APPLICATION_ID}") 37 | 38 | # Application build 39 | add_executable(${BINARY_NAME} 40 | "main.cc" 41 | "my_application.cc" 42 | "${FLUTTER_MANAGED_DIR}/generated_plugin_registrant.cc" 43 | ) 44 | apply_standard_settings(${BINARY_NAME}) 45 | target_link_libraries(${BINARY_NAME} PRIVATE flutter) 46 | target_link_libraries(${BINARY_NAME} PRIVATE PkgConfig::GTK) 47 | add_dependencies(${BINARY_NAME} flutter_assemble) 48 | 49 | # Generated plugin build rules, which manage building the plugins and adding 50 | # them to the application. 51 | include(flutter/generated_plugins.cmake) 52 | 53 | 54 | # === Installation === 55 | # By default, "installing" just makes a relocatable bundle in the build 56 | # directory. 57 | set(BUILD_BUNDLE_DIR "${PROJECT_BINARY_DIR}/bundle") 58 | if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) 59 | set(CMAKE_INSTALL_PREFIX "${BUILD_BUNDLE_DIR}" CACHE PATH "..." FORCE) 60 | endif() 61 | 62 | # Start with a clean build bundle directory every time. 63 | install(CODE " 64 | file(REMOVE_RECURSE \"${BUILD_BUNDLE_DIR}/\") 65 | " COMPONENT Runtime) 66 | 67 | set(INSTALL_BUNDLE_DATA_DIR "${CMAKE_INSTALL_PREFIX}/data") 68 | set(INSTALL_BUNDLE_LIB_DIR "${CMAKE_INSTALL_PREFIX}/lib") 69 | 70 | install(TARGETS ${BINARY_NAME} RUNTIME DESTINATION "${CMAKE_INSTALL_PREFIX}" 71 | COMPONENT Runtime) 72 | 73 | install(FILES "${FLUTTER_ICU_DATA_FILE}" DESTINATION "${INSTALL_BUNDLE_DATA_DIR}" 74 | COMPONENT Runtime) 75 | 76 | install(FILES "${FLUTTER_LIBRARY}" DESTINATION "${INSTALL_BUNDLE_LIB_DIR}" 77 | COMPONENT Runtime) 78 | 79 | if(PLUGIN_BUNDLED_LIBRARIES) 80 | install(FILES "${PLUGIN_BUNDLED_LIBRARIES}" 81 | DESTINATION "${INSTALL_BUNDLE_LIB_DIR}" 82 | COMPONENT Runtime) 83 | endif() 84 | 85 | # Fully re-copy the assets directory on each build to avoid having stale files 86 | # from a previous install. 87 | set(FLUTTER_ASSET_DIR_NAME "flutter_assets") 88 | install(CODE " 89 | file(REMOVE_RECURSE \"${INSTALL_BUNDLE_DATA_DIR}/${FLUTTER_ASSET_DIR_NAME}\") 90 | " COMPONENT Runtime) 91 | install(DIRECTORY "${PROJECT_BUILD_DIR}/${FLUTTER_ASSET_DIR_NAME}" 92 | DESTINATION "${INSTALL_BUNDLE_DATA_DIR}" COMPONENT Runtime) 93 | 94 | # Install the AOT library on non-Debug builds only. 95 | if(NOT CMAKE_BUILD_TYPE MATCHES "Debug") 96 | install(FILES "${AOT_LIBRARY}" DESTINATION "${INSTALL_BUNDLE_LIB_DIR}" 97 | COMPONENT Runtime) 98 | endif() 99 | -------------------------------------------------------------------------------- /example/linux/flutter/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | 3 | set(EPHEMERAL_DIR "${CMAKE_CURRENT_SOURCE_DIR}/ephemeral") 4 | 5 | # Configuration provided via flutter tool. 6 | include(${EPHEMERAL_DIR}/generated_config.cmake) 7 | 8 | # TODO: Move the rest of this into files in ephemeral. See 9 | # https://github.com/flutter/flutter/issues/57146. 10 | 11 | # Serves the same purpose as list(TRANSFORM ... PREPEND ...), 12 | # which isn't available in 3.10. 13 | function(list_prepend LIST_NAME PREFIX) 14 | set(NEW_LIST "") 15 | foreach(element ${${LIST_NAME}}) 16 | list(APPEND NEW_LIST "${PREFIX}${element}") 17 | endforeach(element) 18 | set(${LIST_NAME} "${NEW_LIST}" PARENT_SCOPE) 19 | endfunction() 20 | 21 | # === Flutter Library === 22 | # System-level dependencies. 23 | find_package(PkgConfig REQUIRED) 24 | pkg_check_modules(GTK REQUIRED IMPORTED_TARGET gtk+-3.0) 25 | pkg_check_modules(GLIB REQUIRED IMPORTED_TARGET glib-2.0) 26 | pkg_check_modules(GIO REQUIRED IMPORTED_TARGET gio-2.0) 27 | pkg_check_modules(BLKID REQUIRED IMPORTED_TARGET blkid) 28 | 29 | set(FLUTTER_LIBRARY "${EPHEMERAL_DIR}/libflutter_linux_gtk.so") 30 | 31 | # Published to parent scope for install step. 32 | set(FLUTTER_LIBRARY ${FLUTTER_LIBRARY} PARENT_SCOPE) 33 | set(FLUTTER_ICU_DATA_FILE "${EPHEMERAL_DIR}/icudtl.dat" PARENT_SCOPE) 34 | set(PROJECT_BUILD_DIR "${PROJECT_DIR}/build/" PARENT_SCOPE) 35 | set(AOT_LIBRARY "${PROJECT_DIR}/build/lib/libapp.so" PARENT_SCOPE) 36 | 37 | list(APPEND FLUTTER_LIBRARY_HEADERS 38 | "fl_basic_message_channel.h" 39 | "fl_binary_codec.h" 40 | "fl_binary_messenger.h" 41 | "fl_dart_project.h" 42 | "fl_engine.h" 43 | "fl_json_message_codec.h" 44 | "fl_json_method_codec.h" 45 | "fl_message_codec.h" 46 | "fl_method_call.h" 47 | "fl_method_channel.h" 48 | "fl_method_codec.h" 49 | "fl_method_response.h" 50 | "fl_plugin_registrar.h" 51 | "fl_plugin_registry.h" 52 | "fl_standard_message_codec.h" 53 | "fl_standard_method_codec.h" 54 | "fl_string_codec.h" 55 | "fl_value.h" 56 | "fl_view.h" 57 | "flutter_linux.h" 58 | ) 59 | list_prepend(FLUTTER_LIBRARY_HEADERS "${EPHEMERAL_DIR}/flutter_linux/") 60 | add_library(flutter INTERFACE) 61 | target_include_directories(flutter INTERFACE 62 | "${EPHEMERAL_DIR}" 63 | ) 64 | target_link_libraries(flutter INTERFACE "${FLUTTER_LIBRARY}") 65 | target_link_libraries(flutter INTERFACE 66 | PkgConfig::GTK 67 | PkgConfig::GLIB 68 | PkgConfig::GIO 69 | PkgConfig::BLKID 70 | ) 71 | add_dependencies(flutter flutter_assemble) 72 | 73 | # === Flutter tool backend === 74 | # _phony_ is a non-existent file to force this command to run every time, 75 | # since currently there's no way to get a full input/output list from the 76 | # flutter tool. 77 | add_custom_command( 78 | OUTPUT ${FLUTTER_LIBRARY} ${FLUTTER_LIBRARY_HEADERS} 79 | ${CMAKE_CURRENT_BINARY_DIR}/_phony_ 80 | COMMAND ${CMAKE_COMMAND} -E env 81 | ${FLUTTER_TOOL_ENVIRONMENT} 82 | "${FLUTTER_ROOT}/packages/flutter_tools/bin/tool_backend.sh" 83 | linux-x64 ${CMAKE_BUILD_TYPE} 84 | ) 85 | add_custom_target(flutter_assemble DEPENDS 86 | "${FLUTTER_LIBRARY}" 87 | ${FLUTTER_LIBRARY_HEADERS} 88 | ) 89 | -------------------------------------------------------------------------------- /example/linux/flutter/generated_plugin_registrant.cc: -------------------------------------------------------------------------------- 1 | // 2 | // Generated file. Do not edit. 3 | // 4 | 5 | #include "generated_plugin_registrant.h" 6 | 7 | #include 8 | 9 | void fl_register_plugins(FlPluginRegistry* registry) { 10 | g_autoptr(FlPluginRegistrar) flutter_audio_desktop_registrar = 11 | fl_plugin_registry_get_registrar_for_plugin(registry, "FlutterAudioDesktopPlugin"); 12 | flutter_audio_desktop_plugin_register_with_registrar(flutter_audio_desktop_registrar); 13 | } 14 | -------------------------------------------------------------------------------- /example/linux/flutter/generated_plugin_registrant.h: -------------------------------------------------------------------------------- 1 | // 2 | // Generated file. Do not edit. 3 | // 4 | 5 | #ifndef GENERATED_PLUGIN_REGISTRANT_ 6 | #define GENERATED_PLUGIN_REGISTRANT_ 7 | 8 | #include 9 | 10 | // Registers Flutter plugins. 11 | void fl_register_plugins(FlPluginRegistry* registry); 12 | 13 | #endif // GENERATED_PLUGIN_REGISTRANT_ 14 | -------------------------------------------------------------------------------- /example/linux/flutter/generated_plugins.cmake: -------------------------------------------------------------------------------- 1 | # 2 | # Generated file, do not edit. 3 | # 4 | 5 | list(APPEND FLUTTER_PLUGIN_LIST 6 | flutter_audio_desktop 7 | ) 8 | 9 | set(PLUGIN_BUNDLED_LIBRARIES) 10 | 11 | foreach(plugin ${FLUTTER_PLUGIN_LIST}) 12 | add_subdirectory(flutter/ephemeral/.plugin_symlinks/${plugin}/linux plugins/${plugin}) 13 | target_link_libraries(${BINARY_NAME} PRIVATE ${plugin}_plugin) 14 | list(APPEND PLUGIN_BUNDLED_LIBRARIES $) 15 | list(APPEND PLUGIN_BUNDLED_LIBRARIES ${${plugin}_bundled_libraries}) 16 | endforeach(plugin) 17 | -------------------------------------------------------------------------------- /example/linux/main.cc: -------------------------------------------------------------------------------- 1 | #include "my_application.h" 2 | 3 | int main(int argc, char** argv) { 4 | // Only X11 is currently supported. 5 | // Wayland support is being developed: https://github.com/flutter/flutter/issues/57932. 6 | gdk_set_allowed_backends("x11"); 7 | 8 | g_autoptr(MyApplication) app = my_application_new(); 9 | return g_application_run(G_APPLICATION(app), argc, argv); 10 | } 11 | -------------------------------------------------------------------------------- /example/linux/my_application.cc: -------------------------------------------------------------------------------- 1 | #include "my_application.h" 2 | 3 | #include 4 | 5 | #include "flutter/generated_plugin_registrant.h" 6 | 7 | struct _MyApplication { 8 | GtkApplication parent_instance; 9 | }; 10 | 11 | G_DEFINE_TYPE(MyApplication, my_application, GTK_TYPE_APPLICATION) 12 | 13 | // Implements GApplication::activate. 14 | static void my_application_activate(GApplication* application) { 15 | GtkWindow* window = 16 | GTK_WINDOW(gtk_application_window_new(GTK_APPLICATION(application))); 17 | GtkHeaderBar *header_bar = GTK_HEADER_BAR(gtk_header_bar_new()); 18 | gtk_widget_show(GTK_WIDGET(header_bar)); 19 | gtk_header_bar_set_title(header_bar, "flutter_audio_desktop"); 20 | gtk_header_bar_set_show_close_button(header_bar, TRUE); 21 | gtk_window_set_titlebar(window, GTK_WIDGET(header_bar)); 22 | gtk_window_set_default_size(window, 600, 720); 23 | gtk_widget_show(GTK_WIDGET(window)); 24 | 25 | g_autoptr(FlDartProject) project = fl_dart_project_new(); 26 | 27 | FlView* view = fl_view_new(project); 28 | gtk_widget_show(GTK_WIDGET(view)); 29 | gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(view)); 30 | 31 | fl_register_plugins(FL_PLUGIN_REGISTRY(view)); 32 | 33 | gtk_widget_grab_focus(GTK_WIDGET(view)); 34 | } 35 | 36 | static void my_application_class_init(MyApplicationClass* klass) { 37 | G_APPLICATION_CLASS(klass)->activate = my_application_activate; 38 | } 39 | 40 | static void my_application_init(MyApplication* self) {} 41 | 42 | MyApplication* my_application_new() { 43 | return MY_APPLICATION(g_object_new(my_application_get_type(), 44 | "application-id", APPLICATION_ID, 45 | nullptr)); 46 | } 47 | -------------------------------------------------------------------------------- /example/linux/my_application.h: -------------------------------------------------------------------------------- 1 | #ifndef FLUTTER_MY_APPLICATION_H_ 2 | #define FLUTTER_MY_APPLICATION_H_ 3 | 4 | #include 5 | 6 | G_DECLARE_FINAL_TYPE(MyApplication, my_application, MY, APPLICATION, 7 | GtkApplication) 8 | 9 | /** 10 | * my_application_new: 11 | * 12 | * Creates a new Flutter-based application. 13 | * 14 | * Returns: a new #MyApplication. 15 | */ 16 | MyApplication* my_application_new(); 17 | 18 | #endif // FLUTTER_MY_APPLICATION_H_ 19 | -------------------------------------------------------------------------------- /example/pubspec.lock: -------------------------------------------------------------------------------- 1 | # Generated by pub 2 | # See https://dart.dev/tools/pub/glossary#lockfile 3 | packages: 4 | async: 5 | dependency: transitive 6 | description: 7 | name: async 8 | url: "https://pub.dartlang.org" 9 | source: hosted 10 | version: "2.5.0" 11 | boolean_selector: 12 | dependency: transitive 13 | description: 14 | name: boolean_selector 15 | url: "https://pub.dartlang.org" 16 | source: hosted 17 | version: "2.1.0" 18 | characters: 19 | dependency: transitive 20 | description: 21 | name: characters 22 | url: "https://pub.dartlang.org" 23 | source: hosted 24 | version: "1.1.0" 25 | charcode: 26 | dependency: transitive 27 | description: 28 | name: charcode 29 | url: "https://pub.dartlang.org" 30 | source: hosted 31 | version: "1.2.0" 32 | clock: 33 | dependency: transitive 34 | description: 35 | name: clock 36 | url: "https://pub.dartlang.org" 37 | source: hosted 38 | version: "1.1.0" 39 | collection: 40 | dependency: transitive 41 | description: 42 | name: collection 43 | url: "https://pub.dartlang.org" 44 | source: hosted 45 | version: "1.15.0" 46 | fake_async: 47 | dependency: transitive 48 | description: 49 | name: fake_async 50 | url: "https://pub.dartlang.org" 51 | source: hosted 52 | version: "1.2.0" 53 | ffi: 54 | dependency: transitive 55 | description: 56 | name: ffi 57 | url: "https://pub.dartlang.org" 58 | source: hosted 59 | version: "1.0.0" 60 | file: 61 | dependency: transitive 62 | description: 63 | name: file 64 | url: "https://pub.dartlang.org" 65 | source: hosted 66 | version: "6.1.0" 67 | flutter: 68 | dependency: "direct main" 69 | description: flutter 70 | source: sdk 71 | version: "0.0.0" 72 | flutter_audio_desktop: 73 | dependency: "direct main" 74 | description: 75 | path: ".." 76 | relative: true 77 | source: path 78 | version: "0.1.0" 79 | flutter_test: 80 | dependency: "direct dev" 81 | description: flutter 82 | source: sdk 83 | version: "0.0.0" 84 | matcher: 85 | dependency: transitive 86 | description: 87 | name: matcher 88 | url: "https://pub.dartlang.org" 89 | source: hosted 90 | version: "0.12.10" 91 | meta: 92 | dependency: transitive 93 | description: 94 | name: meta 95 | url: "https://pub.dartlang.org" 96 | source: hosted 97 | version: "1.3.0" 98 | path: 99 | dependency: transitive 100 | description: 101 | name: path 102 | url: "https://pub.dartlang.org" 103 | source: hosted 104 | version: "1.8.0" 105 | path_provider: 106 | dependency: transitive 107 | description: 108 | name: path_provider 109 | url: "https://pub.dartlang.org" 110 | source: hosted 111 | version: "2.0.0" 112 | path_provider_linux: 113 | dependency: transitive 114 | description: 115 | name: path_provider_linux 116 | url: "https://pub.dartlang.org" 117 | source: hosted 118 | version: "2.0.0" 119 | path_provider_macos: 120 | dependency: transitive 121 | description: 122 | name: path_provider_macos 123 | url: "https://pub.dartlang.org" 124 | source: hosted 125 | version: "2.0.0" 126 | path_provider_platform_interface: 127 | dependency: transitive 128 | description: 129 | name: path_provider_platform_interface 130 | url: "https://pub.dartlang.org" 131 | source: hosted 132 | version: "2.0.0" 133 | path_provider_windows: 134 | dependency: transitive 135 | description: 136 | name: path_provider_windows 137 | url: "https://pub.dartlang.org" 138 | source: hosted 139 | version: "2.0.0" 140 | platform: 141 | dependency: transitive 142 | description: 143 | name: platform 144 | url: "https://pub.dartlang.org" 145 | source: hosted 146 | version: "3.0.0" 147 | plugin_platform_interface: 148 | dependency: transitive 149 | description: 150 | name: plugin_platform_interface 151 | url: "https://pub.dartlang.org" 152 | source: hosted 153 | version: "1.0.3" 154 | process: 155 | dependency: transitive 156 | description: 157 | name: process 158 | url: "https://pub.dartlang.org" 159 | source: hosted 160 | version: "4.1.0" 161 | sky_engine: 162 | dependency: transitive 163 | description: flutter 164 | source: sdk 165 | version: "0.0.99" 166 | source_span: 167 | dependency: transitive 168 | description: 169 | name: source_span 170 | url: "https://pub.dartlang.org" 171 | source: hosted 172 | version: "1.8.1" 173 | stack_trace: 174 | dependency: transitive 175 | description: 176 | name: stack_trace 177 | url: "https://pub.dartlang.org" 178 | source: hosted 179 | version: "1.10.0" 180 | stream_channel: 181 | dependency: transitive 182 | description: 183 | name: stream_channel 184 | url: "https://pub.dartlang.org" 185 | source: hosted 186 | version: "2.1.0" 187 | string_scanner: 188 | dependency: transitive 189 | description: 190 | name: string_scanner 191 | url: "https://pub.dartlang.org" 192 | source: hosted 193 | version: "1.1.0" 194 | term_glyph: 195 | dependency: transitive 196 | description: 197 | name: term_glyph 198 | url: "https://pub.dartlang.org" 199 | source: hosted 200 | version: "1.2.0" 201 | test_api: 202 | dependency: transitive 203 | description: 204 | name: test_api 205 | url: "https://pub.dartlang.org" 206 | source: hosted 207 | version: "0.2.19" 208 | typed_data: 209 | dependency: transitive 210 | description: 211 | name: typed_data 212 | url: "https://pub.dartlang.org" 213 | source: hosted 214 | version: "1.3.0" 215 | vector_math: 216 | dependency: transitive 217 | description: 218 | name: vector_math 219 | url: "https://pub.dartlang.org" 220 | source: hosted 221 | version: "2.1.0" 222 | win32: 223 | dependency: transitive 224 | description: 225 | name: win32 226 | url: "https://pub.dartlang.org" 227 | source: hosted 228 | version: "2.0.0" 229 | xdg_directories: 230 | dependency: transitive 231 | description: 232 | name: xdg_directories 233 | url: "https://pub.dartlang.org" 234 | source: hosted 235 | version: "0.2.0" 236 | sdks: 237 | dart: ">=2.12.0-259.9.beta <3.0.0" 238 | flutter: ">=1.20.0" 239 | -------------------------------------------------------------------------------- /example/pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: flutter_audio_desktop_example 2 | description: Demonstrates how to use the flutter_audio_desktop plugin. 3 | 4 | publish_to: 'none' 5 | 6 | environment: 7 | sdk: ">=2.7.0 <3.0.0" 8 | 9 | dependencies: 10 | flutter: 11 | sdk: flutter 12 | 13 | flutter_audio_desktop: 14 | path: ../ 15 | 16 | dev_dependencies: 17 | flutter_test: 18 | sdk: flutter 19 | 20 | flutter: 21 | uses-material-design: true -------------------------------------------------------------------------------- /example/windows/.gitignore: -------------------------------------------------------------------------------- 1 | flutter/ephemeral/ 2 | 3 | # Visual Studio user-specific files. 4 | *.suo 5 | *.user 6 | *.userosscache 7 | *.sln.docstates 8 | 9 | # Visual Studio build-related files. 10 | x64/ 11 | x86/ 12 | 13 | # Visual Studio cache files 14 | # files ending in .cache can be ignored 15 | *.[Cc]ache 16 | # but keep track of directories ending in .cache 17 | !*.[Cc]ache/ 18 | -------------------------------------------------------------------------------- /example/windows/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.15) 2 | project(flutter_audio_desktop_example LANGUAGES CXX) 3 | 4 | set(BINARY_NAME "flutter_audio_desktop_example") 5 | 6 | cmake_policy(SET CMP0063 NEW) 7 | 8 | set(CMAKE_INSTALL_RPATH "$ORIGIN/lib") 9 | 10 | # Configure build options. 11 | get_property(IS_MULTICONFIG GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG) 12 | if(IS_MULTICONFIG) 13 | set(CMAKE_CONFIGURATION_TYPES "Debug;Profile;Release" 14 | CACHE STRING "" FORCE) 15 | else() 16 | if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) 17 | set(CMAKE_BUILD_TYPE "Debug" CACHE 18 | STRING "Flutter build mode" FORCE) 19 | set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS 20 | "Debug" "Profile" "Release") 21 | endif() 22 | endif() 23 | 24 | set(CMAKE_EXE_LINKER_FLAGS_PROFILE "${CMAKE_EXE_LINKER_FLAGS_RELEASE}") 25 | set(CMAKE_SHARED_LINKER_FLAGS_PROFILE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE}") 26 | set(CMAKE_C_FLAGS_PROFILE "${CMAKE_C_FLAGS_RELEASE}") 27 | set(CMAKE_CXX_FLAGS_PROFILE "${CMAKE_CXX_FLAGS_RELEASE}") 28 | 29 | # Use Unicode for all projects. 30 | add_definitions(-DUNICODE -D_UNICODE) 31 | 32 | # Compilation settings that should be applied to most targets. 33 | function(APPLY_STANDARD_SETTINGS TARGET) 34 | target_compile_features(${TARGET} PUBLIC cxx_std_17) 35 | target_compile_options(${TARGET} PRIVATE /W4 /WX /wd"4100") 36 | target_compile_options(${TARGET} PRIVATE /EHsc) 37 | target_compile_definitions(${TARGET} PRIVATE "_HAS_EXCEPTIONS=0") 38 | target_compile_definitions(${TARGET} PRIVATE "$<$:_DEBUG>") 39 | endfunction() 40 | 41 | set(FLUTTER_MANAGED_DIR "${CMAKE_CURRENT_SOURCE_DIR}/flutter") 42 | 43 | # Flutter library and tool build rules. 44 | add_subdirectory(${FLUTTER_MANAGED_DIR}) 45 | 46 | # Application build 47 | add_subdirectory("runner") 48 | 49 | # Generated plugin build rules, which manage building the plugins and adding 50 | # them to the application. 51 | include(flutter/generated_plugins.cmake) 52 | 53 | 54 | # === Installation === 55 | # Support files are copied into place next to the executable, so that it can 56 | # run in place. This is done instead of making a separate bundle (as on Linux) 57 | # so that building and running from within Visual Studio will work. 58 | set(BUILD_BUNDLE_DIR "$") 59 | # Make the "install" step default, as it's required to run. 60 | set(CMAKE_VS_INCLUDE_INSTALL_TO_DEFAULT_BUILD 1) 61 | if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) 62 | set(CMAKE_INSTALL_PREFIX "${BUILD_BUNDLE_DIR}" CACHE PATH "..." FORCE) 63 | endif() 64 | 65 | set(INSTALL_BUNDLE_DATA_DIR "${CMAKE_INSTALL_PREFIX}/data") 66 | set(INSTALL_BUNDLE_LIB_DIR "${CMAKE_INSTALL_PREFIX}") 67 | 68 | install(TARGETS ${BINARY_NAME} RUNTIME DESTINATION "${CMAKE_INSTALL_PREFIX}" 69 | COMPONENT Runtime) 70 | 71 | install(FILES "${FLUTTER_ICU_DATA_FILE}" DESTINATION "${INSTALL_BUNDLE_DATA_DIR}" 72 | COMPONENT Runtime) 73 | 74 | install(FILES "${FLUTTER_LIBRARY}" DESTINATION "${INSTALL_BUNDLE_LIB_DIR}" 75 | COMPONENT Runtime) 76 | 77 | if(PLUGIN_BUNDLED_LIBRARIES) 78 | install(FILES "${PLUGIN_BUNDLED_LIBRARIES}" 79 | DESTINATION "${INSTALL_BUNDLE_LIB_DIR}" 80 | COMPONENT Runtime) 81 | endif() 82 | 83 | # Fully re-copy the assets directory on each build to avoid having stale files 84 | # from a previous install. 85 | set(FLUTTER_ASSET_DIR_NAME "flutter_assets") 86 | install(CODE " 87 | file(REMOVE_RECURSE \"${INSTALL_BUNDLE_DATA_DIR}/${FLUTTER_ASSET_DIR_NAME}\") 88 | " COMPONENT Runtime) 89 | install(DIRECTORY "${PROJECT_BUILD_DIR}/${FLUTTER_ASSET_DIR_NAME}" 90 | DESTINATION "${INSTALL_BUNDLE_DATA_DIR}" COMPONENT Runtime) 91 | 92 | # Install the AOT library on non-Debug builds only. 93 | install(FILES "${AOT_LIBRARY}" DESTINATION "${INSTALL_BUNDLE_DATA_DIR}" 94 | CONFIGURATIONS Profile;Release 95 | COMPONENT Runtime) 96 | -------------------------------------------------------------------------------- /example/windows/flutter/.template_version: -------------------------------------------------------------------------------- 1 | 7 2 | -------------------------------------------------------------------------------- /example/windows/flutter/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.15) 2 | 3 | set(EPHEMERAL_DIR "${CMAKE_CURRENT_SOURCE_DIR}/ephemeral") 4 | 5 | # Configuration provided via flutter tool. 6 | include(${EPHEMERAL_DIR}/generated_config.cmake) 7 | 8 | # TODO: Move the rest of this into files in ephemeral. See 9 | # https://github.com/flutter/flutter/issues/57146. 10 | set(WRAPPER_ROOT "${EPHEMERAL_DIR}/cpp_client_wrapper") 11 | 12 | # === Flutter Library === 13 | set(FLUTTER_LIBRARY "${EPHEMERAL_DIR}/flutter_windows.dll") 14 | 15 | # Published to parent scope for install step. 16 | set(FLUTTER_LIBRARY ${FLUTTER_LIBRARY} PARENT_SCOPE) 17 | set(FLUTTER_ICU_DATA_FILE "${EPHEMERAL_DIR}/icudtl.dat" PARENT_SCOPE) 18 | set(PROJECT_BUILD_DIR "${PROJECT_DIR}/build/" PARENT_SCOPE) 19 | set(AOT_LIBRARY "${PROJECT_DIR}/build/windows/app.so" PARENT_SCOPE) 20 | 21 | list(APPEND FLUTTER_LIBRARY_HEADERS 22 | "flutter_export.h" 23 | "flutter_windows.h" 24 | "flutter_messenger.h" 25 | "flutter_plugin_registrar.h" 26 | ) 27 | list(TRANSFORM FLUTTER_LIBRARY_HEADERS PREPEND "${EPHEMERAL_DIR}/") 28 | add_library(flutter INTERFACE) 29 | target_include_directories(flutter INTERFACE 30 | "${EPHEMERAL_DIR}" 31 | ) 32 | target_link_libraries(flutter INTERFACE "${FLUTTER_LIBRARY}.lib") 33 | add_dependencies(flutter flutter_assemble) 34 | 35 | # === Wrapper === 36 | list(APPEND CPP_WRAPPER_SOURCES_CORE 37 | "core_implementations.cc" 38 | "standard_codec.cc" 39 | ) 40 | list(TRANSFORM CPP_WRAPPER_SOURCES_CORE PREPEND "${WRAPPER_ROOT}/") 41 | list(APPEND CPP_WRAPPER_SOURCES_PLUGIN 42 | "plugin_registrar.cc" 43 | ) 44 | list(TRANSFORM CPP_WRAPPER_SOURCES_PLUGIN PREPEND "${WRAPPER_ROOT}/") 45 | list(APPEND CPP_WRAPPER_SOURCES_APP 46 | "flutter_engine.cc" 47 | "flutter_view_controller.cc" 48 | ) 49 | list(TRANSFORM CPP_WRAPPER_SOURCES_APP PREPEND "${WRAPPER_ROOT}/") 50 | 51 | # Wrapper sources needed for a plugin. 52 | add_library(flutter_wrapper_plugin STATIC 53 | ${CPP_WRAPPER_SOURCES_CORE} 54 | ${CPP_WRAPPER_SOURCES_PLUGIN} 55 | ) 56 | apply_standard_settings(flutter_wrapper_plugin) 57 | set_target_properties(flutter_wrapper_plugin PROPERTIES 58 | POSITION_INDEPENDENT_CODE ON) 59 | set_target_properties(flutter_wrapper_plugin PROPERTIES 60 | CXX_VISIBILITY_PRESET hidden) 61 | target_link_libraries(flutter_wrapper_plugin PUBLIC flutter) 62 | target_include_directories(flutter_wrapper_plugin PUBLIC 63 | "${WRAPPER_ROOT}/include" 64 | ) 65 | add_dependencies(flutter_wrapper_plugin flutter_assemble) 66 | 67 | # Wrapper sources needed for the runner. 68 | add_library(flutter_wrapper_app STATIC 69 | ${CPP_WRAPPER_SOURCES_CORE} 70 | ${CPP_WRAPPER_SOURCES_APP} 71 | ) 72 | apply_standard_settings(flutter_wrapper_app) 73 | target_link_libraries(flutter_wrapper_app PUBLIC flutter) 74 | target_include_directories(flutter_wrapper_app PUBLIC 75 | "${WRAPPER_ROOT}/include" 76 | ) 77 | add_dependencies(flutter_wrapper_app flutter_assemble) 78 | 79 | # === Flutter tool backend === 80 | # _phony_ is a non-existent file to force this command to run every time, 81 | # since currently there's no way to get a full input/output list from the 82 | # flutter tool. 83 | set(PHONY_OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/_phony_") 84 | set_source_files_properties("${PHONY_OUTPUT}" PROPERTIES SYMBOLIC TRUE) 85 | add_custom_command( 86 | OUTPUT ${FLUTTER_LIBRARY} ${FLUTTER_LIBRARY_HEADERS} 87 | ${CPP_WRAPPER_SOURCES_CORE} ${CPP_WRAPPER_SOURCES_PLUGIN} 88 | ${CPP_WRAPPER_SOURCES_APP} 89 | ${PHONY_OUTPUT} 90 | COMMAND ${CMAKE_COMMAND} -E env 91 | ${FLUTTER_TOOL_ENVIRONMENT} 92 | "${FLUTTER_ROOT}/packages/flutter_tools/bin/tool_backend.bat" 93 | windows-x64 $ 94 | VERBATIM 95 | ) 96 | add_custom_target(flutter_assemble DEPENDS 97 | "${FLUTTER_LIBRARY}" 98 | ${FLUTTER_LIBRARY_HEADERS} 99 | ${CPP_WRAPPER_SOURCES_CORE} 100 | ${CPP_WRAPPER_SOURCES_PLUGIN} 101 | ${CPP_WRAPPER_SOURCES_APP} 102 | ) 103 | -------------------------------------------------------------------------------- /example/windows/flutter/generated_plugin_registrant.cc: -------------------------------------------------------------------------------- 1 | // 2 | // Generated file. Do not edit. 3 | // 4 | 5 | // clang-format off 6 | 7 | #include "generated_plugin_registrant.h" 8 | 9 | #include 10 | 11 | void RegisterPlugins(flutter::PluginRegistry* registry) { 12 | FlutterAudioDesktopPluginRegisterWithRegistrar( 13 | registry->GetRegistrarForPlugin("FlutterAudioDesktopPlugin")); 14 | } 15 | -------------------------------------------------------------------------------- /example/windows/flutter/generated_plugin_registrant.h: -------------------------------------------------------------------------------- 1 | // 2 | // Generated file. Do not edit. 3 | // 4 | 5 | // clang-format off 6 | 7 | #ifndef GENERATED_PLUGIN_REGISTRANT_ 8 | #define GENERATED_PLUGIN_REGISTRANT_ 9 | 10 | #include 11 | 12 | // Registers Flutter plugins. 13 | void RegisterPlugins(flutter::PluginRegistry* registry); 14 | 15 | #endif // GENERATED_PLUGIN_REGISTRANT_ 16 | -------------------------------------------------------------------------------- /example/windows/flutter/generated_plugins.cmake: -------------------------------------------------------------------------------- 1 | # 2 | # Generated file, do not edit. 3 | # 4 | 5 | list(APPEND FLUTTER_PLUGIN_LIST 6 | flutter_audio_desktop 7 | ) 8 | 9 | set(PLUGIN_BUNDLED_LIBRARIES) 10 | 11 | foreach(plugin ${FLUTTER_PLUGIN_LIST}) 12 | add_subdirectory(flutter/ephemeral/.plugin_symlinks/${plugin}/windows plugins/${plugin}) 13 | target_link_libraries(${BINARY_NAME} PRIVATE ${plugin}_plugin) 14 | list(APPEND PLUGIN_BUNDLED_LIBRARIES $) 15 | list(APPEND PLUGIN_BUNDLED_LIBRARIES ${${plugin}_bundled_libraries}) 16 | endforeach(plugin) 17 | -------------------------------------------------------------------------------- /example/windows/runner/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.15) 2 | project(runner LANGUAGES CXX) 3 | 4 | add_executable(${BINARY_NAME} WIN32 5 | "flutter_window.cpp" 6 | "main.cpp" 7 | "run_loop.cpp" 8 | "utils.cpp" 9 | "win32_window.cpp" 10 | "${FLUTTER_MANAGED_DIR}/generated_plugin_registrant.cc" 11 | "Runner.rc" 12 | "runner.exe.manifest" 13 | ) 14 | apply_standard_settings(${BINARY_NAME}) 15 | target_compile_definitions(${BINARY_NAME} PRIVATE "NOMINMAX") 16 | target_link_libraries(${BINARY_NAME} PRIVATE flutter flutter_wrapper_app) 17 | target_include_directories(${BINARY_NAME} PRIVATE "${CMAKE_SOURCE_DIR}") 18 | add_dependencies(${BINARY_NAME} flutter_assemble) 19 | -------------------------------------------------------------------------------- /example/windows/runner/Runner.rc: -------------------------------------------------------------------------------- 1 | // Microsoft Visual C++ generated resource script. 2 | // 3 | #pragma code_page(65001) 4 | #include "resource.h" 5 | 6 | #define APSTUDIO_READONLY_SYMBOLS 7 | ///////////////////////////////////////////////////////////////////////////// 8 | // 9 | // Generated from the TEXTINCLUDE 2 resource. 10 | // 11 | #include "winres.h" 12 | 13 | ///////////////////////////////////////////////////////////////////////////// 14 | #undef APSTUDIO_READONLY_SYMBOLS 15 | 16 | ///////////////////////////////////////////////////////////////////////////// 17 | // English (United States) resources 18 | 19 | #if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU) 20 | LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US 21 | 22 | #ifdef APSTUDIO_INVOKED 23 | ///////////////////////////////////////////////////////////////////////////// 24 | // 25 | // TEXTINCLUDE 26 | // 27 | 28 | 1 TEXTINCLUDE 29 | BEGIN 30 | "resource.h\0" 31 | END 32 | 33 | 2 TEXTINCLUDE 34 | BEGIN 35 | "#include ""winres.h""\r\n" 36 | "\0" 37 | END 38 | 39 | 3 TEXTINCLUDE 40 | BEGIN 41 | "\r\n" 42 | "\0" 43 | END 44 | 45 | #endif // APSTUDIO_INVOKED 46 | 47 | 48 | ///////////////////////////////////////////////////////////////////////////// 49 | // 50 | // Icon 51 | // 52 | 53 | // Icon with lowest ID value placed first to ensure application icon 54 | // remains consistent on all systems. 55 | IDI_APP_ICON ICON "resources\\app_icon.ico" 56 | 57 | #endif // English (United States) resources 58 | ///////////////////////////////////////////////////////////////////////////// 59 | 60 | 61 | 62 | #ifndef APSTUDIO_INVOKED 63 | ///////////////////////////////////////////////////////////////////////////// 64 | // 65 | // Generated from the TEXTINCLUDE 3 resource. 66 | // 67 | 68 | 69 | ///////////////////////////////////////////////////////////////////////////// 70 | #endif // not APSTUDIO_INVOKED 71 | -------------------------------------------------------------------------------- /example/windows/runner/flutter_window.cpp: -------------------------------------------------------------------------------- 1 | #include "flutter_window.h" 2 | 3 | #include 4 | 5 | #include "flutter/generated_plugin_registrant.h" 6 | 7 | FlutterWindow::FlutterWindow(RunLoop* run_loop, 8 | const flutter::DartProject& project) 9 | : run_loop_(run_loop), project_(project) {} 10 | 11 | FlutterWindow::~FlutterWindow() {} 12 | 13 | bool FlutterWindow::OnCreate() { 14 | if (!Win32Window::OnCreate()) { 15 | return false; 16 | } 17 | 18 | RECT frame = GetClientArea(); 19 | 20 | // The size here must match the window dimensions to avoid unnecessary surface 21 | // creation / destruction in the startup path. 22 | flutter_controller_ = std::make_unique( 23 | frame.right - frame.left, frame.bottom - frame.top, project_); 24 | // Ensure that basic setup of the controller was successful. 25 | if (!flutter_controller_->engine() || !flutter_controller_->view()) { 26 | return false; 27 | } 28 | RegisterPlugins(flutter_controller_->engine()); 29 | run_loop_->RegisterFlutterInstance(flutter_controller_->engine()); 30 | SetChildContent(flutter_controller_->view()->GetNativeWindow()); 31 | return true; 32 | } 33 | 34 | void FlutterWindow::OnDestroy() { 35 | if (flutter_controller_) { 36 | run_loop_->UnregisterFlutterInstance(flutter_controller_->engine()); 37 | flutter_controller_ = nullptr; 38 | } 39 | 40 | Win32Window::OnDestroy(); 41 | } 42 | 43 | LRESULT 44 | FlutterWindow::MessageHandler(HWND hwnd, UINT const message, 45 | WPARAM const wparam, 46 | LPARAM const lparam) noexcept { 47 | // Give Flutter, including plugins, an opporutunity to handle window messages. 48 | if (flutter_controller_) { 49 | std::optional result = 50 | flutter_controller_->HandleTopLevelWindowProc(hwnd, message, wparam, 51 | lparam); 52 | if (result) { 53 | return *result; 54 | } 55 | } 56 | return Win32Window::MessageHandler(hwnd, message, wparam, lparam); 57 | } 58 | -------------------------------------------------------------------------------- /example/windows/runner/flutter_window.h: -------------------------------------------------------------------------------- 1 | #ifndef RUNNER_FLUTTER_WINDOW_H_ 2 | #define RUNNER_FLUTTER_WINDOW_H_ 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | 9 | #include "run_loop.h" 10 | #include "win32_window.h" 11 | 12 | // A window that does nothing but host a Flutter view. 13 | class FlutterWindow : public Win32Window { 14 | public: 15 | // Creates a new FlutterWindow driven by the |run_loop|, hosting a 16 | // Flutter view running |project|. 17 | explicit FlutterWindow(RunLoop* run_loop, 18 | const flutter::DartProject& project); 19 | virtual ~FlutterWindow(); 20 | 21 | protected: 22 | // Win32Window: 23 | bool OnCreate() override; 24 | void OnDestroy() override; 25 | LRESULT MessageHandler(HWND window, UINT const message, WPARAM const wparam, 26 | LPARAM const lparam) noexcept override; 27 | 28 | private: 29 | // The run loop driving events for this window. 30 | RunLoop* run_loop_; 31 | 32 | // The project to run. 33 | flutter::DartProject project_; 34 | 35 | // The Flutter instance hosted by this window. 36 | std::unique_ptr flutter_controller_; 37 | }; 38 | 39 | #endif // RUNNER_FLUTTER_WINDOW_H_ 40 | -------------------------------------------------------------------------------- /example/windows/runner/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "flutter_window.h" 6 | #include "run_loop.h" 7 | #include "utils.h" 8 | 9 | int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev, 10 | _In_ wchar_t *command_line, _In_ int show_command) { 11 | // Attach to console when present (e.g., 'flutter run') or create a 12 | // new console when running with a debugger. 13 | if (!::AttachConsole(ATTACH_PARENT_PROCESS) && ::IsDebuggerPresent()) { 14 | CreateAndAttachConsole(); 15 | } 16 | 17 | // Initialize COM, so that it is available for use in the library and/or 18 | // plugins. 19 | ::CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED); 20 | 21 | RunLoop run_loop; 22 | 23 | flutter::DartProject project(L"data"); 24 | FlutterWindow window(&run_loop, project); 25 | Win32Window::Point origin(10, 10); 26 | Win32Window::Size size(1280, 720); 27 | if (!window.CreateAndShow(L"flutter_audio_desktop_example", origin, size)) { 28 | return EXIT_FAILURE; 29 | } 30 | window.SetQuitOnClose(true); 31 | 32 | run_loop.Run(); 33 | 34 | ::CoUninitialize(); 35 | return EXIT_SUCCESS; 36 | } 37 | -------------------------------------------------------------------------------- /example/windows/runner/resource.h: -------------------------------------------------------------------------------- 1 | //{{NO_DEPENDENCIES}} 2 | // Microsoft Visual C++ generated include file. 3 | // Used by Runner.rc 4 | // 5 | #define IDI_APP_ICON 101 6 | 7 | // Next default values for new objects 8 | // 9 | #ifdef APSTUDIO_INVOKED 10 | #ifndef APSTUDIO_READONLY_SYMBOLS 11 | #define _APS_NEXT_RESOURCE_VALUE 102 12 | #define _APS_NEXT_COMMAND_VALUE 40001 13 | #define _APS_NEXT_CONTROL_VALUE 1001 14 | #define _APS_NEXT_SYMED_VALUE 101 15 | #endif 16 | #endif 17 | -------------------------------------------------------------------------------- /example/windows/runner/resources/app_icon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alexmercerind/flutter_audio_desktop/d88f6761a499456596248303a683946e7d920967/example/windows/runner/resources/app_icon.ico -------------------------------------------------------------------------------- /example/windows/runner/run_loop.cpp: -------------------------------------------------------------------------------- 1 | #include "run_loop.h" 2 | 3 | #include 4 | 5 | #include 6 | 7 | RunLoop::RunLoop() {} 8 | 9 | RunLoop::~RunLoop() {} 10 | 11 | void RunLoop::Run() { 12 | bool keep_running = true; 13 | TimePoint next_flutter_event_time = TimePoint::clock::now(); 14 | while (keep_running) { 15 | std::chrono::nanoseconds wait_duration = 16 | std::max(std::chrono::nanoseconds(0), 17 | next_flutter_event_time - TimePoint::clock::now()); 18 | ::MsgWaitForMultipleObjects( 19 | 0, nullptr, FALSE, static_cast(wait_duration.count() / 1000), 20 | QS_ALLINPUT); 21 | bool processed_events = false; 22 | MSG message; 23 | // All pending Windows messages must be processed; MsgWaitForMultipleObjects 24 | // won't return again for items left in the queue after PeekMessage. 25 | while (::PeekMessage(&message, nullptr, 0, 0, PM_REMOVE)) { 26 | processed_events = true; 27 | if (message.message == WM_QUIT) { 28 | keep_running = false; 29 | break; 30 | } 31 | ::TranslateMessage(&message); 32 | ::DispatchMessage(&message); 33 | // Allow Flutter to process messages each time a Windows message is 34 | // processed, to prevent starvation. 35 | next_flutter_event_time = 36 | std::min(next_flutter_event_time, ProcessFlutterMessages()); 37 | } 38 | // If the PeekMessage loop didn't run, process Flutter messages. 39 | if (!processed_events) { 40 | next_flutter_event_time = 41 | std::min(next_flutter_event_time, ProcessFlutterMessages()); 42 | } 43 | } 44 | } 45 | 46 | void RunLoop::RegisterFlutterInstance( 47 | flutter::FlutterEngine* flutter_instance) { 48 | flutter_instances_.insert(flutter_instance); 49 | } 50 | 51 | void RunLoop::UnregisterFlutterInstance( 52 | flutter::FlutterEngine* flutter_instance) { 53 | flutter_instances_.erase(flutter_instance); 54 | } 55 | 56 | RunLoop::TimePoint RunLoop::ProcessFlutterMessages() { 57 | TimePoint next_event_time = TimePoint::max(); 58 | for (auto instance : flutter_instances_) { 59 | std::chrono::nanoseconds wait_duration = instance->ProcessMessages(); 60 | if (wait_duration != std::chrono::nanoseconds::max()) { 61 | next_event_time = 62 | std::min(next_event_time, TimePoint::clock::now() + wait_duration); 63 | } 64 | } 65 | return next_event_time; 66 | } 67 | -------------------------------------------------------------------------------- /example/windows/runner/run_loop.h: -------------------------------------------------------------------------------- 1 | #ifndef RUNNER_RUN_LOOP_H_ 2 | #define RUNNER_RUN_LOOP_H_ 3 | 4 | #include 5 | 6 | #include 7 | #include 8 | 9 | // A runloop that will service events for Flutter instances as well 10 | // as native messages. 11 | class RunLoop { 12 | public: 13 | RunLoop(); 14 | ~RunLoop(); 15 | 16 | // Prevent copying 17 | RunLoop(RunLoop const&) = delete; 18 | RunLoop& operator=(RunLoop const&) = delete; 19 | 20 | // Runs the run loop until the application quits. 21 | void Run(); 22 | 23 | // Registers the given Flutter instance for event servicing. 24 | void RegisterFlutterInstance( 25 | flutter::FlutterEngine* flutter_instance); 26 | 27 | // Unregisters the given Flutter instance from event servicing. 28 | void UnregisterFlutterInstance( 29 | flutter::FlutterEngine* flutter_instance); 30 | 31 | private: 32 | using TimePoint = std::chrono::steady_clock::time_point; 33 | 34 | // Processes all currently pending messages for registered Flutter instances. 35 | TimePoint ProcessFlutterMessages(); 36 | 37 | std::set flutter_instances_; 38 | }; 39 | 40 | #endif // RUNNER_RUN_LOOP_H_ 41 | -------------------------------------------------------------------------------- /example/windows/runner/runner.exe.manifest: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | PerMonitorV2 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /example/windows/runner/utils.cpp: -------------------------------------------------------------------------------- 1 | #include "utils.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include 9 | 10 | void CreateAndAttachConsole() { 11 | if (::AllocConsole()) { 12 | FILE *unused; 13 | if (freopen_s(&unused, "CONOUT$", "w", stdout)) { 14 | _dup2(_fileno(stdout), 1); 15 | } 16 | if (freopen_s(&unused, "CONOUT$", "w", stderr)) { 17 | _dup2(_fileno(stdout), 2); 18 | } 19 | std::ios::sync_with_stdio(); 20 | FlutterDesktopResyncOutputStreams(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /example/windows/runner/utils.h: -------------------------------------------------------------------------------- 1 | #ifndef RUNNER_UTILS_H_ 2 | #define RUNNER_UTILS_H_ 3 | 4 | // Creates a console for the process, and redirects stdout and stderr to 5 | // it for both the runner and the Flutter library. 6 | void CreateAndAttachConsole(); 7 | 8 | #endif // RUNNER_UTILS_H_ 9 | -------------------------------------------------------------------------------- /example/windows/runner/win32_window.cpp: -------------------------------------------------------------------------------- 1 | #include "win32_window.h" 2 | 3 | #include 4 | 5 | #include "resource.h" 6 | 7 | namespace { 8 | 9 | constexpr const wchar_t kWindowClassName[] = L"FLUTTER_RUNNER_WIN32_WINDOW"; 10 | 11 | // The number of Win32Window objects that currently exist. 12 | static int g_active_window_count = 0; 13 | 14 | using EnableNonClientDpiScaling = BOOL __stdcall(HWND hwnd); 15 | 16 | // Scale helper to convert logical scaler values to physical using passed in 17 | // scale factor 18 | int Scale(int source, double scale_factor) { 19 | return static_cast(source * scale_factor); 20 | } 21 | 22 | // Dynamically loads the |EnableNonClientDpiScaling| from the User32 module. 23 | // This API is only needed for PerMonitor V1 awareness mode. 24 | void EnableFullDpiSupportIfAvailable(HWND hwnd) { 25 | HMODULE user32_module = LoadLibraryA("User32.dll"); 26 | if (!user32_module) { 27 | return; 28 | } 29 | auto enable_non_client_dpi_scaling = 30 | reinterpret_cast( 31 | GetProcAddress(user32_module, "EnableNonClientDpiScaling")); 32 | if (enable_non_client_dpi_scaling != nullptr) { 33 | enable_non_client_dpi_scaling(hwnd); 34 | FreeLibrary(user32_module); 35 | } 36 | } 37 | 38 | } // namespace 39 | 40 | // Manages the Win32Window's window class registration. 41 | class WindowClassRegistrar { 42 | public: 43 | ~WindowClassRegistrar() = default; 44 | 45 | // Returns the singleton registar instance. 46 | static WindowClassRegistrar* GetInstance() { 47 | if (!instance_) { 48 | instance_ = new WindowClassRegistrar(); 49 | } 50 | return instance_; 51 | } 52 | 53 | // Returns the name of the window class, registering the class if it hasn't 54 | // previously been registered. 55 | const wchar_t* GetWindowClass(); 56 | 57 | // Unregisters the window class. Should only be called if there are no 58 | // instances of the window. 59 | void UnregisterWindowClass(); 60 | 61 | private: 62 | WindowClassRegistrar() = default; 63 | 64 | static WindowClassRegistrar* instance_; 65 | 66 | bool class_registered_ = false; 67 | }; 68 | 69 | WindowClassRegistrar* WindowClassRegistrar::instance_ = nullptr; 70 | 71 | const wchar_t* WindowClassRegistrar::GetWindowClass() { 72 | if (!class_registered_) { 73 | WNDCLASS window_class{}; 74 | window_class.hCursor = LoadCursor(nullptr, IDC_ARROW); 75 | window_class.lpszClassName = kWindowClassName; 76 | window_class.style = CS_HREDRAW | CS_VREDRAW; 77 | window_class.cbClsExtra = 0; 78 | window_class.cbWndExtra = 0; 79 | window_class.hInstance = GetModuleHandle(nullptr); 80 | window_class.hIcon = 81 | LoadIcon(window_class.hInstance, MAKEINTRESOURCE(IDI_APP_ICON)); 82 | window_class.hbrBackground = 0; 83 | window_class.lpszMenuName = nullptr; 84 | window_class.lpfnWndProc = Win32Window::WndProc; 85 | RegisterClass(&window_class); 86 | class_registered_ = true; 87 | } 88 | return kWindowClassName; 89 | } 90 | 91 | void WindowClassRegistrar::UnregisterWindowClass() { 92 | UnregisterClass(kWindowClassName, nullptr); 93 | class_registered_ = false; 94 | } 95 | 96 | Win32Window::Win32Window() { 97 | ++g_active_window_count; 98 | } 99 | 100 | Win32Window::~Win32Window() { 101 | --g_active_window_count; 102 | Destroy(); 103 | } 104 | 105 | bool Win32Window::CreateAndShow(const std::wstring& title, 106 | const Point& origin, 107 | const Size& size) { 108 | Destroy(); 109 | 110 | const wchar_t* window_class = 111 | WindowClassRegistrar::GetInstance()->GetWindowClass(); 112 | 113 | const POINT target_point = {static_cast(origin.x), 114 | static_cast(origin.y)}; 115 | HMONITOR monitor = MonitorFromPoint(target_point, MONITOR_DEFAULTTONEAREST); 116 | UINT dpi = FlutterDesktopGetDpiForMonitor(monitor); 117 | double scale_factor = dpi / 96.0; 118 | 119 | HWND window = CreateWindow( 120 | window_class, title.c_str(), WS_OVERLAPPEDWINDOW | WS_VISIBLE, 121 | Scale(origin.x, scale_factor), Scale(origin.y, scale_factor), 122 | Scale(size.width, scale_factor), Scale(size.height, scale_factor), 123 | nullptr, nullptr, GetModuleHandle(nullptr), this); 124 | 125 | if (!window) { 126 | return false; 127 | } 128 | 129 | return OnCreate(); 130 | } 131 | 132 | // static 133 | LRESULT CALLBACK Win32Window::WndProc(HWND const window, 134 | UINT const message, 135 | WPARAM const wparam, 136 | LPARAM const lparam) noexcept { 137 | if (message == WM_NCCREATE) { 138 | auto window_struct = reinterpret_cast(lparam); 139 | SetWindowLongPtr(window, GWLP_USERDATA, 140 | reinterpret_cast(window_struct->lpCreateParams)); 141 | 142 | auto that = static_cast(window_struct->lpCreateParams); 143 | EnableFullDpiSupportIfAvailable(window); 144 | that->window_handle_ = window; 145 | } else if (Win32Window* that = GetThisFromHandle(window)) { 146 | return that->MessageHandler(window, message, wparam, lparam); 147 | } 148 | 149 | return DefWindowProc(window, message, wparam, lparam); 150 | } 151 | 152 | LRESULT 153 | Win32Window::MessageHandler(HWND hwnd, 154 | UINT const message, 155 | WPARAM const wparam, 156 | LPARAM const lparam) noexcept { 157 | switch (message) { 158 | case WM_DESTROY: 159 | window_handle_ = nullptr; 160 | Destroy(); 161 | if (quit_on_close_) { 162 | PostQuitMessage(0); 163 | } 164 | return 0; 165 | 166 | case WM_DPICHANGED: { 167 | auto newRectSize = reinterpret_cast(lparam); 168 | LONG newWidth = newRectSize->right - newRectSize->left; 169 | LONG newHeight = newRectSize->bottom - newRectSize->top; 170 | 171 | SetWindowPos(hwnd, nullptr, newRectSize->left, newRectSize->top, newWidth, 172 | newHeight, SWP_NOZORDER | SWP_NOACTIVATE); 173 | 174 | return 0; 175 | } 176 | case WM_SIZE: 177 | RECT rect = GetClientArea(); 178 | if (child_content_ != nullptr) { 179 | // Size and position the child window. 180 | MoveWindow(child_content_, rect.left, rect.top, rect.right - rect.left, 181 | rect.bottom - rect.top, TRUE); 182 | } 183 | return 0; 184 | 185 | case WM_ACTIVATE: 186 | if (child_content_ != nullptr) { 187 | SetFocus(child_content_); 188 | } 189 | return 0; 190 | 191 | // Messages that are directly forwarded to embedding. 192 | case WM_FONTCHANGE: 193 | SendMessage(child_content_, WM_FONTCHANGE, NULL, NULL); 194 | return 0; 195 | } 196 | 197 | return DefWindowProc(window_handle_, message, wparam, lparam); 198 | } 199 | 200 | void Win32Window::Destroy() { 201 | OnDestroy(); 202 | 203 | if (window_handle_) { 204 | DestroyWindow(window_handle_); 205 | window_handle_ = nullptr; 206 | } 207 | if (g_active_window_count == 0) { 208 | WindowClassRegistrar::GetInstance()->UnregisterWindowClass(); 209 | } 210 | } 211 | 212 | Win32Window* Win32Window::GetThisFromHandle(HWND const window) noexcept { 213 | return reinterpret_cast( 214 | GetWindowLongPtr(window, GWLP_USERDATA)); 215 | } 216 | 217 | void Win32Window::SetChildContent(HWND content) { 218 | child_content_ = content; 219 | SetParent(content, window_handle_); 220 | RECT frame = GetClientArea(); 221 | 222 | MoveWindow(content, frame.left, frame.top, frame.right - frame.left, 223 | frame.bottom - frame.top, true); 224 | 225 | SetFocus(child_content_); 226 | } 227 | 228 | RECT Win32Window::GetClientArea() { 229 | RECT frame; 230 | GetClientRect(window_handle_, &frame); 231 | return frame; 232 | } 233 | 234 | HWND Win32Window::GetHandle() { 235 | return window_handle_; 236 | } 237 | 238 | void Win32Window::SetQuitOnClose(bool quit_on_close) { 239 | quit_on_close_ = quit_on_close; 240 | } 241 | 242 | bool Win32Window::OnCreate() { 243 | // No-op; provided for subclasses. 244 | return true; 245 | } 246 | 247 | void Win32Window::OnDestroy() { 248 | // No-op; provided for subclasses. 249 | } 250 | -------------------------------------------------------------------------------- /example/windows/runner/win32_window.h: -------------------------------------------------------------------------------- 1 | #ifndef RUNNER_WIN32_WINDOW_H_ 2 | #define RUNNER_WIN32_WINDOW_H_ 3 | 4 | #include 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | // A class abstraction for a high DPI-aware Win32 Window. Intended to be 11 | // inherited from by classes that wish to specialize with custom 12 | // rendering and input handling 13 | class Win32Window { 14 | public: 15 | struct Point { 16 | unsigned int x; 17 | unsigned int y; 18 | Point(unsigned int x, unsigned int y) : x(x), y(y) {} 19 | }; 20 | 21 | struct Size { 22 | unsigned int width; 23 | unsigned int height; 24 | Size(unsigned int width, unsigned int height) 25 | : width(width), height(height) {} 26 | }; 27 | 28 | Win32Window(); 29 | virtual ~Win32Window(); 30 | 31 | // Creates and shows a win32 window with |title| and position and size using 32 | // |origin| and |size|. New windows are created on the default monitor. Window 33 | // sizes are specified to the OS in physical pixels, hence to ensure a 34 | // consistent size to will treat the width height passed in to this function 35 | // as logical pixels and scale to appropriate for the default monitor. Returns 36 | // true if the window was created successfully. 37 | bool CreateAndShow(const std::wstring& title, 38 | const Point& origin, 39 | const Size& size); 40 | 41 | // Release OS resources associated with window. 42 | void Destroy(); 43 | 44 | // Inserts |content| into the window tree. 45 | void SetChildContent(HWND content); 46 | 47 | // Returns the backing Window handle to enable clients to set icon and other 48 | // window properties. Returns nullptr if the window has been destroyed. 49 | HWND GetHandle(); 50 | 51 | // If true, closing this window will quit the application. 52 | void SetQuitOnClose(bool quit_on_close); 53 | 54 | // Return a RECT representing the bounds of the current client area. 55 | RECT GetClientArea(); 56 | 57 | protected: 58 | // Processes and route salient window messages for mouse handling, 59 | // size change and DPI. Delegates handling of these to member overloads that 60 | // inheriting classes can handle. 61 | virtual LRESULT MessageHandler(HWND window, 62 | UINT const message, 63 | WPARAM const wparam, 64 | LPARAM const lparam) noexcept; 65 | 66 | // Called when CreateAndShow is called, allowing subclass window-related 67 | // setup. Subclasses should return false if setup fails. 68 | virtual bool OnCreate(); 69 | 70 | // Called when Destroy is called. 71 | virtual void OnDestroy(); 72 | 73 | private: 74 | friend class WindowClassRegistrar; 75 | 76 | // OS callback called by message pump. Handles the WM_NCCREATE message which 77 | // is passed when the non-client area is being created and enables automatic 78 | // non-client DPI scaling so that the non-client area automatically 79 | // responsponds to changes in DPI. All other messages are handled by 80 | // MessageHandler. 81 | static LRESULT CALLBACK WndProc(HWND const window, 82 | UINT const message, 83 | WPARAM const wparam, 84 | LPARAM const lparam) noexcept; 85 | 86 | // Retrieves a class instance pointer for |window| 87 | static Win32Window* GetThisFromHandle(HWND const window) noexcept; 88 | 89 | bool quit_on_close_ = false; 90 | 91 | // window handle for top level window. 92 | HWND window_handle_ = nullptr; 93 | 94 | // window handle for hosted content. 95 | HWND child_content_ = nullptr; 96 | }; 97 | 98 | #endif // RUNNER_WIN32_WINDOW_H_ 99 | -------------------------------------------------------------------------------- /flutter_audio_desktop.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /lib/flutter_audio_desktop.dart: -------------------------------------------------------------------------------- 1 | export 'package:flutter_audio_desktop/source/main.dart'; 2 | export 'package:flutter_audio_desktop/source/types/source.dart'; 3 | export 'package:flutter_audio_desktop/source/types/audio.dart'; 4 | export 'package:flutter_audio_desktop/source/core/devices.dart'; 5 | 6 | const String title = "flutter_audio_desktop"; 7 | const String author = "alexmercerind"; 8 | const String license = "MIT"; 9 | const String version = "0.0.9"; 10 | -------------------------------------------------------------------------------- /lib/source/core/channel.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter/services.dart'; 2 | 3 | /// Internal method channel used by plugin. 4 | final MethodChannel channel = MethodChannel('flutter_audio_desktop'); 5 | -------------------------------------------------------------------------------- /lib/source/core/devices.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter_audio_desktop/source/core/channel.dart'; 2 | 3 | class AudioDevice { 4 | /// ID of this audio device. 5 | final String id; 6 | 7 | /// Name of this audio device. 8 | final String name; 9 | 10 | /// A playback audio device connected to the device. 11 | AudioDevice(this.id, this.name); 12 | } 13 | 14 | class AudioDevices { 15 | /// ### Gets all connected playback devices 16 | /// 17 | /// Returns [List] of all [AudioDevice] connected to the device. 18 | /// 19 | /// ```dart 20 | /// List devices = await AudioDevices.allDevices; 21 | /// ``` 22 | /// 23 | static Future> get allDevices async { 24 | List devices = []; 25 | var devicesMap = await channel.invokeMethod( 26 | 'getDevices', 27 | {}, 28 | ); 29 | devicesMap.forEach((id, name) { 30 | if (id != 'default') 31 | devices.add( 32 | new AudioDevice( 33 | id?.toString(), 34 | name?.toString(), 35 | ), 36 | ); 37 | }); 38 | return devices; 39 | } 40 | 41 | /// ### Gets default connected playback device 42 | /// 43 | /// Returns default [AudioDevice] with id `'default'`. 44 | /// 45 | /// ```dart 46 | /// List devices = await AudioDevices.allDevices; 47 | /// ``` 48 | /// 49 | static Future get defaultDevice async { 50 | AudioDevice defaultDevice; 51 | var devicesMap = await channel.invokeMethod( 52 | 'getDevices', 53 | {}, 54 | ); 55 | devicesMap.forEach((id, name) { 56 | if (id == 'default') 57 | defaultDevice = new AudioDevice( 58 | id?.toString(), 59 | name?.toString(), 60 | ); 61 | }); 62 | if (defaultDevice == null) { 63 | throw 'EXCEPTION: Could not find the default device.'; 64 | } 65 | return defaultDevice; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /lib/source/core/events.dart: -------------------------------------------------------------------------------- 1 | import 'dart:io'; 2 | 3 | import 'package:flutter_audio_desktop/source/core/channel.dart'; 4 | import 'package:flutter_audio_desktop/source/types/audio.dart'; 5 | 6 | class AudioPlayerInternal { 7 | /// Unique ID of the audio player instance. 8 | int id; 9 | 10 | /// Device ID of the device ,to which the audio player is playing. 11 | String deviceId; 12 | 13 | /// Broadcast stream to listen to playback events e.g. completion, loading, play/pause etc. 14 | Stream