是否可以使用Codename One在iOS中录制音频?

时间:2017-04-03 19:58:55

标签: codenameone audio-recording

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

但是在iOS上,文件既不能在设备上播放(iPhone 4或4S),也不能在电脑上播放。 ffmpeg -i报告

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

请注意,VLC也无法播放。

我提供m4a扩展名,因为录音机使用它(连同aac编解码器)。

以下是我使用的代码(主要基于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类型,它会产生“audio / amr”,我怀疑根据我能阅读的所有帖子告诉你amr在iOS上不受支持。看看源它出现amr是默认的mime类型,因为它总是返回:

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

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

任何帮助表示赞赏,

2 个答案:

答案 0 :(得分:2)

你看过Capture类了吗?这似乎更直截了当。

https://www.codenameone.com/javadoc/index.html

答案 1 :(得分:2)

好的,我通过重载MediaManager的一些方法,即getAvailableRecordingMimeTypes()createMediaRecorder()来阻止它使用getAvailableRecordingMimeTypes方法。

以下是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()保持原样(不经更改复制)。

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