Skip to content
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

Support notification on media change #10

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 19 additions & 2 deletions renderer/actions/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { setPlayer } from "../utils"
import C from "./constant"
import { appIcon } from "../assets/staticbase64"
const fs = window.require("fs")
const path = window.require("path")
const { app, dialog } = window.require("electron").remote
const { app, dialog, Notification, nativeImage } = window.require("electron").remote
const mm = window.require("music-metadata")

export const requestUpdateLibrary = () => ({
Expand Down Expand Up @@ -103,7 +104,10 @@ export function playMedia (media, mediaPlayer) {
// resume play if the same media is already in progress and paused
if (mediaPlayer.currentTime > 0 && mediaPlayer.paused && !mediaPlayer.ended && media.file === currentMedia) {
mediaPlayer.play()
.then(() => dispatch(setCurrentMediaMode("Playing")))
.then(() => {
NotifyOnMediaChange(media.artist, media.title)
return dispatch(setCurrentMediaMode("Playing"))
})
.catch(() => dispatch(setCurrentMediaMode("Paused")))
} else {
// start a fresh play
Expand All @@ -112,6 +116,18 @@ export function playMedia (media, mediaPlayer) {
}
}

function NotifyOnMediaChange (artist, title) {
if (artist && title && Notification.isSupported()) {
const image = nativeImage.createFromDataURL(`data:image/png;base64,${appIcon}`)
const notification = new Notification({
title: title,
body: `by ${artist}`,
icon: image
})
notification.show()
}
}

function fetchMediaBuffer (url, loadMedia) {
if ((url.startsWith("https://") || url.startsWith("http://")) && !navigator.onLine) {
const title = "Network error"
Expand Down Expand Up @@ -169,6 +185,7 @@ function setupMediaSrc (media, mediaPlayer) {
mediaSrc.endOfStream()
mediaPlayer.play()
.then(() => {
NotifyOnMediaChange(media.artist, media.title)
// dispatch(updateMediaInfo(url))
dispatch(setCurrentMediaMode("Playing"))
})
Expand Down
23 changes: 19 additions & 4 deletions renderer/components/Control.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -541,15 +541,30 @@ class Control extends React.Component {
// If we're at the last song, start afresh
// else play next song
if (i === songs.length - 1) {
next = songs[0].file
next = {
file: songs[0].file,
title: songs[0].title,
artist: songs[0].artist
}
} else if (shuffle) {
var rand = this.generateRandomNumber(i, songs.length)
next = songs[rand].file
next = {
file: songs[rand].file,
title: songs[rand].title,
artist: songs[rand].artist
}
} else {
next = songs[++i].file
const j = ++i
next = {
file: songs[j].file,
title: songs[j].title,
artist: songs[j].artist
}
}
const media = {
file: next,
file: next.file,
title: next.title,
artist: next.artist,
source: source
}
dispatch(playMedia(media, getPlayer()))
Expand Down