nativescript-audio icon indicating copy to clipboard operation
nativescript-audio copied to clipboard

record stop method error

Open yoking-shi opened this issue 6 years ago • 2 comments

this is the error: image

my code:

<template>
  <Page class="page">
    <ActionBar class="action-bar">
      <Label class="action-bar-title"
             text="Home"></Label>
    </ActionBar>
    <StackLayout backgroundColor="#3c495e">
      <Label>{{ recording ? '录音中' : '' }}</Label>

      <button @tap="start">开始</button>
      <button @tap="stop">关闭</button>
      <button @tap="play">播放</button>

    </StackLayout>
  </Page>
</template>

<script>
import * as Audio from 'nativescript-audio'
import { File, knownFolders } from 'tns-core-modules/file-system'
export default {
  data () {
    return {
      player: new Audio.TNSPlayer(),
      recorder: new Audio.TNSRecorder(),
      recording: false,
      audioPath: `${knownFolders.currentApp().getFolder('audio').path}/record.mp3`
    }
  },

  mounted() {
    this.recorder.debug = true
  },

  methods: {
    start () {
      console.log('aysnc start >>>>>>>>>>>>>>>>>> start')
      const options = {
        filename: this.audioPath,
        metering: true,
        infoCallback: infoObject => {
          console.log('record-infoCallBack', JSON.stringify(infoObject));
        },
        errorCallback: errorObject => {
          console.log('record-errorCallback', JSON.stringify(errorObject));
        }
      }

      this.recorder.start(options).catch(ex => {
        console.log(ex)
      })
      this.recording = true
      console.log('aysnc start >>>>>>>>>>>>>>>>>> end')
    },

    stop () {
      console.log('stop>>>>>>>>>>>>>>>>>>')
      console.log(this.audioPath)
      this.recorder.stop().catch(ex => {
        console.log(ex)
      })
      this.recording = false
    },

    play () {
      console.log('play>>>>>>>>>>>>>>>>>>')
      const options = {
        audioFile: this.audioPath,
        loop: false,
        completeCallback: () => {
          alert('Audio file complete.')
        },
        errorCallback: errorObject => {
          console.log(JSON.stringify(errorObject))
        },
        infoCallback: infoObject => {
          console.log(JSON.stringify(infoObject))
        }
      }

      this.player.playFromFile(options).catch(err => {
        console.log('error playFromFile', err)
      })
    }
  }
}
</script>

<style scoped lang="scss">
// Start custom common variables
@import "../app-variables";
// End custom common variables

// Custom styles
.fa {
  color: $accent-dark;
}

.info {
  font-size: 20;
}
</style>

How to set encode and format? Any examples of parameters? image thank you :)

yoking-shi avatar Nov 30 '18 09:11 yoking-shi

are you using vue.js? should it be something like this for your data and mounted?

export default {
  data: function() {
    return {
      player: new Audio.TNSPlayer(),
      recorder: new Audio.TNSRecorder(),
      recording: false,
      audioPath: `${knownFolders.currentApp().getFolder('audio').path}/record.mp3`
    }
  },

  mounted: function() {
    this.recorder.debug = true
  },
...
}

cmckni3 avatar Jan 04 '19 07:01 cmckni3

Hi @yoking-shi I have read (I don't remember where xD), you must set manually the value you want to use, you can see all value on this page: https://developer.android.com/reference/android/media/MediaRecorder.AudioEncoder for example 3 for AAC encoding: https://developer.android.com/reference/android/media/MediaRecorder.AudioEncoder.html#AAC

bye :+1:

progressify avatar Jan 05 '19 16:01 progressify