Android MediaPlayer基本使用方式

來源:互聯網
上載者:User

使用MediaPlayer播放音頻或者視頻的最簡單例子:
JAVA代碼部分:

  1. public class MediaPlayerStudy extends Activity {

  2. private Button bplay,bpause,bstop;

  3. private MediaPlayer mp = new MediaPlayer();



  4. @Override

  5. public void onCreate(Bundle savedInstanceState) {

  6. super.onCreate(savedInstanceState);

  7. setContentView(R.layout.main);



  8. bplay = (Button)findViewById(R.id.play);

  9. bpause = (Button)findViewById(R.id.pause);

  10. bstop = (Button)findViewById(R.id.stop);

  11. bplay.setOnClickListener(new OnClickListener(){

  12. @Override

  13. public void onClick(View v) {

  14. try {

  15. mp.setDataSource("/sdcard/test.mp3");

  16. mp.prepare();

  17. mp.start();

  18. } catch (IllegalArgumentException e) {

  19. e.printStackTrace();

  20. } catch (IllegalStateException e) {

  21. e.printStackTrace();

  22. } catch (IOException e) {

  23. e.printStackTrace();

  24. }

  25. mp.setOnCompletionListener(new OnCompletionListener(){

  26. @Override

  27. public void onCompletion(MediaPlayer mp) {

  28. mp.release();

  29. }

  30. });

  31. }

  32. });



  33. bpause.setOnClickListener(new OnClickListener(){

  34. @Override

  35. public void onClick(View v) {

  36. if(mp != null){

  37. mp.pause();

  38. }

  39. }

  40. });



  41. bstop.setOnClickListener(new OnClickListener(){

  42. @Override

  43. public void onClick(View v) {

  44. if(mp != null){

  45. mp.stop();

  46. }

  47. }

  48. });

  49. }



  50. @Override

  51. protected void onDestroy() {

  52. if(mp != null)

  53. mp.release();

  54. super.onDestroy();

  55. }

  56. }

複製代碼


布局檔案main.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical" android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">
  5. <TextView android:layout_width="fill_parent"
  6. android:layout_height="wrap_content" android:text="@string/hello" />
  7. <Button android:text="play" android:id="@+id/play"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"></Button>
  10. <Button android:text="pause" android:id="@+id/pause"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"></Button>
  13. <Button android:text="stop" android:id="@+id/stop"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"></Button>
  16. </LinearLayout>

複製代碼


程式說明:
這個例子只是描述了MediaPlayer的基本使用步驟和方式,MediaPlayer還有多種使用方式和方法,並不只局限於例子所介紹的一種。具體來看:
1)如何獲得MediaPlayer執行個體:
可以使用直接new的方式:
MediaPlayer mp = new MediaPlayer();
也可以使用create的方式,如:
MediaPlayer mp = MediaPlayer.create(this, R.raw.test);//這時就不用調用setDataSource了

2) 如何設定要播放的檔案:
MediaPlayer要播放的檔案主要包括3個來源:
a. 使用者在應用中事先內建的resource資源
例如:MediaPlayer.create(this, R.raw.test);
b. 儲存在SD卡或其他檔案路徑下的媒體檔案
例如:mp.setDataSource("/sdcard/test.mp3");
c. 網路上的媒體檔案
例如:mp.setDataSource("http://www.citynorth.cn/music/confucius.mp3");

MediaPlayer的setDataSource一共四個方法:
setDataSource (String path)

setDataSource (FileDescriptor fd)

setDataSource (Context context, Uri uri)

setDataSource (FileDescriptor fd, long offset, long length)

3)對播放器的主要控制方法:
Android通過控制播放器的狀態的方式來控制媒體檔案的播放,其中:
prepare()和prepareAsync() 提供了同步和非同步兩種方式設定播放器進入prepare狀態,需要注意的是,如果MediaPlayer執行個體是由create方法建立的,那麼第一次啟動播放前不需要再調用prepare()了,因為create方法裡已經調用過了。
start()是真正開機檔案播放的方法,
pause()和stop()比較簡單,起到暫停和停止播放的作用,

seekTo()是定位方法,可以讓播放器從指定的位置開始播放,需要注意的是該方法是個非同步方法呼叫,也就是說該方法返回時並不意味著定位完成,尤其是播放的網路檔案,真正定位完成時會觸發OnSeekComplete.onSeekComplete(),如果需要是可以調用setOnSeekCompleteListener(OnSeekCompleteListener)設定監聽器來處理的。
release()可以釋放播放器佔用的資源,一旦確定不再使用播放器時應當儘早調用它釋放資源。
reset()可以使播放器從Error狀態中恢複過來,重新會到Idle狀態。

4)設定播放器的監聽器:
MediaPlayer提供了一些設定不同監聽器的方法來更好地對播放器的工作狀態進行監聽,以期及時處理各種情況,
如: setOnCompletionListener(MediaPlayer.OnCompletionListener listener)、
setOnErrorListener(MediaPlayer.OnErrorListener listener)等,設定播放器時需要考慮到播放器可能出現的情況設定好監聽和處理邏輯,以保持播放器的健壯性。

相關文章

聯繫我們

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