Android简易本地音乐播放器,简单实现Android本地音乐播放器

音乐播放需要调用service,在此,只是简单梳理播放流程。

public class playmusicservice extends service {

//绑定服务 调用服务的方法。

@override

public ibinder onbind(intent intent) {

return null;

}

}

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context=".mainactivity" >

android:id="@+id/et_path"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:hint="请输入要播放文件的路径" />

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal" >

android:id="@+id/bt_play"

android:οnclick="play"

android:layout_width="0dip"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="播放" />

android:id="@+id/bt_pause"

android:οnclick="pause"

android:layout_width="0dip"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="暂停" />

android:id="@+id/bt_stop"

android:οnclick="stop"

android:layout_width="0dip"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="停止" />

android:id="@+id/bt_replay"

android:οnclick="replay"

android:layout_width="0dip"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="重播" />

public class mainactivity extends activity {

private edittext et_path;

private mediaplayer mediaplayer;

private button bt_play,bt_pause,bt_stop,bt_replay;

@override

protected void oncreate(bundle savedinstancestate) {

super.oncreate(savedinstancestate);

setcontentview(r.layout.activity_main);

et_path = (edittext) findviewbyid(r.id.et_path);

bt_play = (button) findviewbyid(r.id.bt_play);

bt_pause = (button) findviewbyid(r.id.bt_pause);

bt_stop = (button) findviewbyid(r.id.bt_stop);

bt_replay = (button) findviewbyid(r.id.bt_replay);

}

/**

* 播放

* @param view

*/

public void play(view view) {

string filepath = et_path.gettext().tostring().trim();

file file = new file(filepath);

if(file.exists()){

try {

mediaplayer = new mediaplayer();

mediaplayer.setdatasource(filepath);//设置播放的数据源。

mediaplayer.setaudiostreamtype(audiomanager.stream_music);

mediaplayer.prepare();//准备开始播放 播放的逻辑是c代码在新的线程里面执行。

mediaplayer.start();

bt_play.setenabled(false);

mediaplayer.setoncompletionlistener(new oncompletionlistener() {

@override

public void oncompletion(mediaplayer mp) {

bt_play.setenabled(true);

}

});

} catch (exception e) {

e.printstacktrace();

toast.maketext(this, "播放失败", 0).show();

}

}else{

toast.maketext(this, "文件不存在,请检查文件的路径", 0).show();

}

}

/**

* 暂停

* @param view

*/

public void pause(view view) {

if("继续".equals(bt_pause.gettext().tostring())){

mediaplayer.start();

bt_pause.settext("暂停");

return;

}

if(mediaplayer!=null&&mediaplayer.isplaying()){

mediaplayer.pause();

bt_pause.settext("继续");

}

}

/**

* 停止

* @param view

*/

public void stop(view view) {

if(mediaplayer!=null&&mediaplayer.isplaying()){

mediaplayer.stop();

mediaplayer.release();

mediaplayer = null;

}

bt_pause.settext("暂停");

bt_play.setenabled(true);

}

/**

* 重播

* @param view

*/

public void replay(view view) {

if(mediaplayer!=null&&mediaplayer.isplaying()){

mediaplayer.seekto(0);

}else{

play(view);

}

bt_pause.settext("暂停");

}

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持萬仟网。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值