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();
}