大家知道AndroidMediaPlayer播放mp3的實例嗎?下面我們就給大家詳細介紹一下吧!我們積累了一些經驗,在此拿出來與大家分享下,請大家互相指正。
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/white"> <TextView android:id="@+id/myTextView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" > </TextView> <ImageButton android:id="@+id/myButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/play" android:layout_below="@+id/myTextView1" > </ImageButton> <ImageButton android:id="@+id/myButton3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/pause" android:layout_alignTop="@+id/myButton1" android:layout_toRightOf="@+id/myButton1" > </ImageButton> <ImageButton android:id="@+id/myButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/stop" android:layout_alignTop="@+id/myButton1" android:layout_toRightOf="@+id/myButton3" > </ImageButton> </RelativeLayout>
Step 4 :主控制程序MediaPlayerDemo.java的實現,代碼如下:
package com.android.test; import android.app.Activity; import android.media.MediaPlayer; import android.os.Bundle; import android.view.View; import android.widget.ImageButton; import android.widget.TextView; public class MediaPlayerDemo extends Activity { private ImageButton mb1,mb2,mb3; private TextView tv; private MediaPlayer mp; //聲明一個變量判斷是否為暫停,默認為false private boolean isPaused = false; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //通過findViewById找到資源 mb1 = (ImageButton)findViewById(R.id.myButton1); mb2 = (ImageButton)findViewById(R.id.myButton2); mb3 = (ImageButton)findViewById(R.id.myButton3); tv = (TextView)findViewById(R.id.myTextView1); //創建MediaPlayer對象,將raw文件夾下的lovefool.mp3 mp = MediaPlayer.create(this,R.raw.lovefool); //增加播放音樂按鈕的事件 @Override public void onClick(View v) { try { if(mp != null) { mp.stop(); } mp.prepare(); mp.
相信大家已經學會AndroidMediaPlayer播放mp3的實例了吧!感謝大家對我們網站的支持!
相關推薦:
android動態更改屏幕方向的代碼介紹