標籤:
The Android platform offers built-in encoding/decoding for a variety of common media types, so that you can easily integrate audio, video, and images into your applications.
android平台內建了相關的編碼和解碼工具,據此,開發人員可以很輕鬆的整合音頻視頻和照片到自己的應用程式中。
大致分為兩項:
使用android播放音樂和視頻,此時使用MediaPlayer這個類和相關的方法
使用android錄製音頻和視頻,此時使用MediaRecorder這個類和相關的方法
先看一下:android支援的一些常用的格式:
來自API
支援的音頻格式有:WAVE,MP3,3GP,視頻格式:MP4 3GP ,圖片格式:JPG,GIF,png,和bmp.
音視頻的播放:
Android lets you play audio and video from several types of data sources. You can play audio or video from media files stored in the application‘s resources (raw resources),
from standalone files in the filesystem, or from a data stream arriving over a network connection. To play audio or video from your application, use the MediaPlayer class.
API推薦使用MediaPlayer來播放來自不同渠道不同類型的資源檔。
熟悉:MediaPlayer
MediaPlayer class can be used to control playback of audio/video files and streams.繼承之Object類在android.media包下。
關於MediaPlayer的狀態圖和許可權部分:查看API。
接下看一些MediaPlayer常用的方法:
預設的無參構造方法:MediaPlayer mp= new MediaPlayer();
三個靜態方法 :
這些方法可以用來得到不同路徑下的MediaPlayer資來源物件。比如: MediaPlayer mp = MediaPlayer.create(this,R.raw.cong);
開啟了res/raw/檔案下的cong的音頻資源。再調用mp.start()即可播放該資源,注意異常捕獲。
上面四個方法用來擷取資源的相關資訊:比如擷取當前的位置,返回current position in milliseconds ,返回資源的長度,視頻的寬度和高度。
isPlaying(),用來判斷是否在播放,,isLooping()判斷是否迴圈播放,傳回值均為boolean.
Android之Audio和Video