Android平台为多种常见媒体类型提供了内建的编码/解码支持,因而可以简单地向程序整合音频、视频和图像。要使用平台的媒体功能非常简单——同样只需使用意图-活动的机制,之后Android会处理其余的工作。
Android可以从多种数据源类型中播放音频和视频。可以播放存储在程序资源(原始资源 raw resource)中的音频或视频媒体文件,可以播放文件系统中的某一文件,可以播放经由网络的流数据。要让程序播放视频或音频,需使用MediaPlayer类。
如果移动设备硬件支持的话,平台也支持录制音频和视频。要录制音频或视频,需使用MediaRecorder类。注意,模拟器没有获取音频或视频的硬件,不过实际的移动设备通常会提供这些支持,可以通过MediaRecorder类使用。
要了解Android提供内建支持的媒体格式列表,参见附录“Android媒体格式”。
音频和视频播放
可以播放任意来源的媒体:原始资源、系统中的文件或是可用的网络(URL)。
仅可以通过标准输出设备播放音频数据;目前来说,就是至移动设备的扬声器或是蓝牙耳机。现在不能在通话音频中播放声音文件。
播放原始资源
最为常见的需求或许就是从自有程序中播放媒体(一般是声音)了吧。这很容易做到:
- 将声音(或其他媒体资源)文件放入工程的res/raw文件夹,Eclipse插件(或aapt)将会发现它并将其识别为R类可以引用的资源。
- 创建一个MediaPlayer的实例,用MediaPlayer.create引用该资源,之后调用实例的start()方法:
1 2 |
<span style="color: #000000;"><span style="color: #800080;"> MediaPlayer</span> mp = <span style="color: #800080;">MediaPlayer</span>.create(context, R.raw.sound_file_1);</span> <span style="color: #000000;"> mp.start();</span> |
要停止播放,调用stop()。如果希望之后重放该媒体,就必须在再次调用start()之前reset()及prepare()该MediaPlayer对象。(create()在第一次使用时会调用prepare()。)
要暂停播放,调用pause()。在想要继续播放的时候调用start()。
播放文件或流
可以播放储存于文件系统中或位于网络URL的媒体文件:
- 使用new来创建一个新的MediaPlayer实例
- 以一个包含了要播放的文件的路径(文件地址或是URL)的字符串调用setDataSource()
- 首先prepare()之后start()该实例:
1 2 3 4 |
<span style="color: #800080;">MediaPlayer</span> <span style="color: #000000;">mp =</span> <span style="color: #000080;">new</span> <span style="color: #800080;">MediaPlayer</span><span style="color: #000000;">();</span> <span style="color: #000000;">mp.setDataSource(PATH_TO_FILE);</span> <span style="color: #000000;"> mp.prepare();</span> <span style="color: #000000;"> mp.start();</span> |
stop()和pause()的工作方式同上。
注意:当引用的文件不存在时,IllegalArgumentException 和IOException 可能会在使用setDataSource()时被接收或传递。
注意:如果传递的是在线媒体文件的URL,该文件必须可以被逐步下载。
播放JET内容
Android平台包含了一个JET引擎,它允许在程序中添加与JET音频内容的播放控制交互。可以使用SDK附带的JetCreator授权程序创建JET内容的播放控制交互。要在程序中播放并管理JET内容,需要使用JetPlayer类。
关于JET理念的描述及使用JetCreator授权工具的使用说明,参见“JetCreator用户手册”。该工具在OS X和Windows平台下全功能可用,在Linux下所有的内容创建功能也都被支持,只是输入内容审查功能不被支持。
这里有一个如何从一个储存于SD卡的.jet文件中建立JET播放的例子:
1 2 3 4 5 6 7 8 9 10 |
<span style="color: #000000;"><span style="color: #800080;">JetPlayer</span> myJet = <span style="color: #800080;">JetPlayer</span>.getJetPlayer();</span> <span style="color: #000000;">myJet.loadJetFile(<span style="color: #008000;">"/sdcard/level1.jet"</span>);</span> <span style="color: #000000;"><span style="color: #000080;">byte</span> segmentId = 0;</span> <span style="color: #993300;">// queue segment 5, repeat once, use General MIDI, transpose by -1 octave</span> <span style="color: #000000;">myJet.queueJetSegment(<span style="color: #008080;">5</span>, -<span style="color: #008080;">1</span>, <span style="color: #008080;">1</span>, -<span style="color: #008080;">1</span>, <span style="color: #008080;">0</span>, segmentId++);</span> <span style="color: #993300;">// queue segment 2</span> <span style="color: #000000;">myJet.queueJetSegment(<span style="color: #008080;">2</span>, -<span style="color: #008080;">1</span>, <span style="color: #008080;">0</span>, <span style="color: #008080;">0</span>, <span style="color: #008080;">0</span>, segmentId++);</span> <span style="color: #000000;">myJet.play();</span> |
SDK包含了一个程序范例——JetBoy——演示了如何使用JetPlayer创建游戏中的音乐音轨播放交互。它同时也展示了如何使用JET事件来同步音乐播放和游戏逻辑。该程序位于<sdk>/platforms/android-1.5/samples/JetBoy。
音频录制
通过设备录制音频要比播放音频/视频稍微复杂一些,不过其实也是相当简单:
- 使用new来创建android.media.MediaRecorder的一个新的实例
- 使用MediaRecorder.setAudioSource()来设置音频源。通常会使用MediaRecorder.AudioSource.MIC
- 使用MediaRecorder.setOutputFormat()来设置输出文件格式
- 使用MediaRecorder.setOutputFile()来设置输出文件名
- 使用MediaRecorder.setAudioEncoder()来设置音频编码
- 对MediaRecorder实例调用MediaRecorder.prepare()
- 调用MediaRecorder.start()以开始音频录制
- 调用MediaRecorder.stop()以结束音频录制
- 当不再需要MediaRecorder实例时,对其调用MediaRecorder.release()。建议调用MediaRecorder.release()来立即释放资源
下面的这个范例类演示了如何建立、开始及终止音频录制,之后播放录制的音频文件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
<span style="color: #993300;">/*</span> <span style="color: #993300;"> * The application needs to have the permission to write to external storage</span> <span style="color: #993300;"> * if the output file is written to the external storage, and also the</span> <span style="color: #993300;"> * permission to record audio. These permissions must be set in the</span> <span style="color: #993300;"> * application's AndroidManifest.xml file, with something like:</span> <span style="color: #993300;"> *</span> <span style="color: #993300;"> * <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /></span> <span style="color: #993300;"> * <uses-permission android:name="android.permission.RECORD_AUDIO" /></span> <span style="color: #993300;"> *</span> <span style="color: #993300;"> */</span> <span style="color: #000000;"><span style="color: #000080;">package</span> com.android.audiorecordtest;</span> <span style="color: #000000;"><span style="color: #000080;">import</span> android.app.<span style="color: #800080;">Activity</span>;</span> <span style="color: #000000;"><span style="color: #000080;">import</span> android.widget.<span style="color: #800080;">LinearLayout</span>;</span> <span style="color: #000000;"><span style="color: #000080;">import</span> android.os.<span style="color: #800080;">Bundle</span>;</span> <span style="color: #000000;"><span style="color: #000080;">import</span> android.os.<span style="color: #800080;">Environment</span>;</span> <span style="color: #000000;"><span style="color: #000080;">import</span> android.view.<span style="color: #800080;">ViewGroup</span>;</span> <span style="color: #000000;"><span style="color: #000080;">import</span> android.widget.<span style="color: #800080;">Button</span>;</span> <span style="color: #000000;"><span style="color: #000080;">import</span> android.view.<span style="color: #800080;">View</span>;</span> <span style="color: #000000;"><span style="color: #000080;">import</span> android.view.View.<span style="color: #800080;">OnClickListener</span>;</span> <span style="color: #000000;"><span style="color: #000080;">import</span> android.content.<span style="color: #800080;">Context</span>;</span> <span style="color: #000000;"><span style="color: #000080;">import</span> android.util.<span style="color: #800080;">Log</span>;</span> <span style="color: #000000;"><span style="color: #000080;">import</span> android.media.<span style="color: #800080;">MediaRecorder</span>;</span> <span style="color: #000000;"><span style="color: #000080;">import</span> android.media.<span style="color: #800080;">MediaPlayer</span>;</span> <span style="color: #000000;"><span style="color: #000080;">import</span> java.io.<span style="color: #800080;">IOException</span>;</span> <span style="color: #000000;"><span style="color: #000080;">public</span> <span style="color: #000080;">class</span> <span style="color: #800080;">AudioRecordTest</span> <span style="color: #000080;">extends</span> <span style="color: #800080;">Activity</span></span> <span style="color: #000000;">{</span> <span style="color: #000000;"><span style="color: #000080;"> private static</span> <span style="color: #000080;">final</span> <span style="color: #800080;">String</span> LOG_TAG = "AudioRecordTest";</span> <span style="color: #000000;"><span style="color: #000080;"> private</span> <span style="color: #000080;">static</span> <span style="color: #800080;">String</span> mFileName = <span style="color: #000080;">null</span>;</span> <span style="color: #000000;"><span style="color: #000080;"> private</span> <span style="color: #800080;">RecordButton</span> mRecordButton = <span style="color: #000080;">null</span>;</span> <span style="color: #000000;"><span style="color: #000080;"> private</span> <span style="color: #800080;">MediaRecorder</span> mRecorder = <span style="color: #000080;">null</span>;</span> <span style="color: #000000;"><span style="color: #000080;"> private</span> <span style="color: #800080;">PlayButton </span> mPlayButton = <span style="color: #000080;">null</span>;</span> <span style="color: #000000;"><span style="color: #000080;"> private</span> <span style="color: #800080;">MediaPlayer </span> mPlayer = <span style="color: #000080;">null</span>;</span> <span style="color: #000000;"><span style="color: #000080;"> private</span> <span style="color: #000080;">void</span> onRecord(<span style="color: #000080;">boolean</span> start) {</span> <span style="color: #000000;"><span style="color: #000080;"> if</span> (start) {</span> <span style="color: #000000;"> startRecording();</span> <span style="color: #000000;"> } <span style="color: #000080;">else</span> {</span> <span style="color: #000000;"> stopRecording();</span> <span style="color: #000000;"> }</span> <span style="color: #000000;"> }</span> <span style="color: #000000;"><span style="color: #000080;"> private</span> <span style="color: #000080;">void</span> onPlay(<span style="color: #000080;">boolean</span> start) {</span> <span style="color: #000000;"><span style="color: #000080;"> if</span> (start) {</span> <span style="color: #000000;"> startPlaying();</span> <span style="color: #000000;"> } <span style="color: #000080;">else</span> {</span> <span style="color: #000000;"> stopPlaying();</span> <span style="color: #000000;"> }</span> <span style="color: #000000;"> }</span> <span style="color: #000000;"><span style="color: #000080;"> private</span> <span style="color: #000080;">void</span> startPlaying() {</span> <span style="color: #000000;"> mPlayer = <span style="color: #000080;">new</span> <span style="color: #800080;">MediaPlayer</span>();</span> <span style="color: #000000;"><span style="color: #000080;"> try</span> {</span> <span style="color: #000000;"> mPlayer.setDataSource(mFileName);</span> <span style="color: #000000;"> mPlayer.prepare();</span> <span style="color: #000000;"> mPlayer.start();</span> <span style="color: #000000;"> } <span style="color: #000080;">catch</span> (<span style="color: #800080;">IOException</span> e) {</span> <span style="color: #000000;"><span style="color: #800080;"> Log</span>.e(LOG_TAG, <span style="color: #008000;">"prepare() failed"</span>);</span> <span style="color: #000000;"> }</span> <span style="color: #000000;"> }</span> <span style="color: #000000;"><span style="color: #000080;"> private</span> <span style="color: #000080;">void</span> stopPlaying() {</span> <span style="color: #000000;"> mPlayer.release();</span> <span style="color: #000000;"> mPlayer = <span style="color: #000080;">null</span>;</span> <span style="color: #000000;"> }</span> <span style="color: #000000;"><span style="color: #000080;"> private</span> <span style="color: #000080;">void</span> startRecording() {</span> <span style="color: #000000;"> mRecorder = <span style="color: #000080;">new</span> <span style="color: #800080;">MediaRecorder</span>();</span> <span style="color: #000000;"> mRecorder.setAudioSource(<span style="color: #800080;">MediaRecorder.AudioSource</span>.MIC);</span> <span style="color: #000000;"> mRecorder.setOutputFormat(<span style="color: #800080;">MediaRecorder.OutputFormat</span>.THREE_GPP);</span> <span style="color: #000000;"> mRecorder.setOutputFile(mFileName);</span> <span style="color: #000000;"> mRecorder.setAudioEncoder(<span style="color: #800080;">MediaRecorder.AudioEncoder</span>.AMR_NB);</span> <span style="color: #000000;"><span style="color: #000080;"> try</span> {</span> <span style="color: #000000;"> mRecorder.prepare();</span> <span style="color: #000000;"> } <span style="color: #000080;">catch</span> (<span style="color: #800080;">IOException</span> e) {</span> <span style="color: #000000;"><span style="color: #800080;"> Log</span>.e(LOG_TAG, <span style="color: #008000;">"prepare() failed"</span>);</span> <span style="color: #000000;"> }</span> <span style="color: #000000;"> mRecorder.start();</span> <span style="color: #000000;"> }</span> <span style="color: #000000;"><span style="color: #000080;"> private</span> <span style="color: #000080;">void</span> stopRecording() {</span> <span style="color: #000000;"> mRecorder.stop();</span> <span style="color: #000000;"> mRecorder.release();</span> <span style="color: #000000;"> mRecorder = <span style="color: #000080;">null</span>;</span> <span style="color: #000000;"> }</span> <span style="color: #000000;"><span style="color: #000080;"> class</span> <span style="color: #800080;">RecordButton</span> <span style="color: #000080;">extends</span> <span style="color: #800080;">Button</span> {</span> <span style="color: #000000;"><span style="color: #000080;"> boolean</span> mStartRecording = <span style="color: #000080;">true</span>;</span> <span style="color: #000000;"><span style="color: #800080;"> OnClickListener</span> clicker = <span style="color: #000080;">new</span> <span style="color: #800080;">OnClickListener</span>() {</span> <span style="color: #000000;"><span style="color: #000080;"> public</span> <span style="color: #000080;">void</span> onClick(<span style="color: #800080;">View</span> v) {</span> <span style="color: #000000;"> onRecord(mStartRecording);</span> <span style="color: #000000;"><span style="color: #000080;"> if</span> (mStartRecording) {</span> <span style="color: #000000;"> setText("Stop recording");</span> <span style="color: #000000;"> } <span style="color: #000080;">else</span> {</span> <span style="color: #000000;"> setText("Start recording");</span> <span style="color: #000000;"> }</span> <span style="color: #000000;"> mStartRecording = !mStartRecording;</span> <span style="color: #000000;"> }</span> <span style="color: #000000;"> };</span> <span style="color: #000000;"><span style="color: #000080;"> public</span> <span style="color: #800080;">RecordButton</span>(<span style="color: #800080;">Context</span> ctx) {</span> <span style="color: #000000;"><span style="color: #000080;"> super</span>(ctx);</span> <span style="color: #000000;"> setText(<span style="color: #008000;">"Start recording"</span>);</span> <span style="color: #000000;"> setOnClickListener(clicker);</span> <span style="color: #000000;"> }</span> <span style="color: #000000;"> }</span> <span style="color: #000000;"><span style="color: #000080;"> class</span> <span style="color: #800080;">PlayButton</span> <span style="color: #000080;">extends</span> <span style="color: #800080;">Button</span> {</span> <span style="color: #000000;"><span style="color: #000080;"> boolean</span> mStartPlaying = <span style="color: #000080;">true</span>;</span> <span style="color: #000000;"><span style="color: #800080;"> OnClickListener</span> clicker = <span style="color: #000080;">new</span> <span style="color: #800080;">OnClickListener</span>() {</span> <span style="color: #000000;"><span style="color: #000080;"> public</span> <span style="color: #000080;">void</span> onClick(<span style="color: #800080;">View</span> v) {</span> <span style="color: #000000;"> onPlay(mStartPlaying);</span> <span style="color: #000000;"><span style="color: #000080;"> if</span> (mStartPlaying) {</span> <span style="color: #000000;"> setText(<span style="color: #008000;">"Stop playing"</span>);</span> <span style="color: #000000;"> } <span style="color: #000080;">else</span> {</span> <span style="color: #000000;"> setText(<span style="color: #008000;">"Start playing"</span>);</span> <span style="color: #000000;"> }</span> <span style="color: #000000;"> mStartPlaying = !mStartPlaying;</span> <span style="color: #000000;"> }</span> <span style="color: #000000;"> };</span> <span style="color: #000000;"><span style="color: #000080;"> public</span> <span style="color: #800080;">PlayButton</span>(<span style="color: #800080;">Context</span> ctx) {</span> <span style="color: #000000;"><span style="color: #000080;"> super</span>(ctx);</span> <span style="color: #000000;"> setText(<span style="color: #008000;">"Start playing"</span>);</span> <span style="color: #000000;"> setOnClickListener(clicker);</span> <span style="color: #000000;"> }</span> <span style="color: #000000;"> }</span> <span style="color: #000000;"><span style="color: #000080;"> public</span> <span style="color: #800080;">AudioRecordTest</span>() {</span> <span style="color: #000000;"> mFileName = <span style="color: #800080;">Environment</span>.getExternalStorageDirectory().getAbsolutePath();</span> <span style="color: #000000;"> mFileName += <span style="color: #008000;">"/audiorecordtest.3gp"</span>;</span> <span style="color: #000000;"> }</span> <span style="color: #008080;"> @Override</span> <span style="color: #000000;"><span style="color: #000080;"> public</span> <span style="color: #000080;">void</span> onCreate(<span style="color: #800080;">Bundle</span> icicle) {</span> <span style="color: #000000;"><span style="color: #000080;"> super</span>.onCreate(icicle);</span> <span style="color: #000000;"><span style="color: #800080;"> LinearLayout</span> ll = <span style="color: #000080;">new</span> <span style="color: #800080;">LinearLayout</span>(<span style="color: #000080;">this</span>);</span> <span style="color: #000000;"> mRecordButton = <span style="color: #000080;">new</span> <span style="color: #800080;">RecordButton</span>(<span style="color: #000080;">this</span>);</span> <span style="color: #000000;"> ll.addView(mRecordButton,</span> <span style="color: #000000;"><span style="color: #000080;"> new</span> <span style="color: #800080;">LinearLayout.LayoutParams</span>(</span> <span style="color: #000000;"><span style="color: #800080;"> ViewGroup.LayoutParams</span>.WRAP_CONTENT,</span> <span style="color: #000000;"><span style="color: #800080;"> ViewGroup.LayoutParams</span>.WRAP_CONTENT,</span> <span style="color: #000000;"><span style="color: #008080;"> 0</span>));</span> <span style="color: #000000;"> mPlayButton = <span style="color: #000080;">new</span> <span style="color: #800080;">PlayButton</span>(<span style="color: #000080;">this</span>);</span> <span style="color: #000000;"> ll.addView(mPlayButton,</span> <span style="color: #000000;"><span style="color: #000080;"> new</span> <span style="color: #800080;">LinearLayout.LayoutParams</span>(</span> <span style="color: #000000;"><span style="color: #800080;"> ViewGroup.LayoutParams</span>.WRAP_CONTENT,</span> <span style="color: #000000;"><span style="color: #800080;"> ViewGroup.LayoutParams</span>.WRAP_CONTENT,</span> <span style="color: #000000;"><span style="color: #008080;"> 0</span>));</span> <span style="color: #000000;"> setContentView(ll);</span> <span style="color: #000000;"> }</span> <span style="color: #008080;"> @Override</span> <span style="color: #000000;"><span style="color: #000080;"> public</span> <span style="color: #000080;">void</span> onPause() {</span> <span style="color: #000000;"><span style="color: #000080;"> super</span>.onPause();</span> <span style="color: #000000;"><span style="color: #000080;"> if</span> (mRecorder != <span style="color: #000080;">null</span>) {</span> <span style="color: #000000;"> mRecorder.release();</span> <span style="color: #000000;"> mRecorder = <span style="color: #000080;">null</span>;</span> <span style="color: #000000;"> }</span> <span style="color: #000000;"><span style="color: #000080;"> if</span> (mPlayer != <span style="color: #000080;">null</span>) {</span> <span style="color: #000000;"> mPlayer.release();</span> <span style="color: #000000;"> mPlayer = <span style="color: #000080;">null</span>;</span> <span style="color: #000000;"> }</span> <span style="color: #000000;"> }</span> <span style="color: #000000;">}</span> |
本页部分内容根据Android Open Source Project创作并共享的内容修改,并在知识共享 署名2.5许可协议中所述条款的限制下使用。