0

This code gets the audio file and moves it to the app directory but for some strange reason i cant play the file or move it with code could someone please help.

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
}
Burt Beckwith
  • 75,342
  • 5
  • 143
  • 156
Melany
  • 71
  • 5
  • can you tell us what is mean "strange reason i cant play the file or move it with code"? you want to play the audio file? –  Apr 29 '17 at 06:32
  • are you able to play the file if you add it manually? If it does, then the problem may lie in the way you upload the file. If it doesn´t, then the problem lies in the manner you serve your files. If your problem is how you serve your file, I´d suggest you make a serveFile method. – Omar Yafer May 02 '17 at 19:15
  • This answer might help you in your quest for a solution http://stackoverflow.com/questions/29310352/how-to-save-to-a-file-system-directory-in-grails/29312903#29312903. EDIT: Updated wrong link. – Omar Yafer May 02 '17 at 19:19
  • this code moves the file in ubuntu when i try to play the audio manually by going to the folder where the audio file is located the audio file, has a lock image over it, and when i try to play it show that the file has an error and it sais that canot determine the type of stream – Melany May 02 '17 at 20:51

1 Answers1

1

may be your problem is uploading file.

this is my code where i use to upload file.

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'
            }

I am using List, so i can upload more than a file, or you can use.

def singleFile= request.getFile('Myfile')

after upload an audio file, i am using this code to play the audio file.

<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>