Android中从SD卡中读取歌曲

博观而约取,厚积而薄发。这篇文章主要讲述Android中从SD卡中读取歌曲相关的知识,希望能为你提供帮助。
先看看我的效果图吧
 
 
Activity类

Android中从SD卡中读取歌曲

文章图片
Android中从SD卡中读取歌曲

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

示例代码xml类
 
Android中从SD卡中读取歌曲

文章图片
Android中从SD卡中读取歌曲

文章图片
1 < TableLayout 2android:orientation="vertical" 3android:layout_width="match_parent" 4android:layout_height="wrap_content"> 5< TableRow> 6< ListView 7android:id="@+id/list" 8android:layout_width="wrap_content" 9android:layout_height="fill_parent" 10/> 11< /TableRow> 12< /TableLayout> 13< TextView 14android:id="@+id/name" 15android:layout_width="wrap_content" 16android:layout_height="wrap_content" 17android:layout_marginTop="50px" 18/> 19< RelativeLayout 20android:layout_width="fill_parent" 21android:layout_height="wrap_content" 22> 23< ImageButton 24android:id="@+id/ib3" 25android:layout_width="wrap_content" 26android:layout_height="wrap_content" 27android:src="https://www.songbingjia.com/android/@drawable/b" 28android:background="#00000000" 29android:layout_alignParentBottom="true" 30/> 31< SeekBar 32android:id="@+id/seekBar" 33style="?android:attr/progressBarStyleHorizontal" 34android:layout_width="fill_parent" 35android:layout_height="wrap_content" 36android:paddingLeft="88dip" 37android:progressDrawable="@drawable/seekbar" 38android:thumb="@drawable/thumb" 39android:layout_alignTop="@id/ib3" 40android:max="100" 41android:maxHeight="2.0dip" 42android:minHeight="2.0dip" 43/> 44< Button 45android:id="@+id/bt021" 46android:layout_width="wrap_content" 47android:layout_height="wrap_content" 48android:text="|?" 49android:onClick="previous" 50android:layout_toRightOf="@id/ib3" 51android:background="#00000000" 52android:layout_marginRight="30px" 53android:paddingLeft="20dip" 54android:layout_alignParentBottom="true" 55/> 56< Button 57android:id="@+id/pp" 58android:layout_width="wrap_content" 59android:layout_height="wrap_content" 60android:text="?" 61android:onClick="pp" 62android:layout_toRightOf="@id/bt021" 63android:background="#00000000" 64android:layout_marginRight="30px" 65android:layout_alignParentBottom="true" 66/> 67< Button 68android:layout_width="wrap_content" 69android:layout_height="wrap_content" 70android:text="?|" 71android:onClick="next" 72android:layout_toRightOf="@id/pp" 73android:background="#00000000" 74android:layout_marginTop="20dip" 75android:layout_alignParentBottom="true" 76/> 77< /RelativeLayout>

示例代码 
Android中从SD卡中读取歌曲

文章图片
Android中从SD卡中读取歌曲

文章图片
1 < ?xml version="1.0" encoding="utf-8"?> 2 < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3android:orientation="horizontal" 4android:layout_width="fill_parent" 5android:layout_height="fill_parent" 6android:background="#FFFFFF" 7> 8< TableLayout 9android:orientation="vertical" 10android:layout_width="match_parent" 11android:layout_height="wrap_content"> 12< TableRow> 13< TextView 14android:id="@+id/textview" 15android:layout_width="fill_parent" 16android:layout_height="wrap_content" 17/> 18< /TableRow> 19< /TableLayout> 20 21 < /LinearLayout>

第二个xml【Android中从SD卡中读取歌曲】 

    推荐阅读