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

1600802120

时间:2018-12-11 17:27:13      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:roi   输出   res   item   dia   .so   int   tst   iap   

   1.实现的功能

  1.音乐播放器的前一首,下一首,暂停,播放功能。

  2.从sd卡中搜索所有音乐文件,并且筛选出时长大于1分钟的音乐,输出音乐名称,时长,音乐作者(音乐所在的绝对地址)

  3.使用了进度条显示实时播放的时间。

  • 2.对音乐播放器界面截图
  • 技术分享图片
    3.将其中的关键代码放置自己的博客,比如:播放功能的代码
    1.上一首下一首切换,暂停播放
public void onClick(View view) {
        if (view.getId()==R.id.mPreviousBtn){
            changeMusic(--mCurrentPosition);
        }else if (view.getId()==R.id.mPlayBtn){
            
            if (mediaPlayer==null){
                changeMusic(0);
            }else {
                if (mediaPlayer.isPlaying()){
                    mediaPlayer.pause();
                }else {
                    mediaPlayer.start();
                }
            }
        }else if (view.getId()==R.id.mNextBtn){
            changeMusic(++mCurrentPosition);

2.切歌

 private void changeMusic(int position){
        if (position<0){
            mCurrentPosition = position =list.size()-1;
        }else if (position>list.size()-1){
            mCurrentPosition = position=0;
        }

        if (mediaPlayer==null){
            mediaPlayer=new MediaPlayer();
        }

        try {
        
            mediaPlayer.reset();
            mediaPlayer.setDataSource(list.get(position).path);
            mediaPlayer.prepare();
            mediaPlayer.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
        mSeekBar.setProgress(0);
        mSeekBar.setMax(mediaPlayer.getDuration());
        mTotalTimeTv.setText(parseTime(mediaPlayer.getDuration()));

        updateProgress();
    }

3.列表显示

private void initView() {
        mListView = (ListView) findViewById(R.id.main_listview);
        list = new ArrayList<>();
        list = MusicUtils.getMusicData(this);
        adapter = new Adapter(this,list);
        mListView.setAdapter(adapter);
        mediaPlayer.setOnCompletionListener(this);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
          String path = list.get(i).path;
                Log.i("WDD",path);

                mCurrentPosition=i;
                changeMusic(mCurrentPosition);

               play(list.get(i).path);
               String singer = list.get(i).singer;//歌手
               String song = list.get(i).song;//歌曲名称
              int duration = list.get(i).duration;//歌曲长度

             String time = MusicUtils.formatTime(duration);
                String s=singer+"\n"+song+"\n"+time;
                AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
                   builder.setTitle("歌曲");
                    builder.setMessage(s);
                   builder.show();
            }
        });
    }
    /**
     * 播放音频的方法
     */
    private void play(String path) {
        //播放之前要先把音频文件重置
        try {
            mediaPlayer.reset();
            //调用方法传进去要播放的音频路径
            mediaPlayer.setDataSource(path);
           //异步准备音频资源
            mediaPlayer.prepareAsync();
            //调用mediaPlayer的监听方法,音频准备完毕会响应此方法
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
               public void onPrepared(MediaPlayer mediaPlayer) {
                    mediaPlayer.start();//开始音频
               }
            });
        } catch (IOException e) {
           e.printStackTrace();        }
    }

4.从sd卡中搜索到音乐后,输出音乐信息(歌曲的名字……)

 while (cursor.moveToNext()) {
                Song song = new Song();
                song.song = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME));
                song.singer = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
                song.path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
                song.duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
                song.size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE));

 5.设置歌曲时间的格式

  private String parseTime(int oldTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("mm:ss");// 时间格式
        String newTime = sdf.format(new Date(oldTime));
        return newTime;
    }

 6.进度条跟随播放时间移动

private Handler mHandler=new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message message) {

            int progress = mediaPlayer.getCurrentPosition();
            mSeekBar.setProgress(progress);
            mCurrentTimeTv.setText(parseTime(progress));

            updateProgress();
            return true;
        }
    });

  

4.代码链接  https://git.coding.net/ahabbw/android.git

5.apk的下载地址  https://coding.net/u/ahabbw/p/android/attachment

1600802120

标签:roi   输出   res   item   dia   .so   int   tst   iap   

原文地址:https://www.cnblogs.com/liyih/p/10102740.html

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