在这里插入代码片
from jnius import autoclass
from kivy.app import App
from kivy.config import Config
from kivy.core.text import LabelBase
from kivy.logger import Logger
#提供对环境变量的访问。
Environment = autoclass('android.os.Environment')
MediaRecorder = autoclass('android.media.MediaRecorder')
AudioSource = autoclass('android.media.MediaRecorder$AudioSource')
OutputFormat = autoclass('android.media.MediaRecorder$OutputFormat')
AudioEncoder = autoclass('android.media.MediaRecorder$AudioEncoder')
MediaPlayer = autoclass('android.media.MediaPlayer')
File = autoclass('java.io.File')
#相机类
Camera=autoclass('android.hardware.camera2')
#Environment.getExternalStorageDirectory()
#获取用于放置特定类型文件的顶级共享/外部存储目录
#file.getAbsolutePath()
#获取绝对路径()
#返回对象的绝对路径。这里的绝对路径是指当前执行程序的目录路径和创建对象时传递的路径的组合。
#以下函数即安卓上/storage/emulated/0目录或者/storage/emulated/card(存储卡)
storage_path = (Environment.getExternalStorageDirectory()
.getAbsolutePath() + '/kivy_recording.3gp')
#用于录制音频和视频(媒体记录器)
recorder = MediaRecorder()
#MediaPlayer 类可用于控制音频/视频文件和流的播放。(媒体播放器)
player = MediaPlayer()
#java
#MediaRecorder recorder = new MediaRecorder();
#recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
# recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
# recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
# recorder.setOutputFile(PATH_NAME);
# recorder.prepare();
# recorder.start();
#recorder.stop();
#recorder.reset();
#recorder.release();
def init_recorder():
#设置声音源,来自MIC
recorder.setAudioSource(AudioSource.MIC)
#设置输出格式和更新(3GPP 媒体文件格式)AAC ADTS 文件格式AMR NB 文件格式AMR WB 文件格式MPEG_2_TS以 MPEG2/TS 封装的 H.264/AAC 数据MPEG_4 MPEG4 媒体文件格式Ogg 容器中的 Opus 数据WEBM 容器中的 VP8/VORBIS 数据
recorder.setOutputFormat(OutputFormat.THREE_GPP)
#设置声音编码
recorder.setAudioEncoder(AudioEncoder.AMR_NB)
#设置输出目录
recorder.setOutputFile(storage_path)
#准备
recorder.prepare()
#你可以通过返回 setAudioSource() 步骤重用该对象
def reset_player():
if (player.isPlaying()):
player.stop()
player.reset()
#重新开始播放
def restart_player():
reset_player()
try:
#设置播放数据源
player.setDataSource(storage_path)
player.prepare()
player.start()
except:
player.reset()
#kivy入口
class RecorderApp(App):
is_recording = False
def begin_end_recording(self):
if (self.is_recording):
recorder.stop()
recorder.reset()
self.is_recording = False
self.root.ids.begin_end_recording.text = \
('[font=Modern Pictograms][size=120]'
'e[/size][/font]\nBegin recording')
return
init_recorder()
recorder.start()
self.is_recording = True
self.root.ids.begin_end_recording.text = \
('[font=Modern Pictograms][size=120]'
'%[/size][/font]\nEnd recording')
def begin_playback(self):
restart_player()
def delete_file(self):
reset_player()
File(storage_path).delete()
if __name__ == '__main__':
Logger.info('App: storage path == "%s"' % storage_path)
Config.set('graphics', 'width', '600')
Config.set('graphics', 'height', '900')
#注册字体,全局可用
LabelBase.register(name='Modern Pictograms',
fn_regular='modernpics.ttf')
RecorderApp().run()
setAudioSource(int audio_source)
int:要使用的音频源如mic为麦克风
MediaRecorder.AudioSource.DEFAULT,
MediaRecorder.AudioSource.MIC,
MediaRecorder.AudioSource.VOICE_UPLINK,
MediaRecorder.AudioSource.VOICE_DOWNLINK,
MediaRecorder.AudioSource.VOICE_CALL,
MediaRecorder.AudioSource.CAMCORDER,
MediaRecorder.AudioSource.VOICE_RECOGNITION,
MediaRecorder.AudioSource.VOICE_COMMUNICATION,
MediaRecorder.AudioSource.UNPROCESSED, or
MediaRecorder.AudioSource.VOICE_PERFORMANCE
setVideoSource (int video_source)
int:要使用的视频源
#摄像头视频源,使用CameraAPI 作为视频源。
MediaRecorder.VideoSource.CAMERA
#默认视频源
MediaRecorder.VideoSource.DEFAULT
#使用 Surface 作为视频源。android.hardware.camera2从API 源 录制时必须使用此标志 。
使用此视频源类型时,用于MediaRecorder#getSurface() 检索 MediaRecorder 创建的表面。
MediaRecorder.VideoSource.SURFACE
setAudioEncoder(int audio_encode)
int:要使用的音频编码器。值为, , , , , , , 或
MediaRecorder.AudioEncoder.DEFAULT
MediaRecorder.AudioEncoder.AMR_NB
MediaRecorder.AudioEncoder.AMR_WB
MediaRecorder.AudioEncoder.AAC
MediaRecorder.AudioEncoder.HE_AAC
MediaRecorder.AudioEncoder.AAC_ELD
MediaRecorder.AudioEncoder.VORBIS
MediaRecorder.AudioEncoder.OPUS
setVideoEncoder(int video_encoder)
int:要使用的视频编码器。值为, , , , , 或
MediaRecorder.VideoEncoder.DEFAULT
MediaRecorder.VideoEncoder.H263
MediaRecorder.VideoEncoder.H264
MediaRecorder.VideoEncode.MPEG_4_SP
MediaRecorder.VideoEncoder.VP8
MediaRecorder.VideoEncoder.HEVC