Android平台对一些常见的媒体格式内置了编解码器。并且应用可以非常简单地访问平台的多媒体功能――you do so using the same intents and activities mechanism that the rest of Android uses.
Android可以让你播放多种音频和视频类型的数据源。你可以播放存放在应用程序raw资源中的多媒体文件,或是放在文件系统中的多媒体文件,或是从网络上读取的数据流。使用MediaPlayer类来播放音频和视频媒体。
在被硬件支持的情况下,Android也可以让你录制音频和视频。录制需要使用MediaRecorder类。注意在模拟器上无法录制。
对于Android提供内置支持的媒体格式,请参阅Android的媒体格式附录。
1. 播放音频和视频
多媒体数据可以从任何地方被播放:播放raw资源中的多媒体资源,也可以播放文件系统中的多媒体文件,还可以通过指定URL播放存放在网络中的多媒体数据。
您可以只向标准输出设备播放音频数据;目前;目前是指移动设备扬声器或蓝牙耳机。你不能在通话中播放音频声音文件。You cannot play sound files in the conversation audio.
1.1 播放raw资源中的多媒体文件
最常见的情况可能是在你自己的应用中播放媒体文件(特别是音频),这样做非常容易:
1.1.1 将多媒体资源文件放到项目的res/raw目录中,然后eclipse plugin(或aapt)会自动在R类中生成一个代表这个媒体资源文件的索引值。
1.1.2 将代表那个媒体资源文件的索引值传入MediaPlayer.create(Context context, int resid)方法中,得到一个MediaPlayer的实例,然后调用这个MediaPlayer实例的start()方法:
MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_file_1); mp.start();
调用MediaPlayer.stop()方法来停止播放。若你希望隔一会载重新播放那个多媒体资源,那么你必须保证在调用MediaPlayer.start()方法之前,调用MediaPlayer.reset()方法和MediaPlayer.Prepare()方法。(MediaPlayer.create()方法内部会调用MediaPlayer.Prepare()方法)。
要暂停播放,调用MediaPlayer.pause()方法,再次调用MediaPlayer.start()方法会从暂停的位置恢复播放。
1.2 播放文件或者流
你也可以播放文件系统中的媒体文件或是一个网络URL代表的媒体流:
1.2.1 利用new操作符创建一个MediaPlayer的实例。
1.2.2 调用MediaPlayer.setDataSource()方法,并传入媒体文件的路径(本地文件系统或者是网络URL)。
1.2.3 调用MediaPlayer.prepare(),然后调用MediaPlayer.start()方法开始播放:
MediaPlayer mp = new MediaPlayer(); mp.setDataSource(PATH_TO_FILE); // OR web URL mp.prepare(); mp.start();
MediaPlayer.stop()和MediaPlayer.pause()方法的功能同前。
注:mp可能为null,所以需要在new操作符后检查mp是否为null。并且在调用MediaPlayer.setDataSource()方法的时候,可能会因为指定的文件不存在而抛出IllegalArgumentException和IOException异常。
注:如果调用MediaPlayer.setDataSource()方法指定一个网络URL,那么由这个网络URL表示的媒体文件必须支持分段下载。
1.3 播放JET内容
Android平台包含了一个JET引擎,这个引擎可以让你在应用中添加JET音频内容的交互式播放。你可以使用附带在SDK中的JetCreator创作程序来创建用于交互播放的JET内容。在程序中播放和管理JET内容需要使用JetPlayer类。
关于JET的概念和使用JetCreator工具的教程,可以查看JetCreator的用户手册。在OS X和Windows系统中此工具的所有功能都是可用的。在Linux中,支持此工具所有的创建功能,但是不支持对导入的资源进行试听。
下边是针对一个存放在SD卡上的.jet文件建立JET播放的示例代码:
JetPlayer myJet = JetPlayer.getJetPlayer(); myJet.loadJetFile("/sdcard/level1.jet"); byte segmentId = 0; // queue segment 5, repeat once, use General MIDI, transpose by -1 octave myJet.queueJetSegment(5, -1, 1, -1, 0, segmentId++); // queue segment 2 myJet.queueJetSegment(2, -1, 0, 0, 0, segmentId++); myJet.play();
该SDK包括一个示例应用程序――JetBoy――这个示例显示了如何在游戏中使用JetPlayer创建交互式的游戏配乐音乐。它还说明了如何使用Jet事件来同步音乐和游戏逻辑。该应用程序位于 <sdk>/ platforms/android-1.5/samples/JetBoy.
2. 音频采集
在设备上进行音频采集比音频/视频的播放要稍微复杂一点,但仍然相当简单:
1)使用new操作符创建一个android.media.MediaRecorder新实例
2)创建一个android.content.ContentValues实例,并且向里面装入标准属性,比如TITLE(标题),TIMESTAMP(时间戳),以及所有重要的MIME_TYPE.
3)创建一个录制文件将要保存的路径(你可以使用android.content.ContentResolver在数据库中创建一个条目,and get it to assign a path automatically which you can then use)
4)调用MediaRecorder.setAudioSource()方法来指定音频源。你可能想使用MediaRecorder.AudioSource.MIC.
5)使用MediaRecorder.setOutputFormat()方法设定输出文件的格式。
6)使用MediaRecorder.setAudioEncoder()方法设置音频编码器。
7)调用MediaRecorder.prepare()方法。
8)调用MediaRecorder.start()方法开始进行音频采集。
9)调用MediaRecorder.stop()方法停止音频采集。
10)当不使用这个MediaRecorder实例的时候,调用MediaRecorder.release()方法。
2.1 示例代码:音频采集
recorder = new MediaRecorder(); ContentValues values = new ContentValues(3); values.put(MediaStore.MediaColumns.TITLE, SOME_NAME_HERE); values.put(MediaStore.MediaColumns.TIMESTAMP, System.currentTimeMillis()); values.put(MediaStore.MediaColumns.MIME_TYPE, recorder.getMimeContentType()); ContentResolver contentResolver = new ContentResolver(); Uri base = MediaStore.Audio.INTERNAL_CONTENT_URI; Uri newUri = contentResolver.insert(base, values); if (newUri == null) { // need to handle exception here - we were not able to create a new // content entry } String path = contentResolver.getDataFilePath(newUri); // could use setPreviewDisplay() to display a preview to suitable View here recorder.setAudioSource(MediaRecorder.AudioSource.MIC); recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); recorder.setOutputFile(path); recorder.prepare(); recorder.start();
2.2 停止录制
基于上边的音频采集的示例代码,以下代码是示例如何停止音频采集:
recorder.stop(); recorder.release();
分享到:
相关推荐
The Real-time Transport Protocol (RTP) provides a framework for delivery of audio and video across IP networks and unprecedented quality and reliability.
RTP Audio and Video for the Internet
RTP: Audio and Video for the Internet By Colin Perkins Publisher : Addison Wesley Pub Date : June 12, 2003 ISBN : 0-672-32249-8 Pages : 432 The Real-time Transport Protocol (RTP) provides ...
This document species an Internet standards track protocol for the Internet community, and requests discussion and suggestions for improvements. Please refer to the current edition of the \Internet ...
[RTP Audio And Video For The Internet]的PDF版,自己做的。
Multimedia handling with a fast audio and video encoder Multimedia handling with a fast audio and video encode Multimedia handling with a fast audio and video encode Multimedia handling with a fast ...
Sample Code for Using the Windows Media Audio and Video Codec Interfaces
GDLiveStreaming, Live audio and video manipulation pipeline
IEEE Std 1857.3-2013 Systems of Advanced Audio and Video Coding
IEEE Std 1857a-2014 Advanced Audio and Video Coding Amendment 1:
IEEE Std 1857-2013 Advanced Audio and Video Coding - 完整英文电子版(178
基于 web 端的音视频编辑器。(A web-based audio and video editor.).zip
IEEE Std 1857 Series Audio and Video Coding - 包含全部11份最新英文版标准文件.z
IEEE Std 1857 Series Audio and Video Coding - 包含全部11份最新英文版标准文件.7z
4. Resizing and Scaling Video 64 5. Cropping Video 69 6. Padding Video 73 7. Flipping and Rotating Video 77 8. Blur, Sharpen and Other Denoising 81 9. Overlay - Picture in Picture 87 10. Adding Text ...
If you embed audio or video in your website, you should use HTML5.
IP基础设施成为广播音频和视频技术中越来越重要的一部分。 如今,比特率上升,设备价格下降,IP传输技术的使用正在推动传统的通信和传输系统。 但是,要在广播中使用IP网络,特别是对于实时视频应用,新技术需要与...
Internet community, and requests discussion and suggestions for improvements. Please refer to the current edition of the "Internet Official Protocol Standards" (STD 1) for the standardization state...