sound_item.ts 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { _decorator, AudioClip, AudioSource, Button, Component, Label, Node, Sprite, SpriteFrame } from 'cc';
  2. import { main } from '../main';
  3. import { ClientEvent } from '../clientEvent';
  4. import { config } from '../config';
  5. import { bag_item_data, widget_item_data } from '../../data/data';
  6. import { base_res } from './base_res';
  7. const { ccclass, property } = _decorator;
  8. @ccclass('sound_item')
  9. export class sound_item extends base_res {
  10. @property(Node) img_icon:Node = null;
  11. @property(Node) lab_name:Node = null;
  12. private m_audio_clip:AudioClip = null;
  13. private m_name:string ="";
  14. @property(Node) btn_play:Node = null;
  15. @property(SpriteFrame) sf_pause:SpriteFrame = null;
  16. @property(SpriteFrame) sf_play:SpriteFrame = null;
  17. public initView(type:number,data:bag_item_data,name:string,ac:AudioClip){
  18. this.setData(data)
  19. this.setType(type)
  20. this.m_audio_clip = ac;
  21. this.m_name = name;
  22. this.lab_name.getComponent(Label).string = name;
  23. this.init()
  24. }
  25. public init(isCanMove:boolean = true){
  26. if(isCanMove){
  27. this.btn_play.on(Node.EventType.TOUCH_END,()=>{
  28. ClientEvent.dispatchEvent(config.Event.PLAY_SOUND,this.m_name)
  29. })
  30. }
  31. ClientEvent.on(config.Event.PLAY_SOUND,this.onPlaySound,this)
  32. if(isCanMove){
  33. this.node.on(Node.EventType.MOUSE_DOWN,this.onDragRes.bind(this),this)
  34. }
  35. }
  36. public changeAudio(data:bag_item_data,ac:AudioClip){
  37. this.m_name = data.name;
  38. this.setData(data)
  39. this.setType(config.select_res_btn_type.SOUND_LIST)
  40. this.m_audio_clip = ac;
  41. this.lab_name.getComponent(Label).string = this.m_name;
  42. this.btn_play.off(Node.EventType.TOUCH_END)
  43. this.btn_play.on(Node.EventType.TOUCH_END,()=>{
  44. this.onPlaySound(this.m_name)
  45. })
  46. }
  47. onDragRes(){
  48. ClientEvent.dispatchEvent(config.Event.DragRes,this.node)
  49. }
  50. protected onDestroy(): void {
  51. ClientEvent.off(config.Event.PLAY_SOUND,this.onPlaySound,this)
  52. }
  53. onPlaySound(name:string){
  54. if(this.m_name===name){
  55. if(main.cur_play_audio===name&&main.getAudioSource().playing){
  56. main.getAudioSource().stop()
  57. this.btn_play.getComponent(Sprite).spriteFrame = this.sf_pause
  58. main.cur_play_audio = "";
  59. }else{
  60. main.g_audioSource.node.off(AudioSource.EventType.STARTED);
  61. main.g_audioSource.node.off(AudioSource.EventType.ENDED);
  62. main.g_audioSource.node.on(AudioSource.EventType.STARTED, this.onAudioStarted, this);
  63. // Register the ended event callback
  64. main.g_audioSource.node.on(AudioSource.EventType.ENDED, this.onAudioEnded, this);
  65. this.btn_play.getComponent(Sprite).spriteFrame = this.sf_play
  66. if (this.m_audio_clip instanceof AudioClip) {
  67. main.getAudioSource().clip = this.m_audio_clip;
  68. main.getAudioSource().play();
  69. main.getAudioSource().volume = 1;
  70. }
  71. main.cur_play_audio = name;
  72. }
  73. }else{
  74. if(this.btn_play) {
  75. this.btn_play.getComponent(Sprite).spriteFrame = this.sf_pause
  76. }
  77. }
  78. }
  79. onAudioStarted(){
  80. }
  81. onAudioEnded(){
  82. if(this.btn_play) {
  83. this.btn_play.getComponent(Sprite).spriteFrame = this.sf_pause
  84. }
  85. }
  86. }