編輯:關於Android編程
已經好幾天不更新博客了,今天輕松一點模擬個簡單的“音樂播放器”。1分鐘看完~
整個簡單布局,加幾個控制按鈕:
package com.example.playaudiotest; import java.io.File; import android.app.Activity; import android.media.MediaPlayer; import android.os.Bundle; import android.os.Environment; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity implements OnClickListener { private Button play; private Button pause; private Button stop; private MediaPlayer mediaPlayer = new MediaPlayer(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); play = (Button) findViewById(R.id.play); pause = (Button) findViewById(R.id.pause); stop = (Button) findViewById(R.id.stop); play.setOnClickListener(this); pause.setOnClickListener(this); stop.setOnClickListener(this); initMediaPlayer(); } private void initMediaPlayer() { try { File file = new File(Environment.getExternalStorageDirectory(), "music.mp3"); //設置播放路徑,資源 mediaPlayer.setDataSource(file.getPath()); //播放前的准備工作 mediaPlayer.prepare(); } catch (Exception e) { e.printStackTrace(); } } @Override public void onClick(View v) { switch (v.getId()) { case R.id.play: if (!mediaPlayer.isPlaying()) { //開始播放 mediaPlayer.start(); } break; case R.id.pause: if (mediaPlayer.isPlaying()) { //暫停播放 mediaPlayer.pause(); } break; case R.id.stop: if (mediaPlayer.isPlaying()) { //Resets the MediaPlayer to its uninitialized state. After calling this method, you will have to initialize it again by setting the data source and calling prepare(). //重置,如果調用此方法,必須人為的再次調用設置資源、准備方法 mediaPlayer.reset(); initMediaPlayer(); } break; default: break; } } @Override protected void onDestroy() { super.onDestroy(); if (mediaPlayer != null) { mediaPlayer.stop(); mediaPlayer.release(); } } }
Resets the MediaPlayer to its uninitialized state. After calling this method, you will have to initialize it again by setting the data source and calling prepare().
大致意思是:重置,如果調用此方法,必須人為的再次調用設置資源、准備方法
既然調用了就在此調用initMediaPlayer();
運行,開始聽音樂吧~~
本文gif圖比較多,可能會導致頁面加載緩慢,請大家耐心等待今天我們來學習下Android Studio這款軟件的一些秘密,這些就是Template,就是我們輸出一些特定
目錄:? UIScrollView的常見屬性 ? UIScrollView的常用代理方法 ? UIScrollView的縮放 ? UIScr
1、概述Android提供了幾種動畫類型:View Animation 、Drawable Animation 、Property Animation 。View An
我們通常在TextView文本中設置文字。可是如何設置圖文混排呢?我就在這裡寫一個例子 。我們需要用到一點簡單的HTML知識在TextView中預訂了一些類似HTML的標