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

主界面配置文件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();
}
//以下方法會(huì)造成只要應(yīng)用在后臺(tái)音樂(lè)都會(huì)停止播放
@Override
//當(dāng)應(yīng)用不在前臺(tái)時(shí),停止播放
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()檢查外部存儲(chǔ)設(shè)備的可用性
File audio=new File(Environment.getExternalStorageDirectory(),filename);
if(audio.exists()){
//獲取SDCard目錄,2.2的時(shí)候?yàn)?/mnt/sdcart 2.1的時(shí)候?yàn)椋?sdcard,所以使用靜態(tài)方法得到路徑會(huì)好一點(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);//從開(kāi)始位置播放
}else{
if(path!=null){
play();
}
}
break;
case R.id.stopbutton:
if(mediaplayer.isPlaying()){
mediaplayer.stop();
}
break;
default:
break;
}
}
private void play() {
try {
mediaplayer.reset();//把各項(xiàng)參數(shù)恢復(fù)到初始化狀態(tài)
mediaplayer.setDataSource(path);
mediaplayer.prepare();//進(jìn)行緩沖
//設(shè)置緩沖監(jiān)聽(tīng)器
mediaplayer.setOnPreparedListener(new OnPreparedListener() {
//緩沖完畢后調(diào)用onPrepared方法
public void onPrepared(MediaPlayer mp) {
// 里面寫緩沖完要干的事
mediaplayer.start();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
實(shí)現(xiàn)了簡(jiǎn)單的SD卡中音樂(lè)的播放。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android利用代碼控制設(shè)備上其他音樂(lè)播放器的方法
- iOS音樂(lè)播放器實(shí)現(xiàn)代碼完整版
- 基于vue-element組件實(shí)現(xiàn)音樂(lè)播放器功能
- Android版音樂(lè)播放器
- android實(shí)現(xiàn)音樂(lè)播放器進(jìn)度條效果
- vue一個(gè)頁(yè)面實(shí)現(xiàn)音樂(lè)播放器的示例
- 原生JS實(shí)現(xiàn)網(wǎng)頁(yè)手機(jī)音樂(lè)播放器 歌詞同步播放的示例
- python使用Tkinter實(shí)現(xiàn)在線音樂(lè)播放器
- python實(shí)現(xiàn)簡(jiǎn)易云音樂(lè)播放器
- C語(yǔ)言音樂(lè)播放器實(shí)例代碼
相關(guān)文章
Android浮動(dòng)窗口實(shí)現(xiàn)原理及代碼實(shí)例
這篇文章主要介紹了Android浮動(dòng)窗口實(shí)現(xiàn)原理及代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
詳解android與百度echarts項(xiàng)目整合方法
在本篇文章里我們給大家分享了關(guān)于android與百度echarts項(xiàng)目整合方法和具體步驟,需要的朋友們跟著學(xué)習(xí)下。2019-03-03
使用ListView實(shí)現(xiàn)網(wǎng)上訂餐首頁(yè)
這篇文章主要為大家詳細(xì)介紹了使用ListView實(shí)現(xiàn)網(wǎng)上訂餐首頁(yè),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-01-01
Android編程開(kāi)發(fā)之打開(kāi)文件的Intent及使用方法
這篇文章主要介紹了Android編程開(kāi)發(fā)之打開(kāi)文件的Intent及使用方法,已實(shí)例形式分析了Android打開(kāi)文件Intent的相關(guān)布局及功能實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
Android中Activity的四種啟動(dòng)模式和onNewIntent()
android 中activity的啟動(dòng)模式分為四種,(standard、singleTop、singTask、singleInstance),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2018-08-08
如何在Android Studio下進(jìn)行NDK開(kāi)發(fā)
這篇文章主要介紹了如何在Android Studio下進(jìn)行NDK開(kāi)發(fā),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
Android中的TimePickerView(時(shí)間選擇器)的用法詳解
這篇文章主要介紹了Android中的TimePickerView時(shí)間選擇器的用法,這是一個(gè)第三方從底部彈出來(lái)的日期選擇器,文中結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04
Android6.0獲取GPS定位和獲取位置權(quán)限和位置信息的方法
今天小編就為大家分享一篇Android6.0獲取GPS定位和獲取位置權(quán)限和位置信息的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07

