音乐播放器

package com.taiyi.DynamicOffice.music.activity;

import java.io.IOException;
import com.taiyi.DynamicOffice.R;
import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.Intent;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.os.Bundle;
import android.os.Handler;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

/**
 *
 * @author Administrator
 *
 */
public class DialogMusicActivity extends Activity
{

 private static final String tag = "DialogMusicActivity";

 private View viewMusic = null;
 private TextView tv_musicname = null;
 private ImageButton btnplay = null;
 private ImageButton btnback = null;
 private ImageButton btnnext = null;
 private ImageButton btnloop = null;
 private SeekBar duration = null;
 private Cursor c;
 private int maxduration = 100;
 private String musicname = "";
 private String musicPath = "";
 private MediaPlayer mediaPlayer = null;
 private int[] _ids;
 private int mCurrent;
 private int player_state = 0;
 private Builder builder = null;
 private Intent intent=null;

 @Override
 public void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
  viewMusic = getLayoutInflater().inflate(R.layout.dialogmusic, null);
  intent = getIntent(); 
  musicPath = intent.getStringExtra("musicPaths");
  musicname=intent.getStringExtra("musicname");
  showDialog();
 }

 /**
  * 音乐对话框
  */
 public void showDialog()
 {
  if (builder == null)
  {
   builder = new Builder(this);
   builder.setIcon(R.drawable.music);
   builder.setTitle(R.string.music_titel);
   builder.setView(viewMusic);
   builder.show();
   MusicDialog();
  }else
  {
   builder.setIcon(R.drawable.music);
   builder.setTitle(R.string.music_titel);
   builder.setView(viewMusic);
   builder.show();
   MusicDialog();
  }
 }

 /**
  * 音乐对话框中要显示的内容
  */
 public void MusicDialog()
 {
  mediaPlayer = new MediaPlayer();
  btnplay = (ImageButton) viewMusic.findViewById(R.id.playmusic);
  btnback = (ImageButton) viewMusic.findViewById(R.id.backmusic);
  btnnext = (ImageButton) viewMusic.findViewById(R.id.nextmusic);
  btnloop = (ImageButton) viewMusic.findViewById(R.id.musicmodel);
  duration = (SeekBar) viewMusic.findViewById(R.id.seekBar1);
  tv_musicname = (TextView) viewMusic.findViewById(R.id.dialog_musicname);
  initMusicPlay();
  duration.setMax(maxduration);
  tv_musicname.setText(musicname);
  MusicPlayer();
  // 播放音乐
  btnplay.setOnClickListener(new OnClickListener()
  {

   @Override
   public void onClick(View arg0)
   {
    // TODO Auto-generated method stub
    if (mediaPlayer.isPlaying())
    {
     player_state = 1;
     btnplay.setImageResource(R.drawable.isplaying);
     mediaPlayer.pause();
    }
    else
    {
     btnplay.setImageResource(R.drawable.pause1);
     MusicPlayer();
    }
   }

  });
  // 上一首
  btnback.setOnClickListener(new OnClickListener()
  {

   @Override
   public void onClick(View arg0)
   {
    // TODO Auto-generated method stub
    MusicBack();
   }

  });
  // 下一首
  btnnext.setOnClickListener(new OnClickListener()
  {

   @Override
   public void onClick(View arg0)
   {
    // TODO Auto-generated method stub
    MusicNext();

   }

  });
  // 单曲播放音乐
  btnloop.setOnClickListener(new OnClickListener()
  {

   @Override
   public void onClick(View arg0)
   {
    // TODO Auto-generated method stub
    mediaPlayer.setLooping(true);
    mediaPlayer.start();
   }

  });
  // 进度条事件
  duration.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
  {

   @Override
   public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2)
   {
    // TODO Auto-generated method stub
    if (arg2)
    {
     mediaPlayer.seekTo(arg1); // 音质播放不清楚
    }
   }

   @Override
   public void onStartTrackingTouch(SeekBar arg0)
   {
    // TODO Auto-generated method stub
    mediaPlayer.pause();
   }

   @Override
   public void onStopTrackingTouch(SeekBar arg0)
   {
    // TODO Auto-generated method stub
    mediaPlayer.start();

   }
  });
  new Thread().start();
 }

 

 /**
  * 初始化music
  */
 public void initMusicPlay()
 {
  
  c = this.getContentResolver().query(
    MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
    new String[]
    { MediaStore.Audio.Media.TITLE,
      MediaStore.Audio.Media.DURATION,
      MediaStore.Audio.Media.ARTIST,
      MediaStore.Audio.Media._ID,
      MediaStore.Audio.Media.DISPLAY_NAME,
      MediaStore.Audio.Media.DATA }, null, null, null);
  c.moveToFirst();
//  musicname = c.getString(0);
  maxduration = c.getInt(1);
//  musicPath = c.getString(5);
  
  Log.i(tag, tv_musicname.getText().toString());
  _ids = new int[c.getCount()];
  for (int i = 0; i < c.getCount(); i++)
  {
   _ids[i] = c.getInt(3);
   c.moveToNext();
  }
  StrartbarUpdate();
 }

 Handler handler = new Handler();

 // 更新播放进度条
 public void StrartbarUpdate()
 {
  // 更新seekBar1
  handler.post(r);
 }

 Runnable r = new Runnable()
 {

  public void run()
  {
   // TODO Auto-generated method stub
   // 获取歌曲的播放进度是通过MediaPlayer这个类取得的
   // 获取歌曲播放到多少ms
   int CurrentPosition = mediaPlayer.getCurrentPosition();
   // 更新播放时间显示
   // // duration.
   duration.setProgress(CurrentPosition);
   // // 获取歌曲总时间长度
   maxduration = mediaPlayer.getDuration();

   handler.postDelayed(r, 100);

  }
 };

 /**
  * 音乐播放
  */
 public void MusicPlayer()
 {
  if (player_state == 0)
  {
   try
   {
    mediaPlayer.reset();
    mediaPlayer.setDataSource(musicPath);
    mediaPlayer.prepare();
    mediaPlayer.start();
   }
   catch (IllegalArgumentException e)
   {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   catch (IllegalStateException e)
   {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   catch (IOException e)
   {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   mediaPlayer.setOnCompletionListener(new OnCompletionListener()
   {

    @Override
    public void onCompletion(MediaPlayer mp)
    {
     // TODO Auto-generated method stub
     MusicNext();
    }

   });
   mediaPlayer.setOnErrorListener(new OnErrorListener()
   {

    @Override
    public boolean onError(MediaPlayer mp, int what, int extra)
    {
     mediaPlayer.reset();
     return true;
    }

   });
  }
  else
  {
   mediaPlayer.start();
  }
  player_state = 0;

 }

 /**
  * 上一首
  */
 public void MusicBack()
 {
  if (mCurrent == 0)
  {
   mCurrent = _ids.length - 1;
  }
  else if (mCurrent > 0)
  {
   --mCurrent;
  }
  mediaPlayer.stop();
  refreshView();
  MusicPlayer();
 }

 /**
  * 下一首
  */
 public void MusicNext()
 {
  if (_ids.length == 1)
  {

   MusicPlayer();
   return;

  }
  else if (mCurrent == _ids.length - 1)
  {
   mCurrent = 0;
  }
  else if (mCurrent < _ids.length - 1)
  {
   mCurrent++;
  }
  mediaPlayer.stop();
  refreshView();
  MusicPlayer();
 }

 /**
  * 刷新控件
  */
 public void refreshView()
 {
  
  c = getContentResolver().query(
    MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
    new String[]
    { MediaStore.Audio.Media.TITLE,
      MediaStore.Audio.Media.DURATION,
      MediaStore.Audio.Media.ALBUM,
      MediaStore.Audio.Media.DATA }, "_id=?", new String[]
    { _ids[mCurrent] + "" }, null);
  c.moveToFirst();
  musicname = c.getString(0);
  maxduration = c.getInt(1);
  musicPath = c.getString(3);
  duration.setMax(maxduration);
  tv_musicname.setText(musicname);
  StrartbarUpdate();
 }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值