淺入淺出Android(005):使用MediaPlayer製做簡單的音樂播放器,播放網絡音樂

MediaPlayer類支持MP三、3GP等音樂格式。使用該類能夠製做一個簡單的音樂播放器。下面的示例展現瞭如何播放網絡中的一首歌曲,爲了方面,這個歌曲服務器能夠在本身的機器上搭建。筆者使用的是android4。 java

一、建立項目

不做贅述。 android

二、修改AndroidManifest.xml,添加INTERNET權限


<?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>

三、修改佈局文件layout\main.xml:


<?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所言。效果圖以下:


四、主要Java代碼:

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));



建立 MediaPlayer對象並裝載了網絡上的一個音頻,固然也能夠指定爲手機上存儲的一個音頻。

點擊「播放」按鈕時會調用 start()方法開始播放音樂。 點擊「暫停「按鈕時,先檢查有沒有在播放,若播放了在使用pause()暫停播放,不然提示」您還沒播放呢「。 而點擊」從新播放「按鈕時先依次使用reset()和setDataSource()方法重置要播放的音樂。在start()以前須要prepare()。即在使用setDataSource()方法裝載音頻後必須使用prepare()方法,這纔算是真正的裝載。 另外在重寫的onDestroy()方法中,檢查了音樂是否在播放,播放的話就暫停音樂,並release()。
相關文章
相關標籤/搜索