(Android小應用)音樂播放器

來源:互聯網
上載者:User

這個小應用確實是小哈,僅做為學習之用,瞭解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,呵呵,本來功能就不複雜的,僅作為學習之用

相關文章

聯繫我們

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