Android音樂播放-MediaPlayerAndroid音樂播放-MediaPlayer

來源:互聯網
上載者:User

Android音樂播放-MediaPlayerAndroid音樂播放-MediaPlayer
當你坐公交無聊的時候,當你淹沒在地鐵中人潮中的時候,你是否想內心保持一份的安靜呢,那麼請帶上耳機,開啟你的音樂播放器,聽一首老歌帶你進入寧靜的世界,音樂播放這個功能在智能手機出現之前,諾基亞時代,甚至追溯到最開始的大哥大的時候都是屬於音訊範疇。Android中播放音頻不可避免的使用的一個類是Mediaplayer,視頻調用也是這個類。扯遠了,開始正題吧: 基礎維護 首先這個時候來看看要實現的效果吧:     布局如下:  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="com.example.googlemedia.MainActivity" >     <EditText        android:id="@+id/edit_musicPath"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="輸入你喜歡歌曲的路徑" />     <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >         <Button            android:id="@+id/btn_Play"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="playEvent"            android:text="播放" />         <Button            android:id="@+id/btn_Pause"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="pauseEvent"            android:text="暫停" />         <Button            android:id="@+id/btn_Stop"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="stopEvent"            android:text="停止" />         <Button            android:id="@+id/btn_Replay"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="replayEvent"            android:text="重播" />    </LinearLayout> </LinearLayout>Demo完成 音頻檔案:   播放按鈕事件:   public  void playEvent(View view){     editText=(EditText) findViewById(R.id.edit_musicPath);     String pathString=editText.getText().toString().trim();     File file=new File(pathString);     if (file.exists()) {         try {             mediaPlayer = new MediaPlayer();             mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);             mediaPlayer.setDataSource(pathString);             mediaPlayer.prepare();             mediaPlayer.start();             //多次點擊播放按鈕容易混音             btn_PlayButton.setEnabled(false);             //播放完之後需要回調的時候設定顯示             mediaPlayer.setOnCompletionListener(new OnCompletionListener() {                               @Override               public void onCompletion(MediaPlayer mp) {                   // TODO Auto-generated method stub                     btn_PlayButton.setEnabled(true);               }           });       } catch (IllegalArgumentException e) {           // TODO Auto-generated catch block           e.printStackTrace();       } catch (SecurityException e) {           // TODO Auto-generated catch block           e.printStackTrace();       } catch (IllegalStateException e) {           // TODO Auto-generated catch block           e.printStackTrace();       } catch (IOException e) {           // TODO Auto-generated catch block           e.printStackTrace();       }   }else {       Toast.makeText(this, "Sorry,你輸入的路徑有問題,請仔細檢查",Toast.LENGTH_SHORT).show();   }}   暫停和繼續事件: lic  void  pauseEvent(View view){    if ( btn_PauseButton.getText().equals("繼續")) {       mediaPlayer.start();        btn_PauseButton.setText("暫停");       return;   }    if (mediaPlayer!=null&&mediaPlayer.isPlaying()) {        mediaPlayer.pause();        btn_PauseButton.setText("繼續");   }}暫停和繼續效果:   停止事件:  public  void stopEvent(View view){     if (mediaPlayer!=null&&mediaPlayer.isPlaying()) {         btn_PlayButton.setEnabled(true);         mediaPlayer.stop();         //釋放mediaplayer否則的話會佔用記憶體         mediaPlayer.release();         mediaPlayer=null;    }     btn_PauseButton.setText("暫停");     btn_PlayButton.setEnabled(true); } 重播事件:  public  void  replayEvent(View view){    if (mediaPlayer!=null&&mediaPlayer.isPlaying()) {        mediaPlayer.seekTo(0);   }else {       playEvent(view);   }    //重播的時候應該設定播放的狀態    btn_PlayButton.setEnabled(true);}

聯繫我們

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