当前位置:高等教育资讯网  >  中国高校课件下载中心  >  大学文库  >  浏览文档

中国科学技术大学:《多媒体技术基础》课程教学资源(实验参考内容)实验十六 Android 下的音频编程

资源类别:文库,文档格式:PDF,文档页数:4,文件大小:404.39KB,团购合买
点击下载完整版文档(PDF)

中国科学技术大学电子工程与信息科学系©2018 实验十六Android下的音频编程 实验目的 (1)了解RIFF文件格式。 (2)了解Android对媒体回放、录制的支持库。 (3)学习Android下进行声音播放的基本方法。 (4)学习Android下进行声音录制的基本方法。 二、 实验原理 1、Android APIs对多媒体的支持 android.media包下面包含了Android开发中媒体类,可以支持各类媒体的回 放、录制。部分和声音、图像、视频有关的类定义如表1所示。 得益于在android.nedia中定义的丰富函数,Android下可以采用多种方式来 进行声音的播放或录制[1]。例如,(1)MediaPlayer支持AAC、AMR、FLAC、 MP3、MIDI、OGG、PCM等格式,MediaPlayer可以通过设置元数据和播放源来 音频。(2)SoundPool支持多个音频文件同时播放(组合音频也是有上限的),延 时短,比较适合短促、密集的场景。(3)AudioTrack属于更偏底层的音频播放。 可用于单个音频播放和管理,相比于MediaPlayer具有:精炼、高效的优点。也 可以用于播放PCM(PCM无压缩的音频格式)音乐流的回放;如果要播需放其它 格式音频,需要相应的解码器。 在Android中录音可以用MediaRecord录音。AudioRecorder录音声音数据 的编码格式为PCM格式,但PCM语音数据,直接保存成音频文件,是不能够 被播放器播放的,所以需要实现PCM语音数据转为WAV文件。 表1 android.media中与媒体控制有关的部分类[2] Plays a series of audio URIs,but does all the hard AsyncPlayer work on another thread so that any slowness with preparing or loading doesn't block the calling thread. The AudioFormat class is used to access a number of AudioFormat audio format and channel configuration constants. AudioManager AudioManager provides access to volume and ringer mode control. 如果您在阅读过程中发现疏漏和错误,请联系《多媒体技术基础》课程组{network,cxh}@ustc.edu.cn

中国科学技术大学电子工程与信息科学系© 2018 如果您在阅读过程中发现疏漏和错误,请联系《多媒体技术基础》课程组{network,cxh}@ustc.edu.cn 实验十六 Android 下的音频编程 一、 实验目的 (1) 了解 RIFF 文件格式。 (2) 了解 Android 对媒体回放、录制的支持库。 (3) 学习 Android 下进行声音播放的基本方法。 (4) 学习 Android 下进行声音录制的基本方法。 二、 实验原理 1、Android APIs 对多媒体的支持 android.media 包下面包含了 Android 开发中媒体类,可以支持各类媒体的回 放、录制。部分和声音、图像、视频有关的类定义如表 1 所示。 得益于在 android.media 中定义的丰富函数,Android 下可以采用多种方式来 进行声音的播放或录制[1]。例如,(1)MediaPlayer 支持 AAC、AMR、FLAC、 MP3、MIDI、OGG、PCM 等格式,MediaPlayer 可以通过设置元数据和播放源来 音频。(2)SoundPool 支持多个音频文件同时播放(组合音频也是有上限的),延 时短,比较适合短促、密集的场景。(3)AudioTrack 属于更偏底层的音频播放。 可用于单个音频播放和管理,相比于 MediaPlayer 具有:精炼、高效的优点。也 可以用于播放 PCM(PCM 无压缩的音频格式)音乐流的回放;如果要播需放其它 格式音频,需要相应的解码器。 在 Android 中录音可以用 MediaRecord 录音。AudioRecorder 录音声音数据 的编码格式为 PCM 格式,但 PCM 语音数据,直接保存成音频文件,是不能够 被播放器播放的,所以需要实现 PCM 语音数据转为 WAV 文件。 表 1 android.media 中与媒体控制有关的部分类[2] AsyncPlayer Plays a series of audio URIs, but does all the hard work on another thread so that any slowness with preparing or loading doesn't block the calling thread. AudioFormat The AudioFormat class is used to access a number of audio format and channel configuration constants. AudioManager AudioManager provides access to volume and ringer mode control

