Android中从SD卡中读取歌曲
Posted 眼泪,还是流了
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android中从SD卡中读取歌曲相关的知识,希望对你有一定的参考价值。
先看看我的效果图吧
Activity类
1 private TextView nameTextView; 2 private SeekBar seekBar; 3 private ListView listView; 4 private List<Map<String, String>> data; 5 private int current; 6 private MediaPlayer player; 7 private Handler handler = new Handler(); 8 private Button ppButton; 9 private boolean isPause; 10 private boolean isStartTrackingTouch; 11 protected void onCreate(Bundle savedInstanceState) { 12 super.onCreate(savedInstanceState); 13 setContentView(R.layout.mainb); 14 ib3=(ImageButton) findViewById(R.id.ib3); 15 ib3.setOnClickListener(new OnClickListener() { 16 @Override 17 public void onClick(View v) { 18 Intent it=new Intent(Music_b.this,Music_gc.class); 19 String name=data.get(current).get("name"); 20 it.putExtra("name", name); 21 startActivityForResult(it, 1); 22 } 23 }); 24 tv=(TextView)findViewById(R.id.tvbb); 25 sp=(Spinner) findViewById(R.id.sp); 26 sp.setOnItemSelectedListener(new OnItemSelectedListener() { 27 @Override 28 public void onItemSelected(AdapterView<?> arg0, View view, 29 int arg2, long arg3) { 30 TextView tv1=(TextView)view; 31 String str=tv1.getText().toString(); 32 tv.setText(str); 33 } 34 @Override 35 public void onNothingSelected(AdapterView<?> arg0) { 36 } 37 }); 38 sp1=(Spinner) findViewById(R.id.sp1); 39 sp1.setOnItemSelectedListener(new OnItemSelectedListener() { 40 41 @Override 42 public void onItemSelected(AdapterView<?> arg0, View view, 43 int arg2, long arg3) { 44 TextView tv1=(TextView)view; 45 String str=tv1.getText().toString(); 46 tv.setText(str); 47 } 48 @Override 49 public void onNothingSelected(AdapterView<?> arg0) { 50 51 } 52 }); 53 nameTextView = (TextView) findViewById(R.id.name); 54 seekBar = (SeekBar) findViewById(R.id.seekBar); 55 listView = (ListView) findViewById(R.id.list); 56 ppButton = (Button) findViewById(R.id.pp); 57 //创建一个音乐播放器 58 player = new MediaPlayer(); 59 //显示音乐播放器 60 generateListView(); 61 //进度条监听器 62 seekBar.setOnSeekBarChangeListener(new MySeekBarListener()); 63 //播放器监听器 64 player.setOnCompletionListener(new MyPlayerListener()); 65 //意图过滤器 66 IntentFilter filter = new IntentFilter(); 67 } 68 private final class MyPlayerListener implements OnCompletionListener { 69 //歌曲播放完后自动播放下一首歌区 70 public void onCompletion(MediaPlayer mp) { 71 next(); 72 } 73 } 74 public void next(View view) { 75 next(); 76 } 77 public void previous(View view) { 78 previous(); 79 } 80 private void previous() { 81 current = current - 1 < 0 ? data.size() - 1 : current - 1; 82 play(); 83 } 84 private void next() { 85 current = (current + 1) % data.size(); 86 play(); 87 } 88 private final class MySeekBarListener implements OnSeekBarChangeListener { 89 //移动触发 90 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 91 } 92 //起始触发 93 public void onStartTrackingTouch(SeekBar seekBar) { 94 isStartTrackingTouch = true; 95 } 96 //结束触发 97 public void onStopTrackingTouch(SeekBar seekBar) { 98 player.seekTo(seekBar.getProgress()); 99 isStartTrackingTouch = false; 100 } 101 } 102 private void generateListView() { 103 List<File> list = new ArrayList<File>(); 104 //获取sdcard中的所有歌曲 105 findAll(Environment.getExternalStorageDirectory(), list); 106 //播放列表进行排序,字符顺序 107 Collections.sort(list); 108 data = new ArrayList<Map<String, String>>(); 109 for (File file : list) { 110 Map<String, String> map = new HashMap<String, String>(); 111 map.put("name", file.getName()); 112 map.put("path", file.getAbsolutePath()); 113 data.add(map); 114 } 115 SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.mainmp3_item, new String[] { "name" }, new int[] { R.id.mName }); 116 listView.setAdapter(adapter); 117 listView.setOnItemClickListener(new MyItemListener()); 118 } 119 private final class MyItemListener implements OnItemClickListener { 120 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 121 current = position; 122 play(); 123 } 124 } 125 private void play() { 126 try { 127 //重播 128 player.reset(); 129 //获取歌曲路径 130 player.setDataSource(data.get(current).get("path")); 131 //缓冲 132 player.prepare(); 133 //开始播放 134 player.start(); 135 //显示歌名 136 nameTextView.setText(data.get(current).get("name")); 137 //设置进度条长度 138 seekBar.setMax(player.getDuration()); 139 //播放按钮样式 140 ppButton.setText("||"); 141 //发送一个Runnable,handler收到之后就会执行run()方法 142 handler.post(new Runnable() { 143 public void run() { 144 // 更新进度条状态 145 if (!isStartTrackingTouch) 146 seekBar.setProgress(player.getCurrentPosition()); 147 // 1秒之后再次发送 148 handler.postDelayed(this, 1000); 149 } 150 }); 151 } catch (Exception e) { 152 e.printStackTrace(); 153 } 154 } 155 private void findAll(File file, List<File> list) { 156 File[] subFiles = file.listFiles(); 157 if (subFiles != null) 158 for (File subFile : subFiles) { 159 if (subFile.isFile() && subFile.getName().endsWith(".mp3")) 160 list.add(subFile); 161 else if (subFile.isDirectory())//如果是目录 162 findAll(subFile, list); //递归 163 } 164 } 165 public void pp(View view) { 166 //默认从第一首歌开始播放 167 if (!player.isPlaying() && !isPause) { 168 play(); 169 return; 170 } 171 Button button = (Button) view; 172 //暂停/播放按钮 173 if ("||".equals(button.getText())) { 174 pause(); 175 button.setText("|>"); 176 }else if("|>".equals(button.getText())) { 177 resume(); 178 button.setText("||"); 179 } 180 } 181 private void resume() { 182 if (isPause) { 183 player.start(); 184 isPause = false; 185 } 186 } 187 private void pause() { 188 if (player != null && player.isPlaying()) { 189 player.pause(); 190 isPause = true; 191 } 192 } 193 }
xml类
1 <TableLayout 2 android:orientation="vertical" 3 android:layout_width="match_parent" 4 android:layout_height="wrap_content"> 5 <TableRow> 6 <ListView 7 android:id="@+id/list" 8 android:layout_width="wrap_content" 9 android:layout_height="fill_parent" 10 /> 11 </TableRow> 12 </TableLayout> 13 <TextView 14 android:id="@+id/name" 15 android:layout_width="wrap_content" 16 android:layout_height="wrap_content" 17 android:layout_marginTop="50px" 18 /> 19 <RelativeLayout 20 android:layout_width="fill_parent" 21 android:layout_height="wrap_content" 22 > 23 <ImageButton 24 android:id="@+id/ib3" 25 android:layout_width="wrap_content" 26 android:layout_height="wrap_content" 27 android:src="@drawable/b" 28 android:background="#00000000" 29 android:layout_alignParentBottom="true" 30 /> 31 <SeekBar 32 android:id="@+id/seekBar" 33 style="?android:attr/progressBarStyleHorizontal" 34 android:layout_width="fill_parent" 35 android:layout_height="wrap_content" 36 android:paddingLeft="88dip" 37 android:progressDrawable="@drawable/seekbar" 38 android:thumb="@drawable/thumb" 39 android:layout_alignTop="@id/ib3" 40 android:max="100" 41 android:maxHeight="2.0dip" 42 android:minHeight="2.0dip" 43 /> 44 <Button 45 android:id="@+id/bt021" 46 android:layout_width="wrap_content" 47 android:layout_height="wrap_content" 48 android:text="|?" 49 android:onClick="previous" 50 android:layout_toRightOf="@id/ib3" 51 android:background="#00000000" 52 android:layout_marginRight="30px" 53 android:paddingLeft="20dip" 54 android:layout_alignParentBottom="true" 55 /> 56 <Button 57 android:id="@+id/pp" 58 android:layout_width="wrap_content" 59 android:layout_height="wrap_content" 60 android:text="?" 61 android:onClick="pp" 62 android:layout_toRightOf="@id/bt021" 63 android:background="#00000000" 64 android:layout_marginRight="30px" 65 android:layout_alignParentBottom="true" 66 /> 67 <Button 68 android:layout_width="wrap_content" 69 android:layout_height="wrap_content" 70 android:text="?|" 71 android:onClick="next" 72 android:layout_toRightOf="@id/pp" 73 android:background="#00000000" 74 android:layout_marginTop="20dip" 75 android:layout_alignParentBottom="true" 76 /> 77 </RelativeLayout>
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="horizontal" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent" 6 android:background="#FFFFFF" 7 > 8 <TableLayout 9 android:orientation="vertical" 10 android:layout_width="match_parent" 11 android:layout_height="wrap_content"> 12 <TableRow> 13 <TextView 14 android:id="@+id/textview" 15 android:layout_width="fill_parent" 16 android:layout_height="wrap_content" 17 /> 18 </TableRow> 19 </TableLayout> 20 21 </LinearLayout>
以上是关于Android中从SD卡中读取歌曲的主要内容,如果未能解决你的问题,请参考以下文章