mirror of
https://github.com/kevin-DL/vue-audio-recorder.git
synced 2026-01-23 07:41:26 +00:00
Add custom header & minor refactoring
This commit is contained in:
13
demo/app.vue
13
demo/app.vue
@@ -13,6 +13,7 @@
|
|||||||
upload-url="some url"
|
upload-url="some url"
|
||||||
:attempts="3"
|
:attempts="3"
|
||||||
:time="2"
|
:time="2"
|
||||||
|
:headers="headers"
|
||||||
:start-record="callback"
|
:start-record="callback"
|
||||||
:stop-record="callback"
|
:stop-record="callback"
|
||||||
:start-upload="callback"
|
:start-upload="callback"
|
||||||
@@ -24,19 +25,15 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import AudioPlayer from '../src/components/player'
|
|
||||||
import AudioRecorder from '../src/components/recorder'
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'app',
|
name: 'app',
|
||||||
components: {
|
|
||||||
AudioPlayer,
|
|
||||||
AudioRecorder
|
|
||||||
},
|
|
||||||
data () {
|
data () {
|
||||||
return {
|
return {
|
||||||
mp3: '/demo/example.mp3',
|
mp3: '/demo/example.mp3',
|
||||||
showRecorder: true
|
showRecorder: true,
|
||||||
|
headers: {
|
||||||
|
'X-Custom-Header': 'some data'
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
|
|||||||
@@ -2,8 +2,12 @@ import Vue from 'vue'
|
|||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
import app from './app'
|
import app from './app'
|
||||||
|
|
||||||
|
import AudioRecorder from '@/index'
|
||||||
|
|
||||||
Vue.prototype.$http = axios
|
Vue.prototype.$http = axios
|
||||||
|
|
||||||
|
Vue.use(AudioRecorder)
|
||||||
|
|
||||||
new Vue({
|
new Vue({
|
||||||
el: '#app',
|
el: '#app',
|
||||||
render: h => h(app)
|
render: h => h(app)
|
||||||
|
|||||||
@@ -24,7 +24,7 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import { calculateLineHeadPosition } from '@/lib/utils.js'
|
import { calculateLineHeadPosition } from '@/lib/utils'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
props: {
|
props: {
|
||||||
|
|||||||
@@ -97,11 +97,11 @@
|
|||||||
:class="{'ar-player__play--active': isPlaying}"
|
:class="{'ar-player__play--active': isPlaying}"
|
||||||
@click.native="decorator(playback)"/>
|
@click.native="decorator(playback)"/>
|
||||||
|
|
||||||
<icon-button
|
<uploader
|
||||||
id="upload"
|
id="upload"
|
||||||
class="ar-icon ar-icon__sm"
|
class="ar-icon ar-icon__sm"
|
||||||
name="save"
|
:record="record"
|
||||||
@click.native="decorator(upload)"/>
|
:options="uploaderOptions"/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="ar-player-bar">
|
<div class="ar-player-bar">
|
||||||
@@ -120,32 +120,31 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import IconButton from './icon-button'
|
import IconButton from './icon-button'
|
||||||
import LineControl from './line-control'
|
import LineControl from './line-control'
|
||||||
|
import Uploader from './uploader'
|
||||||
import VolumeControl from './volume-control'
|
import VolumeControl from './volume-control'
|
||||||
import { convertTimeMMSS } from '@/lib/utils.js'
|
import { convertTimeMMSS } from '@/lib/utils'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
props: {
|
props: {
|
||||||
src : { type: String },
|
src : { type: String },
|
||||||
uploadUrl : { type: String },
|
record : { type: Object },
|
||||||
record : { type: Object },
|
compact : { type: Boolean, default: true },
|
||||||
compact : { type: Boolean, default: true },
|
uploaderOptions : { type: Object, default: () => new Object }
|
||||||
startUpload : { type: Function },
|
|
||||||
successfulUpload : { type: Function },
|
|
||||||
failedUpload : { type: Function }
|
|
||||||
},
|
},
|
||||||
data () {
|
data () {
|
||||||
return {
|
return {
|
||||||
isPlaying: false,
|
isPlaying : false,
|
||||||
duration: convertTimeMMSS(0),
|
duration : convertTimeMMSS(0),
|
||||||
playedTime: convertTimeMMSS(0),
|
playedTime : convertTimeMMSS(0),
|
||||||
progress: 0
|
progress : 0
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
components: {
|
components: {
|
||||||
IconButton,
|
IconButton,
|
||||||
LineControl,
|
LineControl,
|
||||||
|
Uploader,
|
||||||
VolumeControl
|
VolumeControl
|
||||||
},
|
},
|
||||||
mounted: function() {
|
mounted: function() {
|
||||||
@@ -161,6 +160,10 @@
|
|||||||
})
|
})
|
||||||
|
|
||||||
this.player.addEventListener('timeupdate', this._onTimeUpdate)
|
this.player.addEventListener('timeupdate', this._onTimeUpdate)
|
||||||
|
|
||||||
|
this.$eventBus.$on('remove-record', () => {
|
||||||
|
this._resetProgress()
|
||||||
|
})
|
||||||
},
|
},
|
||||||
computed: {
|
computed: {
|
||||||
audioSource () {
|
audioSource () {
|
||||||
@@ -188,30 +191,6 @@
|
|||||||
|
|
||||||
this.isPlaying = !this.isPlaying
|
this.isPlaying = !this.isPlaying
|
||||||
},
|
},
|
||||||
upload () {
|
|
||||||
if (this.startUpload) {
|
|
||||||
this.startUpload()
|
|
||||||
}
|
|
||||||
|
|
||||||
this.$emit('on-start-upload')
|
|
||||||
|
|
||||||
let data = new FormData()
|
|
||||||
data.append('audio', this.record.blob, 'my-record')
|
|
||||||
|
|
||||||
this.$http.post(this.uploadUrl, data, {
|
|
||||||
headers: {'Content-Type': `multipart/form-data; boundary=${data._boundary}`}
|
|
||||||
}).then(resp => {
|
|
||||||
this.$emit('on-end-upload', 'success')
|
|
||||||
if (this.successfulUpload) {
|
|
||||||
this.successfulUpload(resp)
|
|
||||||
}
|
|
||||||
}).catch(error => {
|
|
||||||
this.$emit('on-end-upload', 'fail')
|
|
||||||
if (this.failedUpload) {
|
|
||||||
this.failedUpload(error)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
},
|
|
||||||
download () {
|
download () {
|
||||||
let link = document.createElement('a')
|
let link = document.createElement('a')
|
||||||
link.href = this.record.url
|
link.href = this.record.url
|
||||||
@@ -228,12 +207,11 @@
|
|||||||
if (this.isPlaying) {
|
if (this.isPlaying) {
|
||||||
this.player.pause()
|
this.player.pause()
|
||||||
}
|
}
|
||||||
setTimeout(() => {
|
|
||||||
this.duration = convertTimeMMSS(0)
|
this.duration = convertTimeMMSS(0)
|
||||||
this.playedTime = convertTimeMMSS(0)
|
this.playedTime = convertTimeMMSS(0)
|
||||||
this.progress = 0
|
this.progress = 0
|
||||||
this.isPlaying = false
|
this.isPlaying = false
|
||||||
}, 0)
|
|
||||||
},
|
},
|
||||||
_onTimeUpdate () {
|
_onTimeUpdate () {
|
||||||
this.playedTime = convertTimeMMSS(this.player.currentTime)
|
this.playedTime = convertTimeMMSS(this.player.currentTime)
|
||||||
|
|||||||
@@ -211,15 +211,7 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<audio-player
|
<audio-player :compact="compact" :record="selected" :uploader-options="uploaderOptions"/>
|
||||||
:compact="compact"
|
|
||||||
:record="selected"
|
|
||||||
:upload-url="uploadUrl"
|
|
||||||
:start-upload="startUpload"
|
|
||||||
:successful-upload="successfulUpload"
|
|
||||||
:failed-upload="failedUpload"
|
|
||||||
@start-upload="onStartUpload"
|
|
||||||
@end-upload="onEndUpload"/>
|
|
||||||
|
|
||||||
<div :class="uploadStatusClasses" v-if="uploadStatus">{{message}}</div>
|
<div :class="uploadStatusClasses" v-if="uploadStatus">{{message}}</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -227,52 +219,76 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import AudioPlayer from './player.vue'
|
import AudioPlayer from './player'
|
||||||
import IconButton from './icon-button.vue'
|
import IconButton from './icon-button'
|
||||||
import Recorder from '@/lib/recorder.js'
|
import Recorder from '@/lib/recorder'
|
||||||
import { convertTimeMMSS } from '@/lib/utils.js'
|
import { convertTimeMMSS } from '@/lib/utils'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
props: {
|
props: {
|
||||||
attempts : { type: Number },
|
attempts : { type: Number },
|
||||||
compact : { type: Boolean, default: false },
|
compact : { type: Boolean, default: false },
|
||||||
time : { type: Number },
|
time : { type: Number },
|
||||||
uploadUrl : { type: String },
|
|
||||||
|
attemptsLimit : { type: Function },
|
||||||
|
micFailed : { type: Function },
|
||||||
|
startRecord : { type: Function },
|
||||||
|
stopRecord : { type: Function },
|
||||||
|
|
||||||
attemptsLimit : { type: Function },
|
|
||||||
failedUpload : { type: Function },
|
failedUpload : { type: Function },
|
||||||
micFailed : { type: Function },
|
headers : { type: Object },
|
||||||
startRecord : { type: Function },
|
|
||||||
startUpload : { type: Function },
|
startUpload : { type: Function },
|
||||||
stopRecord : { type: Function },
|
|
||||||
successfulUpload : { type: Function },
|
successfulUpload : { type: Function },
|
||||||
|
uploadUrl : { type: String },
|
||||||
|
|
||||||
successfulUploadMsg : { type: String, default: 'Upload successful' },
|
successfulUploadMsg : { type: String, default: 'Upload successful' },
|
||||||
failedUploadMsg : { type: String, default: 'Upload fail' }
|
failedUploadMsg : { type: String, default: 'Upload fail' }
|
||||||
},
|
},
|
||||||
data () {
|
data () {
|
||||||
return {
|
return {
|
||||||
isUploading: false,
|
isUploading : false,
|
||||||
recorder: new Recorder({
|
recorder : new Recorder({
|
||||||
afterStop: () => {
|
afterStop: () => {
|
||||||
this.recordList = this.recorder.recordList()
|
this.recordList = this.recorder.recordList()
|
||||||
|
if (this.stopRecord) {
|
||||||
if (this.stopRecord) {
|
this.stopRecord('stop record')
|
||||||
this.stopRecord('stop record')
|
}
|
||||||
}
|
},
|
||||||
},
|
attempts: this.attempts,
|
||||||
attempts: this.attempts,
|
time: this.time
|
||||||
time: this.time
|
}),
|
||||||
}),
|
recordList : [],
|
||||||
recordList: [],
|
selected : {},
|
||||||
selected: {},
|
uploadStatus : null,
|
||||||
uploadStatus: null
|
uploaderOptions : {}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
components: {
|
components: {
|
||||||
AudioPlayer,
|
AudioPlayer,
|
||||||
IconButton
|
IconButton
|
||||||
},
|
},
|
||||||
|
created () {
|
||||||
|
this.uploaderOptions = {
|
||||||
|
failedUpload : this.failedUpload,
|
||||||
|
headers : this.headers,
|
||||||
|
startUpload : this.startUpload,
|
||||||
|
successfulUpload : this.successfulUpload,
|
||||||
|
uploadUrl : this.uploadUrl
|
||||||
|
}
|
||||||
|
|
||||||
|
this.$eventBus.$on('start-upload', () => {
|
||||||
|
this.isUploading = true
|
||||||
|
})
|
||||||
|
|
||||||
|
this.$eventBus.$on('end-upload', (resp) => {
|
||||||
|
this.isUploading = false
|
||||||
|
this.uploadStatus = status
|
||||||
|
setTimeout(() => {this.uploadStatus = null}, 1500)
|
||||||
|
})
|
||||||
|
},
|
||||||
|
beforeDestroy () {
|
||||||
|
this.stopRecorder()
|
||||||
|
},
|
||||||
methods: {
|
methods: {
|
||||||
toggleRecorder () {
|
toggleRecorder () {
|
||||||
if (this.attempts && this.recorder.records.length >= this.attempts) {
|
if (this.attempts && this.recorder.records.length >= this.attempts) {
|
||||||
@@ -301,14 +317,7 @@
|
|||||||
removeRecord (idx) {
|
removeRecord (idx) {
|
||||||
this.recordList.splice(idx, 1)
|
this.recordList.splice(idx, 1)
|
||||||
this.$set(this.selected, 'url', null)
|
this.$set(this.selected, 'url', null)
|
||||||
},
|
this.$eventBus.$emit('remove-record')
|
||||||
onStartUpload () {
|
|
||||||
this.isUploading = true
|
|
||||||
},
|
|
||||||
onEndUpload (status) {
|
|
||||||
this.isUploading = false
|
|
||||||
this.uploadStatus = status
|
|
||||||
setTimeout(() => {this.uploadStatus = null}, 1500)
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
computed: {
|
computed: {
|
||||||
|
|||||||
54
src/components/uploader.vue
Normal file
54
src/components/uploader.vue
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
<style lang="scss">
|
||||||
|
@import '../scss/icons';
|
||||||
|
</style>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<icon-button name="save" @click.native="upload"/>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
import IconButton from './icon-button'
|
||||||
|
|
||||||
|
export default {
|
||||||
|
props: {
|
||||||
|
options : { type: Object },
|
||||||
|
record : { type: Object }
|
||||||
|
},
|
||||||
|
components: {
|
||||||
|
IconButton
|
||||||
|
},
|
||||||
|
methods: {
|
||||||
|
upload () {
|
||||||
|
if (!this.record.url) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
this.$eventBus.$emit('start-upload')
|
||||||
|
|
||||||
|
if (this.options.startUpload) {
|
||||||
|
this.options.startUpload()
|
||||||
|
}
|
||||||
|
|
||||||
|
let data = new FormData()
|
||||||
|
data.append('audio', this.record.blob, 'my-record')
|
||||||
|
|
||||||
|
let headers = Object.assign(this.options.headers, {})
|
||||||
|
headers['Content-Type'] = `multipart/form-data; boundary=${data._boundary}`
|
||||||
|
|
||||||
|
this.$http.post(this.options.uploadUrl, data, { headers: headers }).then(resp => {
|
||||||
|
this.$eventBus.$emit('end-upload', 'success')
|
||||||
|
|
||||||
|
if (this.options.successfulUpload) {
|
||||||
|
this.options.successfulUpload(resp)
|
||||||
|
}
|
||||||
|
}).catch(error => {
|
||||||
|
this.$eventBus.$emit('end-upload', 'fail')
|
||||||
|
|
||||||
|
if (this.options.failedUpload) {
|
||||||
|
this.options.failedUpload(error)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
@@ -37,8 +37,8 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import IconButton from './icon-button.vue'
|
import IconButton from './icon-button'
|
||||||
import LineControl from './line-control.vue'
|
import LineControl from './line-control'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
data () {
|
data () {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import AudioPlayer from './components/player.vue'
|
import AudioPlayer from '@/components/player.vue'
|
||||||
import AudioRecorder from './components/recorder.vue'
|
import AudioRecorder from '@/components/recorder.vue'
|
||||||
|
|
||||||
const components = {
|
const components = {
|
||||||
AudioPlayer,
|
AudioPlayer,
|
||||||
@@ -12,6 +12,8 @@ const components = {
|
|||||||
|
|
||||||
this.installed = true
|
this.installed = true
|
||||||
|
|
||||||
|
Vue.prototype.$eventBus = Vue.prototype.$eventBus || new Vue
|
||||||
|
|
||||||
Vue.component('audio-player', AudioPlayer)
|
Vue.component('audio-player', AudioPlayer)
|
||||||
Vue.component('audio-recorder', AudioRecorder)
|
Vue.component('audio-recorder', AudioRecorder)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user