android:music

   

 package com.terry;

 import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import android.app.ListActivity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast; public class myActivity2 extends ListActivity implements OnClickListener {
Button play;
Button pause;
Button next;
Button last;
// ListView listViews;
List<String> list;
MediaPlayer mediaplayer;
String PATH_ROOT = "/sdcard/";
//当前项目名称
String CURRENT_ITME = "";
//当前位置
int CURRENT_POSITON = 0; @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// Intent intent = new Intent();
setContentView(R.layout.main2); initViews();
initListViews();
mediaplayer = new MediaPlayer();
} private void initViews() {
// TODO Auto-generated method stub
play = (Button) findViewById(R.id.start2);
pause = (Button) findViewById(R.id.pause2);
next = (Button) findViewById(R.id.next2);
last = (Button) findViewById(R.id.last2);
play.setOnClickListener(this);
pause.setOnClickListener(this);
next.setOnClickListener(this);
last.setOnClickListener(this);
} private void initListViews() {
// 初始化ListViews
// listViews
list = new ArrayList<String>();
ArrayAdapter<String> musiclistadapter;
File file = new File(PATH_ROOT);
if (file != null) {
if (file.isDirectory()) {
File[] listfiles = file.listFiles();
if (listfiles != null) {
for (int i = 0; i < listfiles.length; i++) {
String name = listfiles[i].getName();
if (name.endsWith(".mp3")) {
list.add(name);
}
}
}
}
}
musiclistadapter = new ArrayAdapter<String>(this, R.layout.musicitme,
list);
setListAdapter(musiclistadapter);
} @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.start2:
// mediaplayer.p
playMusic(PATH_ROOT + CURRENT_ITME); break; case R.id.pause2:
if (mediaplayer.isPlaying()) {
mediaplayer.pause();
} else {
Toast.makeText(this, "没有正在播放的音乐,我自动播放咯。。。。", 100).show();
playMusic(PATH_ROOT + CURRENT_ITME);
}
break;
case R.id.next2:
System.out.println("xiayishou....");
CURRENT_POSITON += 1;
CURRENT_ITME = list.get(CURRENT_POSITON);
playMusic(PATH_ROOT + CURRENT_ITME);
System.out.println("path:" + PATH_ROOT + CURRENT_ITME);
Toast.makeText(this, "下一首音乐,path:" + PATH_ROOT + CURRENT_ITME, 100)
.show();
break;
case R.id.last2:
System.out.println("shangyishou....");
CURRENT_POSITON -= 1;
CURRENT_ITME = list.get(CURRENT_POSITON);
playMusic(PATH_ROOT + CURRENT_ITME);
System.out.println("path:" + PATH_ROOT + CURRENT_ITME);
Toast.makeText(this, "shang一首音乐,path:" + PATH_ROOT + CURRENT_ITME, 100)
.show();
break;
}
} private void playMusic(String path) {
mediaplayer.reset();
try {
mediaplayer.setDataSource(path);
mediaplayer.prepare();
mediaplayer.start();
mediaplayer
.setOnCompletionListener(new myOnCompleteMusicListener());
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } public class myOnCompleteMusicListener implements OnCompletionListener { @Override
public void onCompletion(MediaPlayer mp) {
// TODO
// playMusic(P);
} } @Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
CURRENT_POSITON = position;
CURRENT_ITME = list.get(position);//获取当前位置的String字符串,即文件名
playMusic(PATH_ROOT + CURRENT_ITME);
Toast.makeText(this, "PATH:" + PATH_ROOT + CURRENT_ITME, 100).show();
System.out.println("path:" + PATH_ROOT + CURRENT_ITME);
} }

android:music

上一篇:使用Autodesk Vault插件向导轻松创建Vault插件


下一篇:java 集合类说明与区别