123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- import SoundStatus from '../const/soundStatus';
- import SoundRecording, {TriggerListener} from '$utils/tool/sound-recording';
- import popup from '$utils/tool/popup';
- import file from '$utils/tool/file';
- import AudioStatus from "$components/sound-recording/const/audio";
- import Status from "$mixins/status/const/status";
- export default <LibMixins>{
- data(){
- return {
- SoundStatus,
- soundStatus:SoundStatus.none,
- soundStatusMessage:{
- [SoundStatus.none]:'长按开始录音',
- [SoundStatus.sound]:'正在录音',
- [SoundStatus.jurisdiction]:'浏览器授权后,请点击重试',
- [SoundStatus.support]:'暂不支持'
- },
- countdown:0,
- support:true,
- assets:false
- }
- },
- methods:{
- // 设置当前录音状态
- setSoundStatus(status:SoundStatus){
- if (this.soundStatus !== status) {
- // 如果状态为 none 清楚资源
- if (status === SoundStatus.none) {
- this.clearAssets();
- }
- this.soundStatus = status;
- }
- },
- reRecording(){
- if (this.status === Status.loading) return;
- if (!this.soundRecording) this.installUseSoundRecording();
- return this.setSoundStatus(SoundStatus.none);
- },
- // 设置授权状态
- setJurisdictionStatus(config:Record<string, any>){
- if (config.support === false) {
- return this.setSoundStatus(SoundStatus.support);
- }
- if (config.jurisdiction === false ) {
- return this.setSoundStatus(SoundStatus.jurisdiction);
- }
- },
- start(){
- if (this.soundStatus === SoundStatus.none ) {
- return this.soundRecording.start();
- }
- },
- stop(){
- if (this.soundStatus === SoundStatus.sound ) {
- return this.soundRecording.stop();
- }
- },
- // 安装
- installSoundRecording(){
- if (this.soundRecording.initializationStatus) return;
- // 安装
- this.soundRecording.initialization().then(()=>{
- this.setJurisdictionStatus({
- jurisdiction:true,
- support:true
- });
- this.soundRecording
- .addListener(TriggerListener.fail,(message)=>{
- // 设置状态从头开始
- this.setSoundStatus(SoundStatus.none);
- message && popup.$toast(message);
- })
- .addListener(TriggerListener.speed,(duration)=>{
- if (this.countdown !== duration) {
- this.countdown = duration;
- }
- })
- .addListener(TriggerListener.soundStatus,(status:boolean)=>{
- this.setSoundStatus(status ? SoundStatus.sound : SoundStatus.end);
- })
- .addListener(TriggerListener.success,(data)=>{
- if (this.assets) return;
- // 设置资源
- this.setAssets(data.blob);
- this.setAudioStatus(AudioStatus.loading);
- // 设置路径
- file.createdPath(data.blob).then((data)=>{
- return this.switchSrc(data);
- }).catch((error)=>{
- return this.setAudioStatus(AudioStatus.noURL);
- });
- })
- }).catch((fail)=>{
- if (typeof fail === 'object') {
- this.setJurisdictionStatus(fail);
- }
- });
- },
- installUseSoundRecording(){
- // 创建录音对象
- this.soundRecording = new SoundRecording({
- min: this.min,
- max: this.max
- });
- this.installSoundRecording();
- },
- // 设置资源
- setAssets(blob){
- if (blob) {
- this.stroageBlob = blob;
- this.assets = true;
- this.stroageUpload = null;
- if (!this.upload) {
- this.$emit('update:value',blob);
- }
- }
- },
- // 清楚资源
- clearAssets(){
- if (this.assets) {
- this.assets = false;
- this.stroageBlob = null;
- this.stroageUpload = null;
- if (!this.upload) {
- this.$emit('update:value',undefined);
- }
- }
- }
- },
- created(){
- !this.src && this.installUseSoundRecording();
- },
- beforeUnmount(){
- this.soundRecording && this.soundRecording.destroy();
- this.soundRecording = null;
- }
- }
|