MediaPlayer類支持MP三、3GP等音樂格式。使用該類能夠製做一個簡單的音樂播放器。下面的示例展現瞭如何播放網絡中的一首歌曲,爲了方面,這個歌曲服務器能夠在本身的機器上搭建。筆者使用的是android4。 java
不做贅述。 android
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.HelloWorld" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="14"/> <uses-permission android:name="android.permission.INTERNET" /> <application android:label="@string/app_name" android:icon="@drawable/ic_launcher"> <activity android:name="MyActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> </manifest>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="這是一個音樂播放器" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="暫停" android:id="@+id/button_pause"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="播放" android:id="@+id/button_play"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="從新播放" android:id="@+id/button_replay"/> </LinearLayout>這裏使用了LinearLayout佈局,三個按鈕的做用如其屬性android:text所言。效果圖以下:
package com.example.HelloWorld; import android.app.Activity; import android.media.MediaPlayer; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; import java.io.IOException; public class MyActivity extends Activity { private MediaPlayer mp; private String url = "http://example.com/songs/test/love-story.mp3"; /** * Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final Button playButton = (Button) findViewById(R.id.button_play); final Button replayButton = (Button) findViewById(R.id.button_replay); final Button pauseButton = (Button) findViewById(R.id.button_pause); mp = MediaPlayer.create(this, Uri.parse(this.url)); playButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mp.start(); } }); pauseButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mp.isPlaying()) { mp.pause(); } else { Toast.makeText(MyActivity.this, "您還沒播放呢", Toast.LENGTH_SHORT).show(); } } }); replayButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mp.reset(); try { mp.setDataSource(MyActivity.this, Uri.parse(url)); mp.prepare(); mp.start(); } catch (IOException e) { e.printStackTrace(); } } }); } @Override protected void onDestroy() { if (mp.isPlaying()) { mp.stop(); } mp.release(); super.onDestroy(); } }
mp = MediaPlayer.create(this, Uri.parse(this.url));