Android媒體開發(fā)之音樂播放器
本文實例為大家分享了Android媒體開發(fā)之音樂播放器的具體代碼,供大家參考,具體內(nèi)容如下
可以對音樂文件實現(xiàn)播放、暫停、重播和停止功能。退出應(yīng)用和回到桌面時音樂停止。
主界面:

主界面配置文件mian.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.musicplay.MainActivity" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/filename" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.00"
android:background="#B0C4DE"
android:text="Payphone.mp3"
android:id="@+id/filename" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="@+id/playbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="mediaplay"
android:text="@string/playbutton" />
<Button
android:id="@+id/pausebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="mediaplay"
android:text="@string/pausebutton" />
<Button
android:id="@+id/resetbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="mediaplay"
android:text="@string/resetbutton" />
<Button
android:id="@+id/stopbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="mediaplay"
android:text="@string/stopbutton" />
</LinearLayout>
</LinearLayout>
主界面的Activity
MainActivity.java:
package com.example.musicplay;
import java.io.File;
import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.os.Environment;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText nameText;
private String path;
private int position;
private MediaPlayer mediaplayer;
private boolean pause;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameText=(EditText) this.findViewById(R.id.filename);
mediaplayer=new MediaPlayer();
}
//以下方法會造成只要應(yīng)用在后臺音樂都會停止播放
@Override
//當(dāng)應(yīng)用不在前臺時,停止播放
protected void onPause() {
if(mediaplayer.isPlaying()){
position=mediaplayer.getCurrentPosition();
mediaplayer.stop();
}
super.onPause();
}
@Override
protected void onResume() {
if(position>0&&path!=null){
play();
mediaplayer.seekTo(position);
position=0;
}
super.onResume();
}
@Override
protected void onDestroy() {
mediaplayer.release();
mediaplayer=null;
super.onDestroy();
}
public void mediaplay(View v){
switch (v.getId()) {
case R.id.playbutton:
String filename=nameText.getText().toString();
//Environment.getExternalStorageDirectory()檢查外部存儲設(shè)備的可用性
File audio=new File(Environment.getExternalStorageDirectory(),filename);
if(audio.exists()){
//獲取SDCard目錄,2.2的時候為:/mnt/sdcart 2.1的時候為:/sdcard,所以使用靜態(tài)方法得到路徑會好一點(diǎn)。
path=audio.getAbsolutePath();
play();
}
else{
path=null;
Toast.makeText(getApplicationContext(), R.string.error, 1).show();
}
break;
case R.id.pausebutton:
if(mediaplayer.isPlaying()){
mediaplayer.pause();
pause=true;
((Button)v).setText(R.string.continues);
}else{
if(pause){
mediaplayer.start();
pause=false;
((Button)v).setText(R.string.pausebutton);
}
}
break;
case R.id.resetbutton:
if(mediaplayer.isPlaying()){
mediaplayer.seekTo(0);//從開始位置播放
}else{
if(path!=null){
play();
}
}
break;
case R.id.stopbutton:
if(mediaplayer.isPlaying()){
mediaplayer.stop();
}
break;
default:
break;
}
}
private void play() {
try {
mediaplayer.reset();//把各項參數(shù)恢復(fù)到初始化狀態(tài)
mediaplayer.setDataSource(path);
mediaplayer.prepare();//進(jìn)行緩沖
//設(shè)置緩沖監(jiān)聽器
mediaplayer.setOnPreparedListener(new OnPreparedListener() {
//緩沖完畢后調(diào)用onPrepared方法
public void onPrepared(MediaPlayer mp) {
// 里面寫緩沖完要干的事
mediaplayer.start();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
實現(xiàn)了簡單的SD卡中音樂的播放。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android apk 項目一鍵打包并上傳到蒲公英的實現(xiàn)方法
這篇文章主要介紹了Android apk 項目一鍵打包并上傳到蒲公英,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06
關(guān)于Android?Webview?設(shè)置Cookie問題詳解
大家好,本篇文章是關(guān)于Android?Webview?設(shè)置Cookie問題詳解,感興趣的同學(xué)可以看看,希望對你起到幫助,有用的話記得收藏,方便下次瀏覽2021-11-11
Android 快速使用正則表達(dá)式,校驗身份證號的實例
下面小編就為大家分享一篇Android 快速使用正則表達(dá)式,校驗身份證號的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
android UI進(jìn)階之a(chǎn)ndroid中隱藏的layout 抽屜的使用方法
android UI進(jìn)階之a(chǎn)ndroid中隱藏的layout 抽屜的使用方法,需要的朋友可以參考一下2013-05-05
Android的HTTP擴(kuò)展包OkHttp中的緩存功能使用方法解析
OkHttp(GitHub主頁https://github.com/square/okhttp)是一款高人氣的第三方Android網(wǎng)絡(luò)編程包,這里我們來看一下Android的HTTP擴(kuò)展包OkHttp中的緩存功能使用方法解析:2016-07-07

