是否可以在iOS中使用Codename One录制音频? [英] Is it possible to record audio in iOS with Codename One?

查看:156
本文介绍了是否可以在iOS中使用Codename One录制音频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序具有一个用于录制音频的按钮(另一个用于在录制结束时播放音频的按钮).我将录制的音频文件发送到服务器上.在Android上,文件记录为.amr(MIME类型音频/amr),可以播放.

My app features a button to record audio (and another to play it back when the recording is over). I send the recorded audio files on a server. On Android the files is recorded as .amr (mime type audio/amr) and can be played back.

但是在iOS上,该文件无法在设备(iPhone 4或4S)或计算机上播放. ffmpeg -i报告

On iOS however the file can neither be played back on the device (iPhone 4 or 4S) nor on a computer. ffmpeg -i reports

[mov,mp4,m4a,3gp,3g2,mj2 @ 0x2fac120] moov atom not found
9gMjOnnmsj9JJZR3.m4a: Invalid data found when processing input

请注意,VLC也无法播放.

我给m4a扩展名是因为录音机使用了它(以及aac编解码器).

I give the m4a extension because Voice recorder uses it (along with aac codec).

这是我使用的代码(主要基于

Here is the code I use (mostly based on https://github.com/codenameone/CodenameOne/blob/master/Ports/iOSPort/src/com/codename1/impl/ios/IOSImplementation.java#L2768-L2794 ) :

audioMemoPath = ParametresGeneraux.getWRITABLE_DIR() + "AudioMemo-"
            + System.currentTimeMillis() + 
            (Display.getInstance().getPlatformName().equals("and")
            ? ".amr"
            : ".m4a");
audioMemoMimeType = MediaManager.getAvailableRecordingMimeTypes()[0];

audioMemoRecorder = MediaManager.createMediaRecorder(audioMemoPath, audioMemoMimeType);

// If the permission audio has not been granted 
// the audiomemorecoreder will be null
     if (audioMemoRecorder != null) {
           audioMemoRecorder.play();
           boolean b = Dialog.show("Recording", "", "Save", "Cancel");
           audioMemoRecorder.pause();
           audioMemoRecorder.cleanup();

            ...
        }

此外,如果我在iOS上显示可用的mime类型,它会产生音频/amr",根据我能读到的所有告诉您在iOS上不支持amr的帖子,我对此表示怀疑.从源头看,默认情况下,amr是默认的mime类型,因为它总是返回:

Moreover if I display the available mime types on iOS, it yields "audio/amr" which I doubt according to all the posts I could read that tell you amr is not supported on iOS. Looking at the source it appears amr is the by default mime type because it is always returned :

/**
 * Gets the available recording MimeTypes
 */ 
public String [] getAvailableRecordingMimeTypes(){
    return new String[]{"audio/amr"};
}

所以我的问题是:是否可以在iOS上录制音频,如果可以,该怎么办?

So my question : is it possible to record audio on iOS, and if it is, how can it be done ?

任何帮助表示赞赏,

推荐答案

好吧,我通过重载MediaManager的某些方法(即getAvailableRecordingMimeTypes()createMediaRecorder())来防止其使用其getAvailableRecordingMimeTypes来使其正常工作方法.

Ok I got it working by overloading some methods of the MediaManager, namely getAvailableRecordingMimeTypes() and also createMediaRecorder() to prevent it from using its getAvailableRecordingMimeTypes method.

这是getAvailableRecordingMimeTypes()的代码:

Here is the code for getAvailableRecordingMimeTypes():

    /**
 * Normally the method returns amr even for ios. So we overload the original
 * method to return aac on ios.
 * @return 
 */
public static String[] getAvailableRecordingMimeTypes() {
    if (Display.getInstance().getPlatformName().equals("ios")) {
        return new String[]{"audio/aac"};
    } else {
        return new String[]{"audio/amr"};
    }

}

createMediaRecorder()保持不变(复制而未更改).

createMediaRecorder() is left as is (copied without changes).

现在可以在iOS中录制音频并在iOS和Android中播放!

Now it is possible to record audio in iOS and play it back in both iOS and Android!

这篇关于是否可以在iOS中使用Codename One录制音频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