Android之Audio和Video

來源:互聯網
上載者:User

標籤:

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.