-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[16.0][IMP] web_notify:t3894 Allow notify with sound #2923
Open
GabbasovDinar
wants to merge
1
commit into
OCA:16.0
Choose a base branch
from
cetmix:16.0-t3894-web_notify-sound-on-notification
base: 16.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,5 @@ | |
* `Tecnativa <https://www.tecnativa.com>`_: | ||
|
||
* David Vidal | ||
|
||
* Cetmix OÜ <https://cetmix.com/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/** @odoo-module alias=web_notify.AudioPlayer **/ | ||
|
||
import {Component, useState} from "@odoo/owl"; | ||
|
||
/** | ||
* @typedef AudioPlayerProps | ||
* @property {string} src URL of the audio file to be played | ||
* @property {number} [volume=1.0] Volume level of the audio (from 0.0 to 1.0) | ||
* @property {boolean} [loop=false] Whether the audio should loop | ||
* @property {Function} [onEnded] Callback function to be called when the audio ends | ||
*/ | ||
|
||
/** | ||
* The AudioPlayer component is responsible for playing audio files with | ||
* specified settings like volume and looping. It also provides the ability | ||
* to trigger actions when the audio playback ends. | ||
*/ | ||
export class AudioPlayer extends Component { | ||
setup() { | ||
this.state = useState({isPlaying: false}); | ||
this.audioElement = new Audio(this.props.src); | ||
|
||
// Set audio properties | ||
this.audioElement.volume = this.props.volume || 1.0; | ||
this.audioElement.loop = this.props.loop || false; | ||
|
||
// Start playing the audio | ||
this.audioElement | ||
.play() | ||
.then(() => { | ||
this.state.isPlaying = true; | ||
}) | ||
.catch((error) => { | ||
console.error("Audio playback failed:", error); | ||
}); | ||
|
||
// Listen for the end of the audio playback | ||
this.audioElement.addEventListener("ended", this.onAudioEnded.bind(this)); | ||
} | ||
|
||
/** | ||
* Stops the audio playback and triggers the onEnded callback if provided. | ||
*/ | ||
stopAudio() { | ||
this.audioElement.pause(); | ||
this.audioElement.currentTime = 0; | ||
this.state.isPlaying = false; | ||
|
||
if (this.props.onEnded) { | ||
this.props.onEnded(); | ||
} | ||
} | ||
|
||
/** | ||
* Handler for when the audio playback ends. | ||
*/ | ||
onAudioEnded() { | ||
if (!this.props.loop) { | ||
this.stopAudio(); | ||
} | ||
} | ||
|
||
willUnmount() { | ||
// Clean up the audio element and listeners | ||
this.audioElement.removeEventListener("ended", this.onAudioEnded); | ||
this.audioElement.pause(); | ||
} | ||
} | ||
|
||
AudioPlayer.template = "web_notify.AudioPlayer"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<!-- Copyright 2024 Cetmix OÜ | ||
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). --> | ||
<templates xml:space="preserve"> | ||
|
||
<t t-name="web_notify.AudioPlayer" owl="1"> | ||
<!-- No visual elements needed, audio is controlled programmatically --> | ||
</t> | ||
|
||
</templates> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** @odoo-module **/ | ||
|
||
import {registry} from "@web/core/registry"; | ||
import {AudioPlayer} from "../components/audio_player.esm"; | ||
const effectRegistry = registry.category("effects"); | ||
|
||
// ----------------------------------------------------------------------------- | ||
// Audio effect | ||
// ----------------------------------------------------------------------------- | ||
|
||
/** | ||
* Handles effect of type "audio_effect". It returns the AudioPlayer component | ||
* with the given audio source URL and other properties. | ||
* | ||
* @param {Object} env | ||
* @param {Object} [params={}] | ||
* @param {string} params.src | ||
* The URL of the audio file to play. | ||
* @param {number} [params.volume=1.0] Volume level of the audio (from 0.0 to 1.0) | ||
* @param {boolean} [params.loop=false] Whether the audio should loop | ||
* @param {Function} [params.onEnded] Callback function to be called when the audio ends | ||
*/ | ||
|
||
function audioEffect(env, params = {}) { | ||
if (!params.src) { | ||
console.warn( | ||
"Audio effect requires a 'src' parameter with the URL of the audio file." | ||
); | ||
return; | ||
} | ||
|
||
return { | ||
Component: AudioPlayer, | ||
props: { | ||
src: params.src, | ||
volume: params.volume || 1.0, | ||
loop: params.loop || false, | ||
onEnded: params.onEnded, | ||
}, | ||
}; | ||
} | ||
|
||
effectRegistry.add("audio_effect", audioEffect); |
58 changes: 58 additions & 0 deletions
58
web_notify/static/src/services/notification_sound_service.esm.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** @odoo-module **/ | ||
|
||
import {registry} from "@web/core/registry"; | ||
|
||
/** | ||
* The notificationSoundService is responsible for handling the playback of audio | ||
* notifications when a new notification is added. This service integrates with | ||
* the notification system and the effect service to provide audible feedback | ||
* based on the type of notification. | ||
* | ||
* Dependencies: | ||
* - notification: The service responsible for displaying notifications on the UI. | ||
* - effect: The service that handles visual and auditory effects in the application. | ||
*/ | ||
|
||
export const notificationSoundService = { | ||
dependencies: ["notification", "effect"], | ||
|
||
/** | ||
* Starts the notification sound service, enabling sound playback for notifications. | ||
* | ||
* @param {Object} env The environment object, providing access to various services. | ||
* @param {Object} services An object containing the dependencies (notification, effect). | ||
* @returns {Object} The add function, used to add notifications with sound. | ||
*/ | ||
start(env, {notification, effect}) { | ||
/** | ||
* Adds a notification with an associated sound effect. | ||
* | ||
* @param {String} message The message to be displayed in the notification. | ||
* @param {Object} [options={}] Additional options for the notification, such as type, sound and etc | ||
* @returns {Function} A function to close the notification. | ||
*/ | ||
function add(message, options = {}) { | ||
const sound = options.sound || false; | ||
delete options.sound; // Remove sound option from the options before passing to notification | ||
|
||
const closeFn = notification.add(message, options); | ||
|
||
if (sound) | ||
// Trigger the audio effect. | ||
effect.add({ | ||
type: "audio_effect", | ||
src: sound, | ||
volume: 0.8, | ||
loop: false, | ||
onEnded: () => { | ||
// Placeholder for any action after sound ends | ||
}, | ||
}); | ||
return closeFn; | ||
} | ||
return {add}; | ||
}, | ||
}; | ||
|
||
// Register the notification sound service in the service registry | ||
registry.category("services").add("notification_sound", notificationSoundService); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not using a service like SoundEffects
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@chienandalu
this service is limited to predefined sound effect settings and does not allow you to specify a link to any sound. In my case, only the sound effect link is passed for playback, which allows any sound to be dynamically played back
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I mean, you can take inspiration in that code to implement your own sound service, resulting in a simpler impletation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@chienandalu I was already inspired by this effect to write the current solution :-)