Android多媒體應(yīng)用使用MediaPlayer播放音頻
Android提供了對常用音頻和視頻格式的支持,它所支持的音頻格式有MP3(.mp3)、3GPP(.3gp)、Ogg(.ogg)和WAVE(.ave)等,支持的視頻格式有3GPP(.3gp)和MPEG-4(.mp4)等。通過AndroidAPI提供的相關(guān)方法,在Android中可以實(shí)現(xiàn)音頻與視頻的播放。
下面介紹使用MediaPlayer播放音頻
在Android中,提供了MediaPlayer類來播放音頻。使用MediaPlayer類播放音頻比較簡單,只需要創(chuàng)建該類的對象,并為其指定要播放的音頻文件,然后調(diào)用該類的start()方法即可,下面進(jìn)行詳細(xì)介紹。
1.創(chuàng)建MediaPlayer對象,并裝載音頻文件
兩種方法,都是使用MediaPlayer的靜態(tài)方法creat()來實(shí)現(xiàn)。
a.creat(Context context,int resid)
從資源文件中加載,例如
MediaPlayer player=MediaPlayer.creat(this,R.rwa.d);
b.creat(Context context,Uri uri)
根據(jù)指定的URI來裝載音頻,例如
MediaPlayer player=MediaPlayer.creat("Http://www.musicbox.com/sound/bg.mp3");
使用creat()方法時,已經(jīng)加載了音頻,但是用無參構(gòu)造方法來創(chuàng)建MediaPlayer對象時,需要單獨(dú)指定要裝載的資源,這可以使用MediaPlayer類的setDataSource()方法來實(shí)現(xiàn)。
在使用setDataSource()方法裝載音頻文件后,實(shí)際上MediaPlayer并為真正裝載該音頻文件,需要調(diào)用MediaPlayer的prepare()方法去真正裝載音頻文件。使用無參構(gòu)造方法來創(chuàng)建MediaPlayer對象并裝載指定的音頻文件,可以使用下面的代碼:
MediaPlayer player=new MediaPlayer(); try { player.setDataSource("/sdcard/suger.mp3");//指定要裝載的音頻文件 } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { player.prepare();//預(yù)加載音頻 } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
2.開始或恢復(fù)播放
開始播放或恢復(fù)已經(jīng)暫停的音頻的播放
player.start();
3.停止播放
可以停止正在播放的音頻
player.stop();
4.暫停播放
可以暫停正在播放的音頻
player.pause();
下面做一個小實(shí)例,實(shí)現(xiàn)包括播放、暫停/繼續(xù)和停止功能的簡易音樂播放器
將要播放的音頻文件上傳到SD卡的Music目錄中,這里要播放的音頻文件為Whistle.mp3
目錄如圖
布局文件,包括一個文本信息顯示控件和三個按鈕(播放、暫停/繼續(xù)和停止按鈕)
res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/ll1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="歡迎使用音樂播放器" android:id="@+id/hint"/> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/play" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="播放"/> <Button android:id="@+id/pause" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="暫停"/> <Button android:id="@+id/stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="停止"/> </LinearLayout> </LinearLayout>
MainActivity:
package com.example.test; import java.io.File; import android.app.Activity; import android.media.MediaPlayer; import android.media.MediaPlayer.OnCompletionListener; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity{ private MediaPlayer player;//MediaPlayer對象 private boolean isPause=false;//是否暫停 private File file;//要播放的音頻文件 private TextView hint;//聲明顯示提示信息的文本框 private Button play;//播放按鈕 private Button pause;//暫停/繼續(xù)按鈕 private Button stop;//停止按鈕 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); play=(Button)findViewById(R.id.play);//獲取"播放"按鈕 play.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { play();//開始播放音樂 if(isPause){ pause.setText("暫停"); isPause=false;//設(shè)置暫停標(biāo)記為false } pause.setEnabled(true);//"暫停/繼續(xù)"按鈕可用 stop.setEnabled(true);//"停止"按鈕可用 play.setEnabled(false);//"播放"按鈕不可用 } }); pause=(Button)findViewById(R.id.pause);//獲取"暫停"按鈕 pause.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if(player.isPlaying()&&!isPause){ player.pause();//暫停播放 isPause=true; ((Button)v).setText("繼續(xù)"); hint.setText("暫停播放音頻..."); play.setEnabled(true);//播放按鈕可用 }else{ player.start();//繼續(xù)播放 isPause=false; ((Button)v).setText("暫停"); hint.setText("繼續(xù)播放音頻..."); play.setEnabled(false);//播放按鈕不可用 } } }); stop=(Button)findViewById(R.id.stop);//獲取"停止"按鈕 stop.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { player.stop(); hint.setText("停止播放音頻..."); pause.setEnabled(false);//"暫停/繼續(xù)"按鈕不可用 stop.setEnabled(false);//"停止"按鈕不可用 play.setEnabled(true);//"播放"按鈕可用 } }); hint=(TextView)findViewById(R.id.hint);//獲取顯示提示信息的文本框 file=new File(getSDPath()+"/"+"Music/Whistle.mp3"); if(file.exists()){ player=MediaPlayer.create(MainActivity.this, Uri.parse(file.getAbsolutePath()));//創(chuàng)建MediaPlayer對象 }else{ hint.setText("要播放的音頻不存在!"); play.setEnabled(false); return; } //添加完成事件監(jiān)聽器,用于當(dāng)音樂播放完畢后,重新開始播放因音樂 player.setOnCompletionListener(new OnCompletionListener() { @Override public void onCompletion(MediaPlayer arg0) { play();//重新開始播放 } }); } //播放音樂的方法 public void play(){ try { player.reset(); player.setDataSource(file.getAbsolutePath());//重新設(shè)置要播放的音頻 player.prepare();//預(yù)加載音頻 player.start();//開始播放 hint.setText("正在播放音樂..."); } catch (Exception e) { e.printStackTrace(); } } //獲取sdcard根目錄的方法 public String getSDPath(){ File sdDir = null; boolean sdCardExist = Environment.getExternalStorageState() .equals(android.os.Environment.MEDIA_MOUNTED); //判斷sd卡是否存在 if(sdCardExist) //如果SD卡存在,則獲取跟目錄 { sdDir = Environment.getExternalStorageDirectory();//獲取根目錄 } return sdDir.toString(); } //Activity銷銷毀時,停止正在播放的音頻,并釋放MediaPlayer所占用的資源 @Override protected void onDestroy() { if(player.isPlaying()){ player.stop();//停止音頻的播放 } player.release();//釋放資源 super.onDestroy(); } }
播放效果如圖
暫停效果如圖
繼續(xù)效果如圖
停止效果如圖
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實(shí)現(xiàn)評論欄隨Recyclerview滑動左右移動
這篇文章主要介紹了Android實(shí)現(xiàn)評論欄隨Recyclerview滑動左右移動效果,仿約會吧應(yīng)用詳情頁實(shí)現(xiàn),感興趣的小伙伴們可以參考一下2016-05-05Android ViewPager無限循環(huán)實(shí)現(xiàn)底部小圓點(diǎn)動態(tài)滑動
這篇文章主要為大家詳細(xì)介紹了Android ViewPager無限循環(huán)實(shí)現(xiàn)底部小圓點(diǎn)動態(tài)滑動的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-03-03Flutter 用自定義轉(zhuǎn)場動畫實(shí)現(xiàn)頁面切換
本篇介紹了 fluro 導(dǎo)航到其他頁面的自定義轉(zhuǎn)場動畫實(shí)現(xiàn),F(xiàn)lutter本身提供了不少預(yù)定義的轉(zhuǎn)場動畫,可以通過 transitionBuilder 參數(shù)設(shè)計多種多樣的轉(zhuǎn)場動畫,也可以通過自定義的 AnimatedWidget實(shí)現(xiàn)個性化的轉(zhuǎn)場動畫效果。2021-06-06解決android studio 3.0 加載項(xiàng)目過慢問題--maven倉庫選擇
這篇文章主要介紹了android studio 3.0 加載項(xiàng)目過慢問題解決方案---maven倉庫選擇,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-11-11android圖像繪制(四)自定義一個SurfaceView控件
自定義控件(類似按鈕等)的使用,自定義一個SurfaceView。如某一塊的動態(tài)圖(自定義相應(yīng)),或者類似UC瀏覽器下面的工具欄,感興趣的朋友可以了解下2013-01-01