22_Android中的本地音樂播放器和網路音樂播放器的編寫,本地視訊播放器和網路視訊播放器,照相機案例,偷拍案例實現
1 編寫以下案例:
當點選了”播放”之後,在手機上的/mnt/sdcard2/natural.mp3就會播放。
2 編寫佈局檔案activity_main.xml
<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 <EditText android:id="@+id/et_path" android:layout_width="fill_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 android:onClick="play" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:text="播放" /> <Button android:id="@+id/bt_pause" android:onClick="pause" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:text <Button android:id="@+id/bt_stop" android:onClick="stop" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:text="停止" /> <Button android:id="@+id/bt_replay" android:onClick="replay" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:text="重播" /> </LinearLayout> </LinearLayout> |
3 編寫MainActivity
package com.itheima.musicplayer; import java.io.File; import java.io.IOException; import android.app.Activity; import android.media.AudioManager; import android.media.MediaPlayer; import android.media.MediaPlayer.OnCompletionListener; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; publicclass MainActivity extends Activity { private EditText et_path; private MediaPlayer mediaPlayer; private Button bt_play,bt_pause,bt_stop,bt_replay; @Override protectedvoid onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_path = (EditText) findViewById(R.id.et_path); bt_play = (Button) findViewById(R.id.bt_play); bt_pause = (Button) findViewById(R.id.bt_pause); bt_stop = (Button) findViewById(R.id.bt_stop); bt_replay = (Button) findViewById(R.id.bt_replay); } /** * 播放 * @param view */ publicvoid play(View view) { String filepath = et_path.getText().toString().trim(); File file = new File(filepath); if(file.exists()){ try { mediaPlayer = new MediaPlayer(); mediaPlayer.setDataSource(filepath);//設定播放的資料來源。 mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.prepare();//準備開始播放播放的邏輯是c程式碼在新的執行緒裡面執行。 mediaPlayer.start(); bt_play.setEnabled(false); mediaPlayer.setOnCompletionListener(new OnCompletionListener() { @Override publicvoid onCompletion(MediaPlayer mp) { bt_play.setEnabled(true); } }); } catch (Exception e) { e.printStackTrace(); Toast.makeText(this, "播放失敗", 0).show(); } }else{ Toast.makeText(this, "檔案不存在,請檢查檔案的路徑", 0).show(); } } /** * 暫停 * @param view */ publicvoid pause(View view) { if("繼續".equals(bt_pause.getText().toString())){ mediaPlayer.start(); bt_pause.setText("暫停"); return; } if(mediaPlayer!=null&&mediaPlayer.isPlaying()){ //這裡表示的是暫停功能 mediaPlayer.pause(); bt_pause.setText("繼續"); } } /** * 停止 * @param view */ publicvoid stop(View view) { if(mediaPlayer!=null&&mediaPlayer.isPlaying()){ //通過stop方法停止播放音樂 mediaPlayer.stop(); mediaPlayer.release(); mediaPlayer = null; } bt_pause.setText("暫停"); bt_play.setEnabled(true); } /** * 重播 * @param |