本講內(nèi)容:Android中的音頻和視頻使用入門指南
Android 提供了 MediaPlayer 和 MediaRecorder 兩個(gè)工具類,來幫助開發(fā)者操作音頻和視頻。我們通過兩個(gè)小例子來學(xué)習(xí)一下多媒體資源的使用。
一、 簡(jiǎn)單音樂播放器
1、新建一個(gè)項(xiàng)目Lesson28_Music , 主Activity的名字是 MainMusic.java
2、拷貝 這幾張圖片到res/drawable目錄下,并建立3個(gè)xml文件,拷貝love.mp3到res/raw文件中。
play.xml
1 | <? xml version = "1.0" encoding = "utf-8" ?> |
2 | < selector xmlns:android = "> |
3 | < item android:state_enabled = "false" android:drawable = "@drawable/play_disable" > |
4 | < item android:drawable = "@drawable/play_50" > |
5 | </ item ></ item ></ selector > |
pause.xml
1 | <? xml version = "1.0" encoding = "utf-8" ?> |
2 | < selector xmlns:android = "> |
3 | < item android:state_enabled = "false" android:drawable = "@drawable/pause_disable" > |
4 | < item android:drawable = "@drawable/pause_50" > |
5 | </ item ></ item ></ selector > |
stop.xml
1 | <? xml version = "1.0" encoding = "utf-8" ?> |
2 | < selector xmlns:android = "> |
3 | < item android:state_enabled = "false" android:drawable = "@drawable/stop_disable" > |
4 | < item android:drawable = "@drawable/stop_50" > |
5 | </ item ></ item ></ selector > |
3、res/layout/main.xml 的內(nèi)容如下:
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
03 | < textview android:layout_height = "wrap_content" android:layout_width = "fill_parent" android:text = "簡(jiǎn)單音樂播放器" android:textsize = "25sp" > |
07 | < imagebutton android:background = "@drawable/play" android:layout_height = "wrap_content" android:layout_width = "wrap_content" android:id = "@+id/play" android:adjustviewbounds = "true" android:layout_margin = "4dp" > |
10 | < imagebutton android:background = "@drawable/pause" android:layout_height = "wrap_content" android:layout_width = "wrap_content" android:id = "@+id/pause" android:adjustviewbounds = "true" android:layout_margin = "4dp" > |
13 | < imagebutton android:background = "@drawable/stop" android:layout_height = "wrap_content" android:layout_width = "wrap_content" android:id = "@+id/stop" android:adjustviewbounds = "true" android:layout_margin = "4dp" > |
4、MainMusic.java的內(nèi)容如下:
001 | package android.basic.lesson28; |
003 | import java.io.IOException; |
005 | import android.app.Activity; |
006 | import android.media.MediaPlayer; |
007 | import android.media.MediaPlayer.OnCompletionListener; |
008 | import android.media.MediaPlayer.OnPreparedListener; |
009 | import android.os.Bundle; |
010 | import android.view.View; |
011 | import android.view.View.OnClickListener; |
012 | import android.widget.ImageButton; |
013 | import android.widget.Toast; |
015 | public class MainMusic extends Activity { |
018 | private ImageButton play, pause, stop; |
019 | private MediaPlayer mPlayer; |
021 | /** Called when the activity is first created. */ |
023 | public void onCreate(Bundle savedInstanceState) { |
024 | super .onCreate(savedInstanceState); |
025 | setContentView(R.layout.main); |
028 | play = (ImageButton) findViewById(R.id.play); |
029 | pause = (ImageButton) findViewById(R.id.pause); |
030 | stop = (ImageButton) findViewById(R.id.stop); |
033 | play.setEnabled( false ); |
034 | pause.setEnabled( false ); |
035 | stop.setEnabled( false ); |
038 | OnClickListener ocl = new View.OnClickListener() { |
041 | public void onClick(View v) { |
045 | Toast.makeText(MainMusic. this , "點(diǎn)擊播放" , Toast.LENGTH_SHORT) |
051 | Toast.makeText(MainMusic. this , "暫停播放" , Toast.LENGTH_SHORT) |
057 | Toast.makeText(MainMusic. this , "停止播放" , Toast.LENGTH_SHORT) |
066 | play.setOnClickListener(ocl); |
067 | pause.setOnClickListener(ocl); |
068 | stop.setOnClickListener(ocl); |
075 | private void initMediaPlayer() { |
078 | mPlayer = MediaPlayer.create(getApplicationContext(), R.raw.love); |
081 | mPlayer.setOnPreparedListener( new OnPreparedListener() { |
083 | public void onPrepared(MediaPlayer mp) { |
085 | Toast.makeText(MainMusic. this , "onPrepared" , Toast.LENGTH_SHORT) |
087 | play.setEnabled( true ); |
092 | mPlayer.setOnCompletionListener( new OnCompletionListener() { |
095 | public void onCompletion(MediaPlayer mp) { |
096 | Toast.makeText(MainMusic. this , "onCompletion" , |
097 | Toast.LENGTH_SHORT).show(); |
104 | private void stop() { |
106 | pause.setEnabled( false ); |
107 | stop.setEnabled( false ); |
111 | play.setEnabled( true ); |
112 | } catch (IllegalStateException e) { |
114 | } catch (IOException e) { |
121 | private void play() { |
124 | play.setEnabled( false ); |
125 | pause.setEnabled( true ); |
126 | stop.setEnabled( true ); |
130 | private void pause() { |
132 | play.setEnabled( true ); |
133 | pause.setEnabled( false ); |
134 | stop.setEnabled( true ); |
139 | protected void onDestroy() { |
141 | if (stop.isEnabled()) { |
5、運(yùn)行程序,查看效果
二、簡(jiǎn)單視頻播放器
Android為視頻播放提供了VideoView 和 MediaController 兩個(gè)現(xiàn)成的組件,讓我們可以方便的實(shí)現(xiàn)MP4、3GP等視頻的播放。下面我們通過一個(gè)例子來看一下:
1、新建一個(gè)項(xiàng)目 Lesson28_Video
2、使用 Format Factory 這個(gè)軟件壓縮一個(gè)視頻備用,我這里壓縮的參數(shù)如下:
注意,如果播放時(shí)完全無法播放或者只有聲音沒有圖像,你就需要換壓縮軟件和調(diào)整壓縮參數(shù)重新壓縮視頻了,暫時(shí)只能這樣,我也是折騰了2-3小時(shí)都是黑屏,郁悶中(似乎得出一個(gè)答案,是否黑屏和機(jī)器設(shè)備的性能有關(guān),我降低壓縮分辨率和每秒幀數(shù),出圖像音畫同步,如果提高每秒幀數(shù),聲音出來后十幾秒圖像才會(huì)出來,但是出來后音畫還是同步的,有興趣的朋友可以多測(cè)試測(cè)試給出一個(gè)結(jié)論)。
用命令行的方式拷貝此視頻到存儲(chǔ)卡(sdcard)中,為什么不用eclipse中的可視化工具拷貝呢?因?yàn)槟莻€(gè)方式靠大文件的時(shí)候經(jīng)常失敗,而命令行方式我沒拷貝失敗一次過。命令就是 adb push ,具體截個(gè)圖給你看:
3、res\layout\main.xml的內(nèi)容如下:
1 | <? xml version = "1.0" encoding = "utf-8" ?> |
3 | < videoview android:layout_height = "fill_parent" android:layout_width = "fill_parent" android:id = "@+id/VideoView01" > |
4、MainVideo.java的內(nèi)容如下:
01 | package android.basic.lesson28; |
03 | import android.app.Activity; |
04 | import android.net.Uri; |
05 | import android.os.Bundle; |
06 | import android.view.Window; |
07 | import android.view.WindowManager; |
08 | import android.widget.MediaController; |
09 | import android.widget.VideoView; |
11 | public class MainVideo extends Activity { |
12 | /** Called when the activity is first created. */ |
14 | public void onCreate(Bundle savedInstanceState) { |
15 | super .onCreate(savedInstanceState); |
17 | this .getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); |
19 | this .requestWindowFeature(Window.FEATURE_NO_TITLE); |
21 | setContentView(R.layout.main); |
24 | VideoView videoView = (VideoView) findViewById(R.id.VideoView01); |
26 | MediaController mediaController = new MediaController( this ); |
28 | mediaController.setAnchorView(videoView); |
30 | videoView.setMediaController(mediaController); |
33 | videoView.setVideoURI(Uri.parse( "/sdcard/love_480320.mp4" )); |
5、運(yùn)行效果如下:
好了本講內(nèi)容就到這里,下次再見。