标签:
很简单的一个获取网络音频播放器,有进度条,播放,暂停,停止,重新播放,支持缓存,以下是源码,希望可以帮到大家
布局文件很简单,就几个按钮,TextView,和SeekBar。
activity_audio_palyer.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:orientation="vertical" >
<TextView
android:id="@+id/tips"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="文件地址" />
<EditText
android:id="@+id/file_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="http://sc1.111ttt.com/2016/1/02/23/195231349486.mp3" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="4.0dip"
android:orientation="horizontal" >
<Button
android:id="@+id/btnPlayUrl"
android:layout_width="80dip"
android:layout_height="wrap_content"
android:text="播放" >
</Button>
<Button
android:id="@+id/btnPause"
android:layout_width="80dip"
android:layout_height="wrap_content"
android:text="暂停" >
</Button>
<Button
android:id="@+id/btnStop"
android:layout_width="80dip"
android:layout_height="wrap_content"
android:text="停止" >
</Button>
<Button
android:id="@+id/btnReplay"
android:layout_width="80dip"
android:layout_height="wrap_content"
android:text="重播" >
</Button>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dip"
android:orientation="horizontal" >
<SeekBar
android:id="@+id/skbProgress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1.0"
android:max="100"
android:paddingLeft="10dip"
android:paddingRight="10dip" >
</SeekBar>
</LinearLayout>
</LinearLayout>
</FrameLayout>
Player.java文件
public class Player implements OnBufferingUpdateListener, OnCompletionListener,
MediaPlayer.OnPreparedListener {
public MediaPlayer mediaPlayer;
private SeekBar skbProgress;
private Timer mTimer = new Timer();
private String videoUrl;
private boolean pause;
private int playPosition;
public Player(String videoUrl, SeekBar skbProgress) {
this.skbProgress = skbProgress;
this.videoUrl = videoUrl;
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setOnBufferingUpdateListener(this);
mediaPlayer.setOnPreparedListener(this);
} catch (Exception e) {
Log.e("mediaPlayer", "error", e);
}
mTimer.schedule(mTimerTask, 0, 1000);
}
/*******************************************************
* 通过定时器和Handler来更新进度条
******************************************************/
TimerTask mTimerTask = new TimerTask() {
@Override
public void run() {
if (mediaPlayer == null)
return;
if (mediaPlayer.isPlaying() && skbProgress.isPressed() == false) {
handleProgress.sendEmptyMessage(0);
}
}
};
Handler handleProgress = new Handler() {
public void handleMessage(Message msg) {
int position = mediaPlayer.getCurrentPosition();
int duration = mediaPlayer.getDuration();
if (duration > 0) {
long pos = skbProgress.getMax() * position / duration;
skbProgress.setProgress((int) pos);
}
};
};
/**
* 来电话了
*/
public void callIsComing() {
if (mediaPlayer.isPlaying()) {
playPosition = mediaPlayer.getCurrentPosition();// 获得当前播放位置
mediaPlayer.stop();
}
}
/**
* 通话结束
*/
public void callIsDown() {
if (playPosition > 0) {
playNet(playPosition);
playPosition = 0;
}
}
/**
* 播放
*/
public void play() {
playNet(0);
}
/**
* 重播
*/
public void replay() {
if (mediaPlayer.isPlaying()) {
mediaPlayer.seekTo(0);// 从开始位置开始播放音乐
} else {
playNet(0);
}
}
/**
* 暂停
*/
public boolean pause() {
if (mediaPlayer.isPlaying()) {// 如果正在播放
mediaPlayer.pause();// 暂停
pause = true;
} else {
if (pause) {// 如果处于暂停状态
mediaPlayer.start();// 继续播放
pause = false;
}
}
return pause;
}
/**
* 停止
*/
public void stop() {
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
}
@Override
/**
* 通过onPrepared播放
*/
public void onPrepared(MediaPlayer arg0) {
arg0.start();
Log.e("mediaPlayer", "onPrepared");
}
@Override
public void onCompletion(MediaPlayer arg0) {
Log.e("mediaPlayer", "onCompletion");
}
@Override
public void onBufferingUpdate(MediaPlayer arg0, int bufferingProgress) {
skbProgress.setSecondaryProgress(bufferingProgress);
int currentProgress = skbProgress.getMax()
* mediaPlayer.getCurrentPosition() / mediaPlayer.getDuration();
Log.e(currentProgress + "% play", bufferingProgress + "% buffer");
}
/**
* 播放音乐
*
* @param playPosition
*/
private void playNet(int playPosition) {
try {
mediaPlayer.reset();// 把各项参数恢复到初始状态
mediaPlayer.setDataSource(videoUrl);
mediaPlayer.prepare();// 进行缓冲
mediaPlayer.setOnPreparedListener(new MyPreparedListener(
playPosition));
} catch (Exception e) {
e.printStackTrace();
}
}
private final class MyPreparedListener implements
android.media.MediaPlayer.OnPreparedListener {
private int playPosition;
public MyPreparedListener(int playPosition) {
this.playPosition = playPosition;
}
@Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();// 开始播放
if (playPosition > 0) {
mediaPlayer.seekTo(playPosition);
}
}
}
}
MainActivity.java文件
public class MainActivity extends Activity {
private Button btnPause, btnPlayUrl, btnStop,btnReplay;
private SeekBar skbProgress;
private Player player;
private EditText file_name_text;
private TextView tipsView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_audio_palyer);
this.setTitle("在线音乐播放---ouyangpeng编写");
btnPlayUrl = (Button) this.findViewById(R.id.btnPlayUrl);
btnPlayUrl.setOnClickListener(new ClickEvent());
btnPause = (Button) this.findViewById(R.id.btnPause);
btnPause.setOnClickListener(new ClickEvent());
btnStop = (Button) this.findViewById(R.id.btnStop);
btnStop.setOnClickListener(new ClickEvent());
btnReplay = (Button) this.findViewById(R.id.btnReplay);
btnReplay.setOnClickListener(new ClickEvent());
file_name_text=(EditText) this.findViewById(R.id.file_name);
tipsView=(TextView) this.findViewById(R.id.tips);
skbProgress = (SeekBar) this.findViewById(R.id.skbProgress);
skbProgress.setOnSeekBarChangeListener(new SeekBarChangeEvent());
String url=file_name_text.getText().toString();
player = new Player(url,skbProgress);
TelephonyManager telephonyManager=(TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(new MyPhoneListener(), PhoneStateListener.LISTEN_CALL_STATE);
}
/**
* 只有电话来了之后才暂停音乐的播放
*/
private final class MyPhoneListener extends android.telephony.PhoneStateListener{
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING://电话来了
player.callIsComing();
break;
case TelephonyManager.CALL_STATE_IDLE: //通话结束
player.callIsDown();
break;
}
}
}
class ClickEvent implements OnClickListener {
@Override
public void onClick(View arg0) {
if (arg0 == btnPause) {
boolean pause=player.pause();
if (pause) {
btnPause.setText("继续");
tipsView.setText("暂停播放...");
}else{
btnPause.setText("暂停");
tipsView.setText("继续播放...");
}
} else if (arg0 == btnPlayUrl) {
player.play();
tipsView.setText("开始播放...");
} else if (arg0 == btnStop) {
player.stop();
tipsView.setText("停止播放...");
} else if (arg0==btnReplay) {
player.replay();
tipsView.setText("重新播放...");
}
}
}
class SeekBarChangeEvent implements SeekBar.OnSeekBarChangeListener {
int progress;
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// 原本是(progress/seekBar.getMax())*player.mediaPlayer.getDuration()
this.progress = progress * player.mediaPlayer.getDuration()
/ seekBar.getMax();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// seekTo()的参数是相对与影片时间的数字,而不是与seekBar.getMax()相对的数字
player.mediaPlayer.seekTo(progress);
}
}
}
OK,在项目文件AndroidManifest.xml里面添加权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.netmusic"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET" />
<!-- 注意:这里要加入一个监听电话的权限 -->
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.netmusic.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这样做出来的就很原始了,1毛钱特效都没有的那种。
按钮这些可以自己定义背景,有专门的媒体按钮,去http://www.iconfont.cn/上找,很多的。
最主要的是咱们的进度条SeekBar,,原始是不是太丑了?来,我们加个样式吧。
在style文件里面:
</style>
<style name="Widget.SeekBar.Normal" parent="@android:style/Widget.SeekBar">
<item name="android:maxHeight">8.0dip</item>
<item name="android:indeterminateOnly">false</item>
<item name="android:indeterminateDrawable">@android:drawable/progress_indeterminate_horizontal</item>
<item name="android:progressDrawable">@drawable/seekbar_horizontal</item>
<item name="android:minHeight">8.0dip</item>
<item name="android:thumb">@drawable/seek_thumb</item>
<item name="android:thumbOffset">10.0dip</item>
</style>
新建seekbar_horizontal.xml,drawable里面的
<?xml version="1.0" encoding="UTF-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background" android:drawable="@drawable/seek_bkg" />
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="2.0dip" />
<gradient android:startColor="#80ffd300" android:endColor="#a0ffcb00" android:angle="270.0" android:centerY="0.75" android:centerColor="#80ffb600" />
</shape>
</clip>
</item>
<item android:id="@android:id/progress">
<clip android:drawable="@drawable/seek" />
</item>
</layer-list>
ok,还有几个图片素材
seek.9.png
seek_bkg.9.png
seek_thumb.png
代码里面引用:
<SeekBar
android:id="@+id/skbProgress"
style="@style/Widget.SeekBar.Normal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1.0"
android:max="100"
android:paddingLeft="10dip"
android:paddingRight="10dip" >
</SeekBar>
这样就可以了,看下效果:
比原来的是不是好看很多?
希望可以帮到大家
有什么问题可以加我QQ问我:1453022932
标签:
原文地址:http://blog.csdn.net/qq_19067845/article/details/51355021