中国科学技术大学电子工程与信息科学系©2018 The AudioRecord class manages the audio resources AudioRecord for Java applications to record audio from the audio input hardware of the platform. Structure that groups a position in frame units AudioTimestamp relative to an assumed audio stream,together with the estimated time when that frame was presented or is committed to be presented. AudioTrack The AudioTrack class manages and plays a single audio resource for Java applications FaceDetector Identifies the faces of people in a Bitmap graphic object. FaceDetector.Face A Face contains all the information identifying the location of a face in a bitmap. A single complete image buffer to use with a media Image source such as a MediaCodec. The ImageReader class allows direct application access to image data rendered into a Surface ImageReader Several Android media API classes accept Surface objects as targets to render to,including MediaPlayer,MediaCodec,and RenderScript Allocations. MediaCodec MediaCodec class can be used to access low-level media codec,i.e. MediaCrypto MediaCrypto class can be used in conjunction with MediaCodec to decode encrypted media data. MediaFormat Encapsulates the information describing the format of media data,be it audio or video. MediaMuxer MediaMuxer facilitates muxing elementary streams. MediaRecorder Used to record audio and video. MediaRecorder.AudioEncoder Defines the audio encoding. MediaRecorder.AudioSource Defines the audio source. MediaRecorder.OutputFormat Defines the output format. MediaRecorder.VideoEncoder Defines the video encoding MediaRecorder.VideoSource Defines the video source. MediaRouter allows applications to control the MediaRouter routing of media channels and streams from the current device to external speakers and destination devices. Ringtone provides a quick method for playing a Ringtone ringtone,notification,or other similar types of sounds. SoundPool The SoundPool class manages and plays audio resources for applications. 2、WAVE文件格式 WAVE(*.WAV文件)是Microsoft开发的一种音频文件格式,它符合RIFF文 件格式标准,RIFF全称为资源互换文件格式(Resources Interchange File Format), 是Windows下大部分多媒体文件遵循的一种文件结构。 根据RIFF规范,其基本的组成单元是chunk。[3]一个WAV文件通常有三 个chunk以及一个可选chunk,其在文件中的排列方式依次是:RIFF chunk,Format 如果您在阅读过程中发现疏漏和错误,请联系《多媒体技术基础》课程组{network,cxh}@ustc.edu.cn

中国科学技术大学电子工程与信息科学系© 2018 如果您在阅读过程中发现疏漏和错误,请联系《多媒体技术基础》课程组{network,cxh}@ustc.edu.cn AudioRecord The AudioRecord class manages the audio resources for Java applications to record audio from the audio input hardware of the platform. AudioTimestamp Structure that groups a position in frame units relative to an assumed audio stream, together with the estimated time when that frame was presented or is committed to be presented. AudioTrack The AudioTrack class manages and plays a single audio resource for Java applications. FaceDetector Identifies the faces of people in a Bitmap graphic object. FaceDetector.Face A Face contains all the information identifying the location of a face in a bitmap. Image A single complete image buffer to use with a media source such as a MediaCodec. ImageReader The ImageReader class allows direct application access to image data rendered into a Surface Several Android media API classes accept Surface objects as targets to render to, including MediaPlayer, MediaCodec, and RenderScript Allocations. MediaCodec MediaCodec class can be used to access low-level media codec, i.e. MediaCrypto MediaCrypto class can be used in conjunction with MediaCodec to decode encrypted media data. MediaFormat Encapsulates the information describing the format of media data, be it audio or video. MediaMuxer MediaMuxer facilitates muxing elementary streams. MediaRecorder Used to record audio and video. MediaRecorder.AudioEncoder Defines the audio encoding. MediaRecorder.AudioSource Defines the audio source. MediaRecorder.OutputFormat Defines the output format. MediaRecorder.VideoEncoder Defines the video encoding. MediaRecorder.VideoSource Defines the video source. MediaRouter MediaRouter allows applications to control the routing of media channels and streams from the current device to external speakers and destination devices. Ringtone Ringtone provides a quick method for playing a ringtone, notification, or other similar types of sounds. SoundPool The SoundPool class manages and plays audio resources for applications. 2、WAVE 文件格式 WAVE(*.WAV 文件)是 Microsoft 开发的一种音频文件格式,它符合 RIFF 文 件格式标准。RIFF全称为资源互换文件格式(Resources Interchange File Format), 是 Windows 下大部分多媒体文件遵循的一种文件结构。 根据 RIFF 规范,其基本的组成单元是 chunk。[3]一个 WAV 文件通常有三 个 chunk 以及一个可选 chunk,其在文件中的排列方式依次是:RIFF chunk,Format

