phonegap(cordova)在ios系统实现录音功能的几种特殊情况

phonegap的录音插件是Media,用它可以实现录音及播放录音的功能,但是在android与ios系统下的实现方式有些许不同,共有以下几点:

一、创建录音文件

android代码:

//实例化录音类,<span style="font-family: Arial, Helvetica, sans-serif;">window.appRootDir是自定义的文件夹路径</span>

        var mediaRec = new Media(window.appRootDir+"test.mp3",
            // 录音执行函数
            function() {
            },
            // 录音失败执行函数
            function(err) {
            }
        );

iso代码:

//window.appRootDir是自定义的文件夹路径
		window.appRootDir.getFile(
			"test.wav", 
			{create:true,exclusive:false},
			function(fileEntry){
            	//实例化录音类
		        var mediaRec = new Media("documents://"+window.appRootDir+"test.wav",
		            // 录音执行函数
		            function() {
		            },
		            // 录音失败执行函数
		            function(err) {
		            }
		        );
          	},function(){  
      		}
      	);

以上两段代码首先是创建的文件格式不同,android比较灵活,可以创建任何音频格式的文件,ios经测试MP3好像不支持,wav是可以的。其次就是android在实例化Media对象的时候可以顺便创建test.mp3文件,ios好像必须先创建好test.wav文件,才能实例化Media对象。还有就是new Media的第一个参数,ios系统必须加上"documents://"前缀。

二、解决android创建的录音ios不能播放

android必须创建“.amr”格式的文件,ios才能识别,也就是将以上代码的“.MP3”后缀改为“.amr”后缀,并且将“AudioPlayer.java”中的“startRecording”方法改为以下代码:

public void startRecording(String file) {
        switch (this.mode) {
        case PLAY:
            Log.d(LOG_TAG, "AudioPlayer Error: Can't record in play mode.");
            sendErrorStatus(MEDIA_ERR_ABORTED);
            break;
        case NONE:
            this.audioFile = file;
            this.recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            this.recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); // THREE_GPP);
            this.recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC); //AMR_NB);
            this.recorder.setOutputFile(this.tempFile);
            try {
                this.recorder.prepare();
                this.recorder.start();
                this.setState(STATE.MEDIA_RUNNING);
                return;
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            sendErrorStatus(MEDIA_ERR_ABORTED);
            break;
        case RECORD:
            Log.d(LOG_TAG, "AudioPlayer Error: Already recording.");
            sendErrorStatus(MEDIA_ERR_ABORTED);
        }
    }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

西山水壶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值