此代码获取音频文件并将其移至app目录但由于某些奇怪的原因我无法播放该文件或使用代码移动它可能有人请求帮助。
def insertAudio = new AudioData()
def filer = params.audioBroadcast
if (filer.length() == 0){
insertAudio.urlMediaCampaign = null
}else{
def mvFile = new File (params.audioBroadcast)
mvFile.text = 'simple content'
mvFile.renameTo( 'musica.wav')
def newFile = new File( 'musica.wav')
assert newFile.exists()
assert 'simple content' == newFile.text
insertAudio.urlMediaCampaign = newFile << newFile
}
答案 0 :(得分:1)
可能是您上传文件的问题。
这是我用来上传文件的代码。
List fileList = request.getFiles('Myfile') // 'files' is the name of the input
fileList.each { file ->
def randomName = "randomName"
def filename = file.getOriginalFilename()
def matcher1 = (filename =~ /.*\.(.*)$/)
def extension1
if(matcher1.matches()) {
extension1 = matcher1[0][1]
if(extension1 in ['wav','mp3', 'WAV','MP3','ogg','OGG']) {
def userDir = new File("C://app/myfiles")
if (!userDir.exists()) {
userDir.mkdirs()
}
filename = randomName+"."+extension1
File fileDest = new File(userDir,filename)
file.transferTo(fileDest)
} else {
println 'not ok = '+extension1
}
} else {
println 'No file extension found'
}
我正在使用List
,因此我可以上传多个文件,或者您可以使用。
def singleFile= request.getFile('Myfile')
上传音频文件后,我正在使用此代码播放音频文件。
<audio id="myAudio">
<source src="/../assets/${myaudiofilename}" type="audio/ogg">
<source src="/../assets/${myaudiofilename}" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
<script>
var myaudio = document.getElementById("myAudio");
myaudio.play(); //playaudio
</script>