中国科学技术大学电子工程与信息科学系©2018 chunk,Fact chunk(附加块,可选),Data chunk。如图I所示。 File offset field name Field Size (bytes) (bytes) 0 ChunkID The "RIFF"chunk descriptor ChunkSize 4 The Format of concern here is Format "WAVE",which requires two 12 sub-chunks:"fmt and "data" Subchunk1 ID 16 Subchunk1 Size 20 AudioFormat c2 22 The "fmt"sub-chunk Num Channels 2 24 SampleRate describes the format of 28 the sound information in ByteRate 4 the data sub-chunk 32 BlockAlign 34 BitsPerSample 2 36 Subchunk2ID 40 The"data”sub-chunk Subchunk2Size 44 Indicates the size of the sound information and data contains the raw sound data 图1*WAV文件的RIFF格式示意[4] 3、android.media声音播放的几个类 http://www.android-doc.com/reference/android/media/MediaPlayer.html http://www.android-doc.com/reference/android/media/SoundPool.html http://www.android-doc.com/reference/android/media/AudioTrack.html 4、android..meida录制声音的类 http://www.android-doc.com/reference/android/media/AudioRecord.html 三、 示例说明 函数名称 函数功能 void play_with_media_palyer(String str_file_name) 使用MediaPlayer播放本地音乐文件 如果您在阅读过程中发现疏漏和错误,请联系《多媒体技术基础》课程组{network,.cxh}@ustc.edu.cn

中国科学技术大学电子工程与信息科学系© 2018 如果您在阅读过程中发现疏漏和错误,请联系《多媒体技术基础》课程组{network,cxh}@ustc.edu.cn chunk,Fact chunk(附加块,可选),Data chunk。如图 1 所示。 图 1 *.WAV 文件的 RIFF 格式示意[4] 3、android.media 声音播放的几个类 http://www.android-doc.com/reference/android/media/MediaPlayer.html http://www.android-doc.com/reference/android/media/SoundPool.html http://www.android-doc.com/reference/android/media/AudioTrack.html 4、android.meida 录制声音的类 http://www.android-doc.com/reference/android/media/AudioRecord.html 三、 示例说明 函数名称 函数功能 void play_with_media_palyer(String str_file_name) 使用 MediaPlayer 播放本地音乐文件

