android基礎---->音頻和視頻的使用

來源:互聯網
上載者:User

標籤:

  Android 在播放音頻和視頻方面也是做了相當不錯的支援,它提供了一套較為完整的API,使得開發人員可以很輕鬆地編寫出一個簡易的音頻或視頻播放器。今天我們開始android中音頻和視頻使用的學習。

 

目錄導航

  1.    音訊播放
  2.    視頻的播放
  3.    友情連結

 

音訊播放

項目結構如下:一個簡單的讀取sd卡上的音頻或者視頻資源的應用

在Android 中播放音頻檔案一般都是使用MediaPlayer 類來實現的,它對多種格式的音頻檔案提供了非常全面的控制方法,從而使得播放音樂的工作變得十分簡單。

一、 初始化音訊播放,調用MediaPlayer的setDataSource方法,可以接收音頻檔案的絕對路徑,也可以是http或者rtsp的url:

private final static String TAG = "MainActivity";private MediaPlayer mediaPlayer = new MediaPlayer();@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    initMediaPlayer();}private void initMediaPlayer() {    Log.i(TAG, "init media player");    try {        File file = new File(Environment.getExternalStorageDirectory(), "test.mp3");        mediaPlayer.setDataSource(file.getPath()); // 指定音頻檔案的路徑        mediaPlayer.prepare(); // 讓MediaPlayer進入到準備狀態    } catch (Exception e) {        e.printStackTrace();    }}

涉及到讀取sd卡檔案的許可權:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

二、 對音訊一系列操作:

// 播放音頻public void playMusic(View view) {    if (!mediaPlayer.isPlaying()) {        mediaPlayer.start(); // 開始播放    }}// 暫停音頻public void pauseMusic(View view) {    if (mediaPlayer.isPlaying()) {        mediaPlayer.pause(); // 暫停播放    }}// 停止音頻public void stopMusic(View view) {    if (mediaPlayer.isPlaying()) {        mediaPlayer.reset(); // 停止播放        initMediaPlayer();    }

三、 在ondestroy方法中釋放資源:

@Overrideprotected void onDestroy() {    super.onDestroy();    if (mediaPlayer != null) {        mediaPlayer.stop();        mediaPlayer.release();    }}

四、 layout檔案activity_main.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <VideoView        android:id="@+id/video_view"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent">        <Button            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:onClick="playVideo"            android:text="PlayVideo" />        <Button            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:onClick="pauseVideo"            android:text="PauseVideo" />        <Button            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:onClick="replyVideo"            android:text="ReplayVideo" />    </LinearLayout></LinearLayout>
View Code

 

視頻的播放

一、 初始化視頻的播放:

private VideoView videoView;@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_play_video);    videoView = (VideoView) findViewById(R.id.video_view);    initVideoPath();}// 初始化播放檔案private void initVideoPath() {    File file = new File(Environment.getExternalStorageDirectory(), "test.mp4");    videoView.setVideoPath(file.getPath()); // 指定視頻檔案的路徑}

 二、 視頻的一系列的操作:

// 播放視頻public void playVideo(View view) {    if (!videoView.isPlaying()) {        videoView.start(); // 開始播放    }}//暫停視頻public void pauseVideo(View view) {    if (videoView.isPlaying()) {        videoView.pause(); // 暫時播放    }}//重新播放視頻public void replyVideo(View view) {    if (videoView.isPlaying()) {        videoView.resume(); // 重新播放    }}

三、 在ondestroy方法中釋放資源:

@Overrideprotected void onDestroy() {    super.onDestroy();    if (videoView != null) {        videoView.suspend();    }}

四、 視頻的layout的檔案:activity_play_video.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <VideoView        android:id="@+id/video_view"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent">        <Button            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:onClick="playVideo"            android:text="PlayVideo" />        <Button            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:onClick="pauseVideo"            android:text="PauseVideo" />        <Button            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:onClick="replyVideo"            android:text="ReplayVideo" />    </LinearLayout></LinearLayout>
View Code

 

友情連結

 

android基礎---->音頻和視頻的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.