当前位置: 移动技术网 > 移动技术>移动开发>Android > 简单实现Android本地音乐播放器

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

2019年07月24日  | 移动技术网移动技术  | 我要评论

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

public class playmusicservice extends service {

  //绑定服务 调用服务的方法。
  @override
  public ibinder onbind(intent intent) {
    return null;
  }

}

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  tools:context=".mainactivity" >

  <edittext
    android:id="@+id/et_path"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="请输入要播放文件的路径" />

  <linearlayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <button
      android:id="@+id/bt_play"
      android:onclick="play"
      android:layout_width="0dip"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="播放" />
    <button
       android:id="@+id/bt_pause"
      android:onclick="pause"
      android:layout_width="0dip"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="暂停" />
    <button
       android:id="@+id/bt_stop"
      android:onclick="stop"
      android:layout_width="0dip"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="停止" />
    <button
       android:id="@+id/bt_replay"
      android:onclick="replay"
      android:layout_width="0dip"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="重播" />
  </linearlayout>

</linearlayout>

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

}

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网