大家好,我们今天要利用Android MediaPlayerStep 1:preparation work.mksdcard 512M sdcard.imgcreate a new avd named AndroidSdcardAndroid MediaPlayer 简单综合应用_第1张图片push songs into sdcard(before you push,you make sure your avd is running,else the operation of push will not work):adb push f:\music\1.mp3 /sdcardStep 2: Layout UI desigen:create two .xml files we called song_item.xml and songlist.xml the code are:song_item.xml:
< ?xml version ="1.0" encoding ="utf-8"? > < TextView android:id ="@+id/text1" xmlns:android ="http://schemas.android.com/apk/res/android" android:layout_width ="wrap_content" android:layout_height ="wrap_content" />songlist.xml:
< ?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" > < ListView android:id ="@id/android:list" android:layout_width ="fill_parent" android:layout_height ="fill_parent" android:layout_weight ="1" android:drawSelectorOnTop ="false" /> < TextView android:id ="@id/android:empty" android:layout_width ="fill_parent" android:layout_height ="fill_parent" android:text ="No songs found on SD Card." /> </LinearLayout>Step 3: the core code MeusicDemo.java:
        
  1. package com.android.test; import java.io.File; import java.io.FilenameFilter; import java.io.IOException; import java.util.ArrayList; import java.util.List; import android.app.ListActivity; import android.media.MediaPlayer; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListView; class Mp3Filter implements FilenameFilter { public boolean accept(File dir, String name) { return (name.endsWith( ".mp3"));}} public class MeusicDemo extends ListActivity { private static final String MEDIA_PATH = new String( "/sdcard/"); private List<String> songs = new ArrayList<String>(); private MediaPlayer mp = new MediaPlayer();@Override public void onCreate(Bundle icicle) { try { super.onCreate(icicle); setContentView(R.layout.songlist); updateSongList();} catch (NullPointerException e) { Log.v(getString(R.string.app_name), e.getMessage());}} public void updateSongList() { File home = new File(MEDIA_PATH); if (home.listFiles( new Mp3Filter()).length > 0) { for (File file : home.listFiles( new Mp3Filter())) { songs.add(file.getName());}ArrayAdapter<String> songList = new ArrayAdapter<String>( this,R.layout.song_item,songs);setListAdapter(songList);} } @Override protected void onListItemClick(ListView l, View v, int position, long id) { try {mp.reset(); mp.setDataSource(MEDIA_PATH + songs.get(position)); mp.prepare(); mp.start();} catch(IOException e) { Log.v(getString(R.string.app_name), e.getMessage());}}}
Step 4:run it.the result like this:now u can enjoy the music.lol~if u wanna the source code please leave your email address, i will send u!

更多相关文章

  1. Android拷贝图片到指定文件路径
  2. android 关于图片的放大,缩小,旋转功能的实现(附源码)
  3. Android Location 和 Sensor的使用,图片旋转角度
  4. Android 图片拖动和缩放
  5. android 背景圆角以及图片圆角处理
  6. Android浏览器显示大分辨率图片的问题 分享
  7. Android for opencv(1)android使用opencv基本操作:读写 图片,操作像
  8. Android浏览器显示大分辨率图片的问题
  9. android 网络访问-图片处理优秀开源项目

随机推荐

  1. Android电源管理
  2. 用Android模拟器体验angry birds
  3. 64位 ubuntu android SDK 无法新建AVD,adb
  4. iOS从业者的Android初体验
  5. Android(安卓)- 主要的UI元素。
  6. 利用 RDP Wrapper 实现 Android(安卓)平
  7. Android随笔之——Android时间、日期相关
  8. Android获取外置SD卡读写路径
  9. Android(安卓)adb 我的常用调试命令记录(
  10. Android系统源码目录解析