国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

Android媒體開發(fā)之音樂(lè)播放器

2019-10-22 18:18:22
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例為大家分享了Android媒體開發(fā)之音樂(lè)播放器的具體代碼,供大家參考,具體內(nèi)容如下

可以對(duì)音樂(lè)文件實(shí)現(xiàn)播放、暫停、重播和停止功能。退出應(yīng)用和回到桌面時(shí)音樂(lè)停止。

主界面:

 Android,媒體,音樂(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);//從開始位置播放   }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)聽器   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í)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到Android開發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 娄底市| 福安市| 敖汉旗| 河间市| 北宁市| 辽宁省| 和田县| 临桂县| 阿瓦提县| 东乌| 娄底市| 环江| 建湖县| 中西区| 资源县| 林芝县| 绩溪县| 鞍山市| 睢宁县| 上杭县| 铁岭市| 永川市| 龙门县| 崇义县| 铁岭县| 开化县| 阜平县| 利川市| 乌鲁木齐县| 闽清县| 双牌县| 子长县| 武城县| 鹰潭市| 宜春市| 谷城县| 威海市| 社会| 奇台县| 灌云县| 东阳市|