All files / modules/bgm bgm.service.ts

0% Statements 0/49
100% Branches 1/1
100% Functions 1/1
0% Lines 0/49

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93                                                                                                                                                                                         
/**
 * 背景音乐服务
 */
interface BgmItem {
  id: number;
  name: string;
  url: string;
  mood: string;
  duration: number;
  size?: number;
}
 
export class BgmService {
  // 背景音乐数据
  private bgmList: BgmItem[] = [
    { id: 1, name: '轻柔钢琴', url: 'https://example.com/bgm/light-piano.mp3', mood: '轻柔', duration: 180, size: 2400000 },
    { id: 2, name: '温暖弦乐', url: 'https://example.com/bgm/warm-strings.mp3', mood: '温暖', duration: 240, size: 3200000 },
    { id: 3, name: '自然雨声', url: 'https://example.com/bgm/rain-nature.mp3', mood: '自然', duration: 300, size: 4000000 },
    { id: 4, name: '咖啡馆氛围', url: 'https://example.com/bgm/cafe-ambient.mp3', mood: '氛围', duration: 200, size: 2600000 },
    { id: 5, name: '企业配乐', url: 'https://example.com/bgm/corporate.mp3', mood: '商务', duration: 150, size: 2000000 },
    { id: 6, name: '欢快节拍', url: 'https://example.com/bgm/upbeat.mp3', mood: '欢快', duration: 180, size: 2400000 },
    { id: 7, name: '抒情慢歌', url: 'https://example.com/bgm/ballad.mp3', mood: '抒情', duration: 220, size: 2900000 },
    { id: 8, name: '古风配乐', url: 'https://example.com/bgm/ancient-chinese.mp3', mood: '古风', duration: 260, size: 3400000 },
    { id: 9, name: '科技感', url: 'https://example.com/bgm/tech.mp3', mood: '科技', duration: 190, size: 2500000 },
    { id: 10, name: '儿童音乐', url: 'https://example.com/bgm/children.mp3', mood: '童趣', duration: 160, size: 2100000 },
  ];
 
  // 风格分类
  private moods = ['轻柔', '温暖', '自然', '氛围', '商务', '欢快', '抒情', '古风', '科技', '童趣'];
 
  /**
   * 获取所有背景音乐
   */
  getBgmList(): BgmItem[] {
    return this.bgmList;
  }
 
  /**
   * 按风格筛选背景音乐
   */
  getBgmByMood(mood: string): BgmItem[] {
    if (!mood) return this.bgmList;
    return this.bgmList.filter(bgm => bgm.mood === mood);
  }
 
  /**
   * 获取单个背景音乐
   */
  getBgmById(id: number): BgmItem | undefined {
    return this.bgmList.find(bgm => bgm.id === id);
  }
 
  /**
   * 获取所有风格
   */
  getMoods(): string[] {
    return this.moods;
  }
 
  /**
   * 添加背景音乐
   */
  addBgm(data: { name: string; url: string; mood: string; duration: number }): BgmItem {
    const newBgm: BgmItem = {
      id: this.bgmList.length + 1,
      ...data,
    };
    this.bgmList.push(newBgm);
    return newBgm;
  }
 
  /**
   * 更新背景音乐
   */
  updateBgm(id: number, data: Partial<{ name: string; url: string; mood: string; duration: number; size: number }>): BgmItem | null {
    const index = this.bgmList.findIndex(bgm => bgm.id === id);
    if (index === -1) return null;
    this.bgmList[index] = { ...this.bgmList[index], ...data };
    return this.bgmList[index];
  }
 
  /**
   * 删除背景音乐
   */
  deleteBgm(id: number): boolean {
    const index = this.bgmList.findIndex(bgm => bgm.id === id);
    if (index === -1) return false;
    this.bgmList.splice(index, 1);
    return true;
  }
}
 
export const bgmService = new BgmService();