中国科学技术大学电子工程与信息科学系©2018 void play_with sound pool(String str file_name) 使用SoundPool播放本地音乐文件 void play_with_audio_track(String str_file_name) 使用AudioTrack播放本地音乐文件 boolean makePCMFileToWAVFile(String pcmPath,String 将PCM样本文件添加WAV文件的文件头 destinationPath,boolean deletePcmFile) void audio record 5s(String str file name) 使用AudioRecord进行录音的简单示例 void start_stop_audio_record( 使用多线程进行录音控制的的简单示例 四、 实验要求 (1)根据实验原理部分所列出的信息做进一步的文献调研:详细学习WAVE文 件格式。找一个WAV文件(如C.Windows\media\Ring0l.wav),采取合 适的方法分析其声道数目、采样频率、样本精度。结果写入实验报告。 (2)编译链接所给AudioSample示例,阅读声音播放的三个函数,做必要的修 改,播放手机特定目录下的声音文件。请将关键代码写入实验报告。 (3)阅读声音录制的2个函数,做必要的修改,自己进行声音的录制。示例代 码给出了多线程实现的一种方式。请进行进一步的文献调研,学习多线程 代码有哪些不同的实现方式。相关结论请记录到实验报告中。 五、思考题 (1)单声道的声音保存为*.WAV文件和多声道的声音保存成*.WAV文件后应 该在文件的哪个部分标识声道有关的信息? (2)请通过实验验证你所用的手机(或其他Android设备)在同一个APP中 能否同时有多个声音播放软件同时播放不同的声音文件? (3)请通过实验验证你所用的手机(或其他Android设备)能否在同一个APP 中同时进行声音的播放和录制? 参考文献 [1] CNBLOGS.【Android】播放音频的几种方式介绍[Online].Available: https://www.cnblogs.com/HDK2016/p/8043247.html [2] AndroidAPIs.android.media [Online].Available:http://www.android- doc.com/reference/android/media/package-summary.html [3] CNBLOGS.RIFF和WAVE音频文件格式[Online].Available: https://www.cnblogs.com/wangguchangqing/p/5957531.html [4] Unknown. WAVE PCM soundfile format [Online]. Available: http://soundfile.sapp.org/doc/WaveFormat/ 如果您在阅读过程中发现疏漏和错误,请联系《多媒体技术基础》课程组{network,cxh}@ustc.edu.cn

中国科学技术大学电子工程与信息科学系© 2018 如果您在阅读过程中发现疏漏和错误,请联系《多媒体技术基础》课程组{network,cxh}@ustc.edu.cn void play_with_sound_pool(String str_file_name) 使用 SoundPool 播放本地音乐文件 void play_with_audio_track(String str_file_name) 使用 AudioTrack 播放本地音乐文件 boolean makePCMFileToWAVFile(String pcmPath, String destinationPath, boolean deletePcmFile) 将 PCM 样本文件添加 WAV 文件的文件头 void audio_record_5s(String str_file_name) 使用 AudioRecord 进行录音的简单示例 void start_stop_audio_record() 使用多线程进行录音控制的的简单示例 四、 实验要求 (1) 根据实验原理部分所列出的信息做进一步的文献调研;详细学习 WAVE 文 件格式。找一个 WAV 文件(如 C:\Windows\media\Ring01.wav),采取合 适的方法分析其声道数目、采样频率、样本精度。结果写入实验报告。 (2) 编译链接所给 AudioSample 示例,阅读声音播放的三个函数,做必要的修 改,播放手机特定目录下的声音文件。请将关键代码写入实验报告。 (3) 阅读声音录制的 2 个函数,做必要的修改,自己进行声音的录制。示例代 码给出了多线程实现的一种方式。请进行进一步的文献调研,学习多线程 代码有哪些不同的实现方式。相关结论请记录到实验报告中。 五、 思考题 (1) 单声道的声音保存为*.WAV 文件和多声道的声音保存成*.WAV 文件后应 该在文件的哪个部分标识声道有关的信息? (2) 请通过实验验证你所用的手机(或其他 Android 设备)在同一个 APP 中 能否同时有多个声音播放软件同时播放不同的声音文件? (3) 请通过实验验证你所用的手机(或其他 Android 设备)能否在同一个 APP 中同时进行声音的播放和录制? 参考文献 [1] CNBLOGS. 【Android】播放音频的几种方式介绍 [Online]. Available: https://www.cnblogs.com/HDK2016/p/8043247.html [2] AndroidAPIs. android.media [Online]. Available: http://www.android￾doc.com/reference/android/media/package-summary.html [3] CNBLOGS. RIFF 和 WAVE 音频文件格式 [Online]. Available: https://www.cnblogs.com/wangguchangqing/p/5957531.html [4] Unknown. WAVE PCM soundfile format [Online]. Available: http://soundfile.sapp.org/doc/WaveFormat/

点击下载完整版文档(PDF)VIP每日下载上限内不扣除下载券和下载次数;
按次数下载不扣除下载券;
24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
已到末页,全文结束
相关文档

关于我们|帮助中心|下载说明|相关软件|意见反馈|联系我们

Copyright © 2008-现在 cucdc.com 高等教育资讯网 版权所有