Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發實例 >> (Android小應用)音樂播放器

(Android小應用)音樂播放器

編輯:Android開發實例

這個小應用確實是小哈,僅做為學習之用,了解MediaPlayer類,以後再做個稍微大點的,打算從網絡上直接下載歌曲和歌詞文件,並動態顯示歌詞,這個小應用先不實現那些吧,以後補上……

界面:

<?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="音樂文件路徑"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/filename"
android:text="test.mp3"
/>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/startButton"
android:text="播放"
android:textSize="15px"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/startButton"
android:layout_alignTop="@id/startButton"
android:layout_marginLeft="10px"
android:id="@+id/pauseButton"
android:text="暫停"
android:textSize="15px"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/pauseButton"
android:layout_alignTop="@id/pauseButton"
android:layout_marginLeft="10px"
android:id="@+id/stopButton"
android:text="停止"
android:textSize="15px"
/>
</RelativeLayout>
</LinearLayout>

很簡單的布局

AudioActivity:

package com.studio.audio;

import java.io.IOException;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class AudioActivity extends Activity {
/** Called when the activity is first created. */
private MediaPlayer mediaPlayer = null;
private Button startButton = null;
private Button pauseButton = null;
private Button stopButton = null;
private EditText editText = null;

private boolean isPause = false;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

editText = (EditText) findViewById(R.id.filename);
startButton = (Button) findViewById(R.id.startButton);
pauseButton = (Button) findViewById(R.id.pauseButton);
stopButton = (Button) findViewById(R.id.stopButton);

startButton.setOnClickListener(listener);
pauseButton.setOnClickListener(listener);
stopButton.setOnClickListener(listener);

mediaPlayer = new MediaPlayer();
mediaPlayer
.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
Toast.makeText(AudioActivity.this, "播放完成",
Toast.LENGTH_LONG).show();
}
});
}

private OnClickListener listener = new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Button button = (Button) v;
switch (button.getId()) {
case R.id.startButton:
try {
String fileName = editText.getText().toString();
mediaPlayer.reset();
mediaPlayer.setDataSource("/sdcard/" + fileName);
mediaPlayer.prepare();
mediaPlayer.start();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case R.id.pauseButton:
try {
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
} else {
mediaPlayer.start();
}
} catch (Exception e) {
// TODO: handle exception
}
break;
case R.id.stopButton:
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
break;

default:
break;
}
}
};

@Override
protected void onPause() {
// TODO Auto-generated method stub
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
isPause = true;
}
super.onPause();
}

@Override
protected void onResume() {
// TODO Auto-generated method stub
if (isPause) {
mediaPlayer.start();
isPause = false;
}
super.onResume();
}

@Override
protected void onDestroy() {
// TODO Auto-generated method stub
mediaPlayer.release();
mediaPlayer = null;
super.onDestroy();
}

}

因為我們這裡用的sdcard裡面的音樂文件,所以我們得先往sdcard裡面放首音樂

 

選擇“sdcard",然後點擊右上角第二個手機圖標(指向手機裡面的那個)就可以將本地的文件傳到sdcard裡面,注意名字的關聯,因為程序裡面定義的名字是test.mp3,呵呵,本來功能就不復雜的,僅作為學習之用

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved