編輯:關於Android編程
本部分代碼在《Android應用開發揭秘》中提到,但是在eclipse環境下調試時出現異常,幾番糾結,代碼終於可以播放器音樂、並成功移植到手機上......
package com.example.calculate; import java.io.File; import java.io.FilenameFilter; import java.io.IOException; import java.util.ArrayList; import java.util.List; import android.annotation.SuppressLint; import android.app.ListActivity; import android.media.MediaPlayer; import android.media.MediaPlayer.OnCompletionListener; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.widget.ArrayAdapter; import android.widget.ImageButton; import android.widget.ListView; @SuppressLint(SdCardPath) public class Music extends ListActivity{ private static ImageButton mFrontImageButton = null; private static ImageButton mStopImageButton = null; private static ImageButton mPlayImageButton = null; private static ImageButton mPauseImageButton = null; private static ImageButton mNextImageButton = null; //MediaPlayer對象 private MediaPlayer mMediaPlayer = null; //歌曲播放列表 private ListmMusicList = new ArrayList (); //當前歌曲播放索引 private int currentListItem = 0; //音樂路徑 // private static final String MUSIC_PATH = new String(/sdcard/); //該路徑是用於eclipse 下仿真用的 private static final String MUSIC_PATH = new String(/storage/sdcard1/qqmusic/song/); //該路徑是用於手機,這個路徑是我手機歌曲的路徑 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.music); // setContentView(R.layout.activity_calculate); mMediaPlayer = new MediaPlayer(); //更新播放列表 musicList(); mStopImageButton = (ImageButton)findViewById(R.id.imageButton2); mPlayImageButton = (ImageButton)findViewById(R.id.imageButton3); mPlayImageButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { playMusic(MUSIC_PATH + mMusicList.get(currentListItem)); } }); mPauseImageButton = (ImageButton)findViewById(R.id.imageButton4); mPauseImageButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { if (mMediaPlayer.isPlaying()) mMediaPlayer.stop(); else mMediaPlayer.start(); } }); mNextImageButton = (ImageButton)findViewById(R.id.imageButton5); mNextImageButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { nextMusic(); } }); mFrontImageButton = (ImageButton)findViewById(R.id.imageButton1); mFrontImageButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { FrontMusic(); } }); listener(); } //監聽事件 void listener(){ //停止 mStopImageButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if(mMediaPlayer.isPlaying()){ mMediaPlayer.reset(); } } }); } public void onClick(View view) { if (view == mFrontImageButton){ }else if (view == mStopImageButton) { if (mMediaPlayer.isPlaying()) { mMediaPlayer.reset(); } }else if (view == mPlayImageButton) { playMusic(MUSIC_PATH+mMusicList.get(currentListItem)); }else if (view == mPauseImageButton) { if (mMediaPlayer.isPlaying()) mMediaPlayer.stop(); else mMediaPlayer.start(); }else if (view == mNextImageButton) { nextMusic(); }else if (view == mFrontImageButton) { FrontMusic(); } } public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { mMediaPlayer.stop(); mMediaPlayer.release(); this.finish(); return true; } return super.onKeyDown(keyCode, event); } protected void onListItemClick(ListView l, View v, int position, long id) { currentListItem = position; playMusic(MUSIC_PATH+mMusicList.get(currentListItem)); } public void playMusic(String path) { try { //mMediaPlayer在重新播放一首歌曲之前要之行下面的操縱, //reset()重置, //setDataSource(path)播放歌曲路徑. //prepare()、start() 准備、播放. mMediaPlayer.reset(); mMediaPlayer.setDataSource(path); mMediaPlayer.prepare(); mMediaPlayer.start(); mMediaPlayer.setOnCompletionListener(new OnCompletionListener() { public void onCompletion(MediaPlayer arg0) { //播放完一首歌曲之後進行下一首 nextMusic(); } }); }catch (IOException e) { e.printStackTrace(); } } public void nextMusic() { if (++currentListItem >= mMusicList.size()) { currentListItem = 0; }else { playMusic(MUSIC_PATH+mMusicList.get(currentListItem)); } } public void FrontMusic() { if (--currentListItem >= mMusicList.size()) { currentListItem = 0; }else { playMusic(MUSIC_PATH+mMusicList.get(currentListItem)); } } //綁定音樂 void musicList(){ File home=new File(MUSIC_PATH); if(home.listFiles(new MusicFilter()).length>0){ for(File file:home.listFiles(new MusicFilter())){ mMusicList.add(file.getName()); } ArrayAdapter musicList=new ArrayAdapter (Music.this,R.layout.c1, mMusicList); setListAdapter(musicList); } } } /* 過濾文件類型 */ class MusicFilter implements FilenameFilter { public boolean accept(File dir, String name) { //這裡還可以設置其他格式的音樂文件 return (name.endsWith(.mp3)); } }
以下是music.xml界面布局
手機運行的效果:
過程很糾結,堅持!過程問題越多,受教越廣,去享受那份糾結吧。。。。
1. 網頁源碼查看器網頁源碼查看器案例實現在EditText中輸入網址,點擊按鈕獲取,獲取到網頁源碼,顯示在TextView上。在IE浏覽器中,快捷鍵Shift+F12可
1.普通側滑效果圖:思路:通過自定義View繼承HorizontalScrollView,然後重寫onMeasure(),onLayout(),onTouchEvent(
我們在ListView中需要下載資源時,贊不考慮緩存機制,那麼每一個Item可能都需要開啟一個線程去下載資源(如果沒有線程池),如果Item很多,那麼我們可能就會無限制的
android Fragments詳解 Fragment是activity的界面中的一部分或一種行為。你可以把多個Fragment們組合到一個activity中