You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
98 lines
2.9 KiB
98 lines
2.9 KiB
using UnityEngine; |
|
using UnityEngine.UI; |
|
using System.IO; |
|
using UniRx; |
|
using AX.AudioSystem; |
|
|
|
public class ChatAudioItem : ItemBase<ChatAudioMessage> |
|
{ |
|
// 用户姓名 |
|
public Text UserName; |
|
// 语音内容 |
|
public Button AudioContent; |
|
// UI动画 |
|
public UGUISpriteAnimation IsPlaying; |
|
// 播放状态 |
|
public GameObject IsPlayingIcon; |
|
//语音时长 |
|
public Text TimeSpanSeconds; |
|
//语音播放器 |
|
private AudioPlayer ChatAudioPlayer; |
|
// 文件路径 |
|
private string filePath; |
|
|
|
public override void Init() |
|
{ |
|
AudioContent.OnClickAsObservable() |
|
.Subscribe(x => Play()) |
|
.AddTo(this); |
|
} |
|
|
|
public override void UpdateItemData() |
|
{ |
|
// 获取发送人 |
|
long senderId = Data.SenderId; |
|
UserData user = CurrentUserInfo.room.FindUserById(senderId); |
|
// 设置发送人姓名 |
|
UserName.text = string.Format("{0}", user != null ? user.UserInfo.RealName : "系统"); |
|
// 获取发送内容 |
|
TimeSpanSeconds.text = string.Format("{0}\"", Data.TimeSpanSeconds); |
|
// 动画默认不播放 |
|
IsPlaying.IsPlaying = false; |
|
// 语音是否播放过 |
|
IsPlayingIcon.SetActive(!Data.IsPlayed); |
|
// 根据 |
|
if (ChatAudioPlayer == null) |
|
{ |
|
// 聊天播放器 |
|
ChatAudioPlayer = AudioManager.Instance.GetAudioPlayer(Data.ChatGroup.ToString()); |
|
} |
|
filePath = Path.Combine(ChatManager.Instance.UserPath, Data.FileName); |
|
// 语音没有播放过,添加到自动播放 |
|
if (!Data.IsPlayed) |
|
{ |
|
ChatAudioPlayer.PlayScheduled(filePath); |
|
} |
|
} |
|
|
|
public override void UpdateItemSize() |
|
{ |
|
Vector2 audioSize = new Vector2(70 + Data.TimeSpanSeconds * 5, 33); |
|
(AudioContent.transform as RectTransform).sizeDelta = audioSize; |
|
} |
|
|
|
private void Update() |
|
{ |
|
int index = ChatAudioPlayer.PlayList.IndexOf(filePath); |
|
if (index != -1) |
|
{ |
|
Track audio = ChatAudioPlayer.PlayList[index]; |
|
IsPlaying.IsPlaying = audio.Playing; |
|
Data.IsPlayed = audio.Played; |
|
IsPlayingIcon.SetActive(!Data.IsPlayed); |
|
} |
|
} |
|
public void Play() |
|
{ |
|
// 关闭除当前消息组的其他播放器 |
|
AudioManager.Instance.StopAllWithout(Data.ChatGroup.ToString()); |
|
// 判断聊天语音播放器是否在播放 |
|
if (ChatAudioPlayer.IsPlaying) |
|
{ |
|
// 想要播放的音频是否在播放中 |
|
if (filePath == ChatAudioPlayer.PlayList[ChatAudioPlayer.PlayIndex].Filename) |
|
{ |
|
ChatAudioPlayer.Stop(); |
|
} |
|
else |
|
{ |
|
ChatAudioPlayer.Stop(); |
|
ChatAudioPlayer.Play(filePath); |
|
} |
|
} |
|
else |
|
{ |
|
ChatAudioPlayer.Play(filePath); |
|
} |
|
} |
|
}
|
|
|