android音樂播放器(雛形)

來源:互聯網
上載者:User

丁浪建議:學習本執行個體之前,請掌握Activity的生命週期相關的事件和方法,這樣學習效果會更好。
 
本執行個體僅供參考學習,並非一款非常完善的產品。由於時間和本人技術有限,不足或者錯誤之處敬請諒解。希望熱心的網友能夠繼續完善。
下面是Activity部分代碼(我一般都會有詳細注釋):

package cn.chaoyang.activity; 
 
import java.io.File; 
import java.io.IOException; 
 
import android.app.Activity; 
import android.media.MediaPlayer; 
import android.os.Bundle; 
import android.os.Environment; 
import android.text.BoringLayout.Metrics; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
//學習本執行個體之前,請掌握Activity的生命週期和相關的方法,這樣學習效果會更好。 
public class MainActivity extends Activity { 
    private MediaPlayer mediaplayer; 
    private EditText txtName; 
    private int postion; 
    private String fileName; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        ButtonClickListener listener =new ButtonClickListener(); 
         txtName =(EditText)this.findViewById(R.id.inputName); 
        Button btnPlay =(Button)this.findViewById(R.id.btnPlay); 
        Button btnPause =(Button) this.findViewById(R.id.btnPause); 
        Button btnStop =(Button) this.findViewById(R.id.btnStop); 
        Button btnResart=(Button) this.findViewById(R.id.btnRestart); 
        btnPlay.setOnClickListener(listener); 
        btnPause.setOnClickListener(listener); 
        btnStop.setOnClickListener(listener); 
        btnResart.setOnClickListener(listener); 
    } 
    //當系統復原後,可以重新讀取出之前儲存的狀態值 
    @Override 
    protected void onRestoreInstanceState(Bundle savedInstanceState) { 
      this.fileName=savedInstanceState.getString("fileName"); 
      this.postion=savedInstanceState.getInt("postion"); 
      super.onRestoreInstanceState(savedInstanceState); 
    } 
  //當發生意外時,在系統將Activity的進程殺死之前,儲存一些狀態值 
    @Override 
    protected void onSaveInstanceState(Bundle outState) { 
        outState.putString("fileName", fileName); 
        outState.putInt("postion",postion); 
        super.onSaveInstanceState(outState); 
    } 
  //onDestroy方法可以殺掉程式的進程,徹底釋放資源 
    @Override 
    protected void onDestroy() { 
        mediaplayer.release(); 
        super.onDestroy(); 
    } 
   //如果打電話結束了,繼續播放音樂 
    @Override 
    protected void onResume() { 
        if(postion>0&&fileName!=null) 
        { 
            try { 
                play(); 
            } catch (IOException e) { 
                // TODO Auto-generated catch block 
                e.printStackTrace(); 
            } 
            mediaplayer.seekTo(postion); 
            postion=0; 
        } 
        super.onResume(); 
    } 
  //如果突然來電話或者來簡訊,Acticity會暫停,停止播放音樂 
    @Override 
    protected void onPause() { 
        if(mediaplayer.isPlaying()) 
        { 
            postion =mediaplayer.getCurrentPosition();//儲存當前播放點 
            mediaplayer.stop(); 
        } 
        super.onPause(); 
    } 
 
    private final class ButtonClickListener implements View.OnClickListener 
    { 
              
        @Override 
        public void onClick(View v) { 
            // TODO Auto-generated method stub 
            mediaplayer =new MediaPlayer(); 
             
            Button button =(Button) v ; 
            try { 
            switch (v.getId()) 
            { 
            //播放 
            case R.id.btnPlay: 
                if(!mediaplayer.isPlaying()) 
                { 
            play(); 
                } 
            break; 
            //暫停 
            case R.id.btnPause: 
                //如果現正播放,則按下按鈕後暫停.且按鈕上的文本顯示為"繼續“ 
                if(mediaplayer.isPlaying()) 
                { 
                    mediaplayer.pause(); 
                    button.setText(R.string.txtContinue);//設定按鈕文本 
                }else{ 
                    //如果是暫停狀態,按下按鈕後繼續播放 
                   //play(); 
                } 
                break; 
                //停止 
            case R.id.btnStop: 
                if(mediaplayer.isPlaying()){ 
                mediaplayer.stop(); 
                } 
                break; 
                 
                //重複 
            case R.id.btnRestart: 
                if(mediaplayer.isPlaying()){ 
                    mediaplayer.seekTo(0); 
                }else{ 
                    play(); 
                } 
                break; 
            } 
            }catch (Exception e) { 
                // TODO: handle exception 
            } 
             
             
        } 
 
    } 
    private void play() throws IOException 
    { 
        //獲得音樂檔案的絕對路徑 
         fileName=txtName.getText().toString(); 
        File file =new File(Environment.getExternalStorageDirectory(),fileName); 
        mediaplayer.reset();//歸位 
        mediaplayer.setDataSource(file.getAbsolutePath());//設定需要播放的資料來源 
        mediaplayer.prepare(); 
        mediaplayer.start(); 
    } 

下面是軟體布局檔案代碼,很簡單的線性布局

<?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="@string/hello" 
    /> 
    <TextView   
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:text="@string/labName" 
    /> 
    <EditText 
     android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:id="@+id/inputName" 
    /> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <Button 
     android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:text="@string/play" 
    android:id="@+id/btnPlay" 
    /> 
     <Button 
     android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:text="@string/pause" 
    android:id="@+id/btnPause" 
    /> 
     <Button 
     android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:text="@string/stop" 
    android:id="@+id/btnStop" 
    /> 
     <Button 
     android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:text="@string/restart" 
    android:id="@+id/btnRestart" 
    /> 
    </LinearLayout> 
</LinearLayout> 

下面是資源檔string.xml代碼

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <string name="hello">Hello World, MainActivity!</string> 
    <string name="app_name">mp3播放器</string> 
    <string name="labName">輸入歌名</string> 
    <string name="play">播放</string> 
    <string name="pause">暫停</string> 
    <string name="stop">停止</string> 
    <string name="restart">重複</string> 
     <string name="txtContinue">繼續</string> 
</resources> 

 
 
本執行個體的目的,是為了熟悉android中音頻介面的使用及相關操作,鞏固Activity生命週期的相關知識。至於頁面配置部分,採用的是非常傻瓜式的顯示和控制風格。
如果想要開發一款完善的(音樂播放器相關的)產品,還有太多太多的地方需要完善

摘自:編程世界一凡人

聯繫我們

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