简易音乐播放器
作者:互联网
1、新建xml文件定义各个属性:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/skin_bg"
android:orientation="vertical" >
<TextView
android:id="@+id/txttile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp" />
<TextView
android:id="@+id/txtdangtime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
android:layout_marginTop="385dp"
android:text="1:30"
android:textSize="18dp" />
<TextView
android:id="@+id/txtzongtime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="270dp"
android:layout_marginTop="385dp"
android:text="3:30"
android:textSize="18dp" />
<SeekBar
android:id="@+id/seekbar"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:layout_marginLeft="45dp"
android:layout_marginTop="380dp"
android:background="@android:color/holo_red_dark" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<ImageButton
android:id="@+id/buttonplayer"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginBottom="100dp"
android:layout_marginLeft="140dp"
android:layout_marginTop="420dp"
android:src="@drawable/zanting" />
<ImageButton
android:id="@+id/buttonnext"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="420dp"
android:layout_toRightOf="@id/buttonplayer"
android:src="@drawable/nextsong" />
<ImageButton
android:id="@+id/buttonshang"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="90dp"
android:layout_marginTop="420dp"
android:background="@drawable/shangsong" />
</RelativeLayout>
</RelativeLayout>
-----------------------------------------------------------------------------分割线-----------------------------------------------------------------------------------
2、新建Activty继承Activty
package com.example.com.scxh.mediaplayer;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import com.example.com.scxh.mediaplayer.ShowPlayer.UpdateStatus;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.TextView;
public class MidiaPlayer3 extends Activity implements OnClickListener {
private int currentPosition = 0;
private ListAdapters songList;
private List<String> songs = new ArrayList<String>();
private SeekBar mSeekBar;
private MediaPlayer mMediaPlayer;
private boolean isPlayering;
private Handler mHandler = new Handler();
private TextView mCurrentTimeTxt;
private TextView mTitleMusicTxt;
private TextView mTotalTimeTxt;
private ImageButton mPlayBtn, mNextBtn, mPrecBtn;
private int mSec;
private int mCurrentPosition;
private String path;
private int mprogress = 0;
private boolean playing = true;
private ArrayList<String> mList = new ArrayList<String>();
@Override
protected void onCreate(Bundle icicle) {
// TODO Auto-generated method stub
super.onCreate(icicle);
setContentView(R.layout.midiaplyer3);
mTitleMusicTxt = (TextView) findViewById(R.id.txttile);
mTotalTimeTxt = (TextView) findViewById(R.id.txtzongtime);
mCurrentTimeTxt = (TextView) findViewById(R.id.txtdangtime);
mSeekBar = (SeekBar) findViewById(R.id.seekbar);
mPlayBtn = (ImageButton) findViewById(R.id.buttonplayer);
mNextBtn = (ImageButton) findViewById(R.id.buttonnext);
mPrecBtn = (ImageButton) findViewById(R.id.buttonshang);
mNextBtn.setOnClickListener(this);
mPrecBtn.setOnClickListener(this);
mPlayBtn.setOnClickListener(this);
// 接收数据源
Intent intent = getIntent();
path = intent.getStringExtra("key");
// 接收列表数据源
mList = intent.getStringArrayListExtra("song");
Log.e("path", path);
isPlayering = true;
MediaPlayer(path);
mTitleMusicTxt.setText(path);
}
public void MediaPlayer(String path) {
if (mMediaPlayer == null) {
try {
// 创建一个播放器
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(path);
mMediaPlayer.prepare();
mMediaPlayer.start();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
// 进度条拖动监听
mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
mMediaPlayer.seekTo(mprogress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
mprogress = progress;
}
});
//上曲播放结束自动播放下一曲
mMediaPlayer
.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
if (++mCurrentPosition > (mList.size() - 1)) {
mCurrentPosition = 0;
}
mMediaPlayer.release();
mMediaPlayer = null;
MediaPlayer(mList.get(mCurrentPosition));
}
});
// 启动线程
UpdatePlayerViewThread mPlayerThread = new UpdatePlayerViewThread();
mPlayerThread.start();
}
// 刷新当前界面
class UpdatePlayerViewThread extends Thread {
@Override
public void run() {
while (true) {
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (isPlayering) {
mHandler.post(new Runnable() {
@Override
public void run() {
Log.e("tag", "aaa>>>>>>>>>>>>>>>");
// 获取歌曲总时间
mSeekBar.setMax(mMediaPlayer.getDuration());
// 获取当前播放歌曲进度时间
mSeekBar.setProgress(mMediaPlayer
.getCurrentPosition());
Log.v("mMediaPlayer", "mMediaPlayer>>>>>>>>>>>>>>>");
int pos = 0;
pos = mMediaPlayer.getCurrentPosition();
int min = (pos / 1000) / 60;
int sec = (pos / 1000) % 60;
int maxPos = mMediaPlayer.getDuration();
int maxMin = (maxPos / 1000) / 60;
int maxSec = (maxPos / 1000) % 60;
if (maxSec < 10) {
mTotalTimeTxt.setText("" + maxMin + ":0"
+ maxSec);
} else {
mTotalTimeTxt.setText("" + maxMin + ":"
+ maxSec);
}
if (sec < 10) {
mCurrentTimeTxt.setText("" + min + ":0" + sec);
} else {
mCurrentTimeTxt.setText("" + min + ":" + sec);
}
}
});
}
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
isPlayering = false;
// 如果播放器不等于空
if (mMediaPlayer != null) {
// 释放
mMediaPlayer.release();
// 播放器等于空
mMediaPlayer = null;
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.buttonnext:
// 播放下一曲
if (++mCurrentPosition > (mList.size() - 1)) {
mCurrentPosition = 0;
}
mMediaPlayer.release();
mMediaPlayer = null;
MediaPlayer(mList.get(mCurrentPosition));
break;
case R.id.buttonplayer:
// 暂停/播放
if (playing == true) {
mPlayBtn.setImageResource(R.drawable.zanting);
// 暂停
mMediaPlayer.pause();
playing = false;
} else {
mPlayBtn.setImageResource(R.drawable.znting);
playing = true;
mMediaPlayer.start();
}
break;
case R.id.buttonshang:
// 播放上一曲
if (--mCurrentPosition < 0) {
mCurrentPosition = mList.size() - 1;
}
mMediaPlayer.release();
mMediaPlayer = null;
MediaPlayer(mList.get(mCurrentPosition));
break;
}
}
}
以上播放器只能实现播放,暂停、上一曲、下一曲、播放结束自动播放下一曲、拖动进度条、歌曲当前时间总时间。
标签:播放器,mMediaPlayer,int,void,音乐,private,简易,import,android 来源: https://blog.51cto.com/u_14397532/3035664