More core audio work

This commit is contained in:
root
2026-02-26 11:23:10 +00:00
parent c8141a07f8
commit 3116bfb681
13 changed files with 291 additions and 38 deletions
+71 -13
View File
@@ -1,30 +1,88 @@
import {HttpClient} from '@angular/common/http';
import {FileSystemFile, Track, TrackMeta, PlaylistItem} from '../models';
import {Signal} from '@angular/core';
import {IndexedArray} from '../lib';
export class AudioService {
protected context: AudioContext;
protected track?: MediaElementAudioSourceNode;
protected playlist: IndexedArray<PlaylistItem> = new IndexedArray();
public list: Signal<PlaylistItem[]> = this.playlist.playlist;
public current: Signal<PlaylistItem|null> = this.playlist.current;
public index: Signal<number> = this.playlist.index;
constructor() {
this.context = new AudioContext();
this.addItem(new Track(
new FileSystemFile('1.mp3', 'audio/1.mp3', 0),
new TrackMeta(),
0,
'mp3',
128
));
this.addItem(new Track(
new FileSystemFile('2.mp3', 'audio/2.mp3', 0),
new TrackMeta(),
0,
'mp3',
128
));
this.addItem(new Track(
new FileSystemFile('3.mp3', 'audio/3.mp3', 0),
new TrackMeta(),
0,
'mp3',
128
));
}
createElement(filename: string) {
const element = document.createElement('audio');
const source = document.createElement('source');
source.src = filename;
source.type = 'audio/mp3';
element.appendChild(source);
return element;
addItem(track: Track) {
this.playlist.add(new PlaylistItem(track, this.context));
}
removeItem(item: PlaylistItem) {
if (item.isPlaying() && item === this.current()) {
this.current()?.stop();
this.playlist.remove(item);
this.current()?.play();
return;
}
this.playlist.remove(item);
}
async play(): Promise<void> {
if (this.context.state === 'suspended') {
await this.context.resume();
}
const element = this.createElement('audio/1.mp3');
this.track = this.context.createMediaElementSource(element);
this.track.connect(this.context.destination);
await element.play();
await this.current()?.play();
}
async stop(): Promise<void> {
await this.current()?.stop();
}
async next(): Promise<void> {
if (this.current()?.isPlaying()) {
await this.current()?.stop();
this.playlist.next();
await this.current()?.play();
} else {
this.playlist.next();
}
}
async prev(): Promise<void> {
if (this.current()?.isPlaying()) {
await this.current()?.stop();
this.playlist.prev();
await this.current()?.play();
} else {
this.playlist.prev();
}
}
async pause(): Promise<void> {
await this.current()?.pause();
}
}