package com.xmobileapp.Mp3;
import java.io.IOException;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.xmobileapp.Mp3.R;
public class ShowPlayer extends Activity implements OnClickListener {
private Button stopButton;
private Button restartButton;
private Button nextSong;
private Button resetButton;
private TextView title;
private ProgressBar progressBar;
private boolean isPlaying;
public static MediaPlayer mp;
private final static int NEXT = 2;
private String mPath;
private Handler mHandler = new Handler();
private TextView mTimepos;
private UpdateStatus mus;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
if(mp == null){
mp = new MediaPlayer();
}else{
mp.stop();
mp = null;
mp = new MediaPlayer();
}
Intent intent = getIntent();
Bundle bundle = intent.getBundleExtra("playPara");
mPath = bundle.getString("songPath");
System.out.println("mPath="+mPath);
try {
mp.setDataSource(mPath);
} 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();
}
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();
setContentView(R.layout.mediaplayer);
initControls();
isPlaying = true;
mus = new UpdateStatus(); //start update thread and media player
mus.start();
}
private void initControls() {
title = (TextView) findViewById(R.id.text_kb_streamed);
stopButton = (Button) findViewById(R.id.button_stream);
stopButton.setId(1);
stopButton.setOnClickListener(this);
restartButton = (Button) findViewById(R.id.button_stream);
restartButton.setId(2);
restartButton.setOnClickListener(this);
restartButton.setVisibility(View.GONE);
nextSong = (Button) findViewById(R.id.nextsong);
nextSong.setId(4);
nextSong.setOnClickListener(this);
mTimepos = (TextView)findViewById(R.id.timepos);
resetButton = (Button) findViewById(R.id.button_play);
resetButton.setId(3);
resetButton.setOnClickListener(this);
progressBar = (ProgressBar)findViewById(R.id.progress_bar);
progressBar.setOnClickListener(this);
progressBar.setMax(mp.getDuration());
progressBar.setProgress(mp.getCurrentPosition());
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case 1:
mp.pause();
isPlaying = false;
stopButton.setVisibility(View.GONE);
restartButton.setVisibility(View.VISIBLE);
break;
case 2:
mp.start();
isPlaying = true;
stopButton.setVisibility(View.VISIBLE);
restartButton.setVisibility(View.GONE);
break;
case 3:
mp.stop();
mp.reset();
try {
mp.setDataSource(mPath);
} 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();
}
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
isPlaying = false;
progressBar.setProgress(0);
stopButton.setVisibility(View.GONE);
restartButton.setVisibility(View.VISIBLE);
break;
case 4:
this.setResult(NEXT);
finish();
break;
default :
break;
}
}
class UpdateStatus extends Thread{
@Override
public void run() {
while (true) {
if( isPlaying)
mHandler.post(new Runnable() {
public void run(){
try {
progressBar.setMax(mp.getDuration());
progressBar.setProgress(mp.getCurrentPosition());
int pos = 0;
pos = mp.getCurrentPosition();
int min = (pos/1000)/60;
int sec = (pos/1000)%60;
int maxPos = mp.getDuration();
int maxMin = (maxPos/1000)/60;
int maxSec = (maxPos/1000)%60;
String maxTime = new String();
if(maxSec<10){
maxTime = ""+maxMin+":0"+maxSec;
}else{
maxTime = ""+maxMin+":"+maxSec;
}
if(sec<10)
mTimepos.setText(""+min+":0"+sec+"/"+maxTime);
else
mTimepos.setText(""+min+":"+sec+"/"+maxTime);
} catch (Exception e) {
e.printStackTrace();
}
}
});
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
AndroidMP3.zip_android_androidMP3
共28个文件
class:12个
xml:5个
java:3个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 143 浏览量
2022-09-24
03:09:56
上传
评论
收藏 55KB ZIP 举报
温馨提示
一个android下的MP3播放器的应用程序,展示了如何使用android系统的媒体API
资源推荐
资源详情
资源评论
收起资源包目录
AndroidMP3.zip (28个子文件)
AndroidMP3
bin
classes.dex 12KB
com
xmobileapp
Mp3
R$layout.class 461B
R$id.class 597B
R$drawable.class 436B
Mp3Filter.class 572B
R$attr.class 340B
ShowPlayer$UpdateStatus.class 1KB
R.class 531B
R$string.class 427B
ShowPlayer$UpdateStatus$1.class 2KB
Mp3.class 5KB
ShowPlayer.class 5KB
Mp3$ListAdapter.class 2KB
AndroidMP3.apk 16KB
resources.ap_ 8KB
res
drawable
icon.png 3KB
button_pause.png 3KB
values
strings.xml 157B
layout
main.xml 382B
mediaplayer.xml 2KB
arrayset.xml 390B
assets
default.properties 449B
gen
com
xmobileapp
Mp3
R.java 1KB
src
com
xmobileapp
Mp3
Mp3.java 5KB
ShowPlayer.java 6KB
.project 813B
.classpath 273B
AndroidManifest.xml 1005B
共 28 条
- 1
资源评论
小贝德罗
- 粉丝: 68
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功