码迷,mamicode.com
首页 > 移动开发 > 详细

android:music

时间:2015-09-18 00:31:41      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:

   

  1 package com.terry;
  2 
  3 import java.io.File;
  4 import java.io.FileFilter;
  5 import java.io.IOException;
  6 import java.util.ArrayList;
  7 import java.util.List;
  8 
  9 import android.app.ListActivity;
 10 import android.media.MediaPlayer;
 11 import android.media.MediaPlayer.OnCompletionListener;
 12 import android.os.Bundle;
 13 import android.view.View;
 14 import android.view.View.OnClickListener;
 15 import android.widget.ArrayAdapter;
 16 import android.widget.Button;
 17 import android.widget.ListView;
 18 import android.widget.Toast;
 19 
 20 public class myActivity2 extends ListActivity implements OnClickListener {
 21     Button play;
 22     Button pause;
 23     Button next;
 24     Button last;
 25     // ListView listViews;
 26     List<String> list;
 27     MediaPlayer mediaplayer;
 28     String PATH_ROOT = "/sdcard/";
 29     //当前项目名称
 30     String CURRENT_ITME = "";
 31     //当前位置
 32     int CURRENT_POSITON = 0;
 33 
 34     @Override
 35     protected void onCreate(Bundle savedInstanceState) {
 36         // TODO Auto-generated method stub
 37         super.onCreate(savedInstanceState);
 38         // Intent intent = new Intent();
 39         setContentView(R.layout.main2);
 40 
 41         initViews();
 42         initListViews();
 43         mediaplayer = new MediaPlayer();
 44     }
 45 
 46     private void initViews() {
 47         // TODO Auto-generated method stub
 48         play = (Button) findViewById(R.id.start2);
 49         pause = (Button) findViewById(R.id.pause2);
 50         next = (Button) findViewById(R.id.next2);
 51         last = (Button) findViewById(R.id.last2);
 52         play.setOnClickListener(this);
 53         pause.setOnClickListener(this);
 54         next.setOnClickListener(this);
 55         last.setOnClickListener(this);
 56     }
 57 
 58     private void initListViews() {
 59         // 初始化ListViews
 60         // listViews
 61         list = new ArrayList<String>();
 62         ArrayAdapter<String> musiclistadapter;
 63         File file = new File(PATH_ROOT);
 64         if (file != null) {
 65             if (file.isDirectory()) {
 66                 File[] listfiles = file.listFiles();
 67                 if (listfiles != null) {
 68                     for (int i = 0; i < listfiles.length; i++) {
 69                         String name = listfiles[i].getName();
 70                         if (name.endsWith(".mp3")) {
 71                             list.add(name);
 72                         }
 73                     }
 74                 }
 75             }
 76         }
 77         musiclistadapter = new ArrayAdapter<String>(this, R.layout.musicitme,
 78                 list);
 79         setListAdapter(musiclistadapter);
 80     }
 81 
 82     @Override
 83     public void onClick(View v) {
 84         switch (v.getId()) {
 85         case R.id.start2:
 86             // mediaplayer.p
 87             playMusic(PATH_ROOT + CURRENT_ITME);
 88 
 89             break;
 90 
 91         case R.id.pause2:
 92             if (mediaplayer.isPlaying()) {
 93                 mediaplayer.pause();
 94             } else {
 95                 Toast.makeText(this, "没有正在播放的音乐,我自动播放咯。。。。", 100).show();
 96                 playMusic(PATH_ROOT + CURRENT_ITME);
 97             }
 98             break;
 99         case R.id.next2:
100             System.out.println("xiayishou....");
101             CURRENT_POSITON += 1;
102             CURRENT_ITME = list.get(CURRENT_POSITON);
103             playMusic(PATH_ROOT + CURRENT_ITME);
104             System.out.println("path:" + PATH_ROOT + CURRENT_ITME);
105             Toast.makeText(this, "下一首音乐,path:" + PATH_ROOT + CURRENT_ITME, 100)
106                     .show();
107             break;
108         case R.id.last2:
109             System.out.println("shangyishou....");
110             CURRENT_POSITON -= 1;
111             CURRENT_ITME = list.get(CURRENT_POSITON);
112             playMusic(PATH_ROOT + CURRENT_ITME);
113             System.out.println("path:" + PATH_ROOT + CURRENT_ITME);
114             Toast.makeText(this, "shang一首音乐,path:" + PATH_ROOT + CURRENT_ITME, 100)
115                     .show();
116             break;
117         }
118     }
119 
120     private void playMusic(String path) {
121         mediaplayer.reset();
122         try {
123             mediaplayer.setDataSource(path);
124             mediaplayer.prepare();
125             mediaplayer.start();
126             mediaplayer
127                     .setOnCompletionListener(new myOnCompleteMusicListener());
128         } catch (IllegalArgumentException e) {
129             // TODO Auto-generated catch block
130             e.printStackTrace();
131         } catch (SecurityException e) {
132             // TODO Auto-generated catch block
133             e.printStackTrace();
134         } catch (IllegalStateException e) {
135             // TODO Auto-generated catch block
136             e.printStackTrace();
137         } catch (IOException e) {
138             // TODO Auto-generated catch block
139             e.printStackTrace();
140         }
141 
142     }
143 
144     public class myOnCompleteMusicListener implements OnCompletionListener {
145 
146         @Override
147         public void onCompletion(MediaPlayer mp) {
148             // TODO
149             // playMusic(P);
150         }
151 
152     }
153 
154     @Override
155     protected void onListItemClick(ListView l, View v, int position, long id) {
156         // TODO Auto-generated method stub
157         super.onListItemClick(l, v, position, id);
158         CURRENT_POSITON = position;
159         CURRENT_ITME = list.get(position);//获取当前位置的String字符串,即文件名
160         playMusic(PATH_ROOT + CURRENT_ITME);
161         Toast.makeText(this, "PATH:" + PATH_ROOT + CURRENT_ITME, 100).show();
162         System.out.println("path:" + PATH_ROOT + CURRENT_ITME);
163     }
164 
165 }

 技术分享

android:music

标签:

原文地址:http://www.cnblogs.com/Sunnor/p/4817917.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!