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

Farmer app audio support #290

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ <h2 class="step-title">{{ step.title | translate }}</h2>

}
</div>
<div>
<h2>Test Audio Playback</h2>
<picsa-audio-playback audioUrl="https://codeskulptor-demos.commondatastorage.googleapis.com/GalaxyInvaders/theme_01.mp3"></picsa-audio-playback>
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import { RouterModule } from '@angular/router';
import { FARMER_CONTENT_DATA } from '@picsa/data';
import { FadeInOut } from '@picsa/shared/animations';
import { PicsaScrollRestoreDirective } from '@picsa/shared/directives';
import { AudioPlaybackComponent } from '@picsa/shared/features/audio-playback';
import { AudioService } from '@picsa/shared/features/audio-playback';
import { PicsaTranslateModule } from '@picsa/shared/modules';

// import TestAudio from '@picsa/farmer-content/src/assets/'
@Component({
selector: 'farmer-home',
standalone: true,
Expand All @@ -20,11 +23,14 @@ import { PicsaTranslateModule } from '@picsa/shared/modules';
PicsaScrollRestoreDirective,
PicsaTranslateModule,
RouterModule,
AudioPlaybackComponent
],
providers: [AudioService],
templateUrl: './farmer-home.component.html',
styleUrl: './farmer-home.component.scss',
animations: [FadeInOut()],
})
export class FarmerContentHomeComponent {
public content = FARMER_CONTENT_DATA;
// public testAudio = TestAudio
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<button (click)="togglePlayback()" class="audio-button">
<div *ngIf="isAudioPlaying; else playIcon">
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit(non-blocking) - it would be good for the icon to change when play button first clicked (currently only changes when clicked twice, once to start and once to pause), so that the user gets feedback that the audio click was registered. Could also consider putting the icon in a mat-button to get a ripple effect

nit(non-blocking) - feel free if you want to use the @if(...) and @else(...) syntax. Can also probably just put the if/else around the icon and not need additional ng-template

<mat-icon>play_circle_outline</mat-icon>
</div>
<ng-template #playIcon>
<mat-icon>headset</mat-icon>
</ng-template>
</button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.audio-button{
outline: none;
background-color: inherit;
border: none;
padding: 5px;
.mat-icon{
color: #7292C7;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit(non-blocking): might be good to link the color to the primary/secondary pallette. You could use var(--color-secondary) to get the deeper blue, or if you want to register a lighter variant you can define it in libs\theme\src\themes\_mixins.scss, testing with something like

  --color-secondary-200: #{map-get($accent, 200)};

The full list of colors that could be registered can be seen in libs\theme\src\themes\picsa-default.scss (currently just the 500 and 50 weight variants are in use)

font-size: 28px;
height: 32px;
width: 32px;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { AudioPlaybackComponent } from './audio-playback.component';

describe('AudioPlaybackComponent', () => {
let component: AudioPlaybackComponent;
let fixture: ComponentFixture<AudioPlaybackComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [AudioPlaybackComponent],
}).compileComponents();

fixture = TestBed.createComponent(AudioPlaybackComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Component, ViewChild, ElementRef, OnDestroy, Input } from '@angular/core';
import { CommonModule } from '@angular/common';
import { AudioService } from './audio-playback.service';
import { MatIconModule } from '@angular/material/icon';

@Component({
selector: 'picsa-audio-playback',
standalone: true,
imports:[CommonModule, MatIconModule],
templateUrl: './audio-playback.component.html',
styleUrls: ['./audio-playback.component.scss'],
})
export class AudioPlaybackComponent implements OnDestroy {
@Input() audioUrl: string;
isAudioPlaying = false;

constructor(private audioService: AudioService) {}

togglePlayback(): void {
if (this.audioService.isPlaying()) {
this.audioService.pauseAudio();
this.isAudioPlaying = true;
} else {
this.audioService.playAudio(this.audioUrl);
this.isAudioPlaying = false;
}
}

isPlaying(): boolean {
return this.audioService.isPlaying();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit(non-blocking): I like how you have linked the audio play detection to the service, as it's possible that the service will stop component playback (e.g. if the user tries to start a second audio while the first one is playing), however you might need to remove the isAudioPlaying variable from this component to avoid confusion

}

ngOnDestroy(): void {
this.audioService.stop();
}
}
35 changes: 35 additions & 0 deletions libs/shared/src/features/audio-playback/audio-playback.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Injectable } from '@angular/core';
import { Router, NavigationStart } from '@angular/router';

@Injectable({
providedIn: 'root',
})
export class AudioService {
private audio: HTMLAudioElement;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 - nice you were able to do get things working with the standard html audio api. In another project I've used HowlerJS, however it looks like the api should be sufficient for now


constructor() {
this.audio = new Audio();
}

playAudio(url: string): void {
if (this.audio.src !== url) {
this.audio.src = url;
}
this.audio.play();
}

pauseAudio(): void {
this.audio.pause();
}

isPlaying(): boolean {
return !this.audio.paused;
}

stop(): void {
if (this.audio) {
this.audio.pause();
this.audio.currentTime = 0;
}
}
}
2 changes: 2 additions & 0 deletions libs/shared/src/features/audio-playback/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './audio-playback.component';
export * from './audio-playback.service';
1 change: 1 addition & 0 deletions libs/shared/src/features/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './animations';
export * from './audio-playback';
export * from './charts';
export * from './data-table';
export * from './dialog';
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -16925,7 +16925,7 @@ __metadata:
"leaflet-draw@github:enketo/Leaflet.draw#ff730785db7fcccbf2485ffcf4dffe1238a7c617":
version: 1.0.4
resolution: "leaflet-draw@https://github.com/enketo/Leaflet.draw.git#commit=ff730785db7fcccbf2485ffcf4dffe1238a7c617"
checksum: b08b88994769667f11f2b6a8937656c89cea34dafd4661abab0b48b4b97f3bddbdce7b23ddfdb8d7c6335e065530e32a70e281314afa34afa134bf68597945fc
checksum: b2280f200f5ea410e33cc5feb500d67d86cb5f16b294eea1306da055614ffc906be7de6dbc9bbf4db6e6e5d27d0396e4701a3b6c4c920548d2aac94b8223879f
languageName: node
linkType: hard

Expand Down
Loading