Android MediaPlayer 簡單綜合應用——列出sdcard裡所有.mp3檔案,並且可以點擊播放!

來源:互聯網
上載者:User

大家好,我們今天要利用Android  MediaPlayer

 

Step 1:preparation work.

 

mksdcard 512M sdcard.img

 

create a new avd named AndroidSdcard

 

 

 

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 /sdcard

 

Step 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:

 

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!

相關文章

聯繫我們

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