码迷,mamicode.com
首页 > 其他好文 > 详细

论自作音乐播放器涉及知识点总结

时间:2016-04-17 19:14:17      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:

1.内容提供者查找手机SD卡下的音乐

配置读取SD卡权限

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

//调用方法
List<Good> list = GetMusic.Music(MainActivity.this);
技术分享
package com.bawei.day07_music;

import java.util.ArrayList;

import com.bawei.vo.Good;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.provider.MediaStore.Audio;

public class GetMusic {

    public static ArrayList<Good> Music(Context context) {
        ContentResolver resolver = context.getContentResolver();
        // 内容提供者访问MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
        Cursor cursor = resolver.query(
                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
                null);
        ArrayList<Good> list=new ArrayList<Good>();
        while (cursor.moveToNext()) {
            // 名字
            String name = cursor.getString(cursor
                    .getColumnIndex(Audio.Media.TITLE));
            // 专辑名
            String album = cursor.getString(cursor
                    .getColumnIndexOrThrow(Audio.Media.ALBUM));
            // 歌手名
            String artist = cursor.getString(cursor
                    .getColumnIndexOrThrow(Audio.Media.ARTIST));
            // URI歌曲文件存放路径
            String path = cursor.getString(cursor
                    .getColumnIndexOrThrow(Audio.Media.DATA));
            // 歌曲文件播放时间长度
            int duration = cursor.getInt(cursor
                    .getColumnIndexOrThrow(Audio.Media.DURATION));
            // 音乐文件大小
            int size = cursor.getInt(cursor
                    .getColumnIndexOrThrow(Audio.Media.SIZE));
            //保存到集合
            list.add(new Good(name,artist,path,false));
        }
        return list;
    }
}
技术分享

2.播放音乐

技术分享
//播放音乐
    private void startMusic(int arg0) {
        //让上一首停下
        if (player != null && player.isPlaying()) {
            player.stop();
            player.reset();
            player = null;
            
        }
       
        try {
            //播放器
            player = new MediaPlayer();
            //播放路径
            player.setDataSource(list.get(arg0).getMusic());
            //准备
            player.prepare();
            //播放
            player.start();
            //得到歌曲总进度
            c = player.getDuration();
            //给进度条seekbar设置最大值
            seekBar.setMax(c);
            //timer
            time();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
技术分享
技术分享
//timer
    private void time() {
        // TODO Auto-generated method stub
        //timer
        timer = new Timer();
        //播放进度100毫秒前进一下
        timer.schedule(new TimerTask() {

            private int d;

            @Override
            public void run() {
                // TODO Auto-generated method stub
                //得到当前播放进度 
                d = player.getCurrentPosition();
                //赋给seekbar
                seekBar.setProgress(d);
//handler传值 显示时间 Message message = Message.obtain(); message.arg1 = c; message.arg2 = d; handler.sendMessage(message); } }, 0, 100); }
技术分享
技术分享
    //显示时间
    Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            //当前进度
            int cu1 = msg.arg1;
            //总长度
            int cu2 = msg.arg2;
            //转换成时间格式
            SimpleDateFormat sim = new SimpleDateFormat("mm:ss");
            String cur1 = sim.format(cu1);
            String cur2 = sim.format(cu2);
            //赋给textview
            text1.setText(cur2);
            text2.setText(cur1);
           
        };
    };
技术分享

3.暂停继续播放

                  if (player != null) {
                        //音乐暂停
                        player.pause();
                        //得到当前seekBar进度
                        m = seekBar.getProgress();
                    }
                       //继续播放
                        player.seekTo(m);

4.seekbar调整音乐播放进度

技术分享
    //seekbar调整音乐播放进度
                        seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

                            public void onStopTrackingTouch(SeekBar seekBar) {
                                // TODO Auto-generated method stub
                                if (player != null) {
                                     //得到当前拖拽的进度
                                    player.seekTo(seekBar.getProgress());
//timer timecan();
//timer计时 time(); } } public void onStartTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub //清空timer timecan(); } public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // TODO Auto-generated method stub } });
技术分享
技术分享
 ////清空timer
    private void timecan() {
        // TODO Auto-generated method stub
        if (timer != null) {
            timer.cancel();
        }
    }
技术分享

5.Intent跳转传list集合 

先把List<>改为ArrayList<>

ArrayList<Good> list=new ArrayList<Good>();
Intent intent = new Intent(MainActivity.this,InfoActivity.class);
intent.putExtra("list", list); startActivity(intent);
Intent intent = getIntent();
ArrayList<Good> list= (ArrayList<Good>) intent.getSerializableExtra("list");

论自作音乐播放器涉及知识点总结

标签:

原文地址:http://www.cnblogs.com/wbp0818/p/5401803.html

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