当前位置: 移动技术网 > IT编程>移动开发>Android > Android MediaPlayer实现音乐播放器实例代码

Android MediaPlayer实现音乐播放器实例代码

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

中央8套节目表,充100送100,lgw

android mediaplayer实现音乐播放器

1、布局文件

<?xml version="1.0" encoding="utf-8"?> 
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent" 
  android:orientation="vertical" > 
 
  <textview 
    android:id="@+id/hint" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:padding="10px" 
    android:text="单击“开始”按钮播放音频" /> 
 
  <linearlayout 
    android:id="@+id/linearlayout1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 
  <button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="播放" /> 
 
  <button 
    android:id="@+id/button2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:enabled="false" 
    android:text="暂停" /> 
 
  <button 
    android:id="@+id/button3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:enabled="false" 
    android:text="停止" /> 
  </linearlayout> 
</linearlayout> 

2、mainactivity的成员变量

private mediaplayer player;//mediaplayer对象 
  private boolean ispause = false;//是否暂停 
  private file file;//要播放的音频文件 
  private textview hint;//声明显示提示信息的文本框 

3、oncreate()方法中获取组件

final button button1 = (button)findviewbyid(r.id.button1);//获取“播放”按钮 
    final button button2 = (button)findviewbyid(r.id.button2);//获取“暂停/继续”按钮 
    final button button3 = (button)findviewbyid(r.id.button3);//获取“停止”按钮 
    hint = (textview)findviewbyid(r.id.hint);//获取用于显示提示信息的文本框 
    file = new file("/storage/emulated/0/qqmusic/song/乔维怡 - 白月光[mqms2].mp3");//获取要播放的文件 
    if(file.exists()){ 
      player = mediaplayer.create(this, uri.parse(file.getabsolutepath()));//创建mediaplayer独享 
    }else{ 
      hint.settext("要播放的音频文件不存在!"); 
      button1.setenabled(false); 
      return; 
    } 

4、编写play()方法

private void play(){ 
    try { 
      player.reset(); 
      player.setdatasource(file.getabsolutepath());//重新设置要播放的音频 
      player.prepare();//预加载音频 
      player.start();//开始播放 
      hint.settext("正在播放音频....."); 
    } catch (exception e) { 
      e.printstacktrace(); 
    } 
  } 

5、为mediaplayer对象添加监听事件,播完重新播放

player.setoncompletionlistener(new oncompletionlistener() { 
      @override 
      public void oncompletion(mediaplayer mp) { 
        play();//重新开始播放 
      } 
    }); 

6、为播放添加单击事件监听器

button1.setonclicklistener(new onclicklistener() { 
      @override 
      public void onclick(view v) { 
        play();//开始播放音乐 
        if(ispause){ 
          button2.settext("暂停"); 
          ispause = false;//设置暂停标记变量的值为false 
        } 
        button2.setenabled(true);//“暂停/继续”按钮可用 
        button3.setenabled(true);//"停止"按钮可用 
        button1.setenabled(false);//“播放”按钮不可用 
      } 
    }); 

7、在“暂停/继续”按钮添加单击事件监听器

button2.setonclicklistener(new onclicklistener() { 
    @override 
    public void onclick(view v) { 
      if(player.isplaying()&&!ispause){ 
        player.pause();//暂停播放 
        ispause = true; 
        ((button)v).settext("继续"); 
        hint.settext("暂停播放音频..."); 
        button1.setenabled(true);//“播放”按钮可用 
      }else{ 
        player.start();//继续播放 
        ((button)v).settext("暂停"); 
        hint.settext("正在播放音频..."); 
        ispause = false; 
        button1.setenabled(false);//“播放”按钮不可用 
      } 
    } 
  }); 

8、停止按钮

button3.setonclicklistener(new onclicklistener() { 
      @override 
      public void onclick(view v) { 
        player.stop();//停止播放 
        hint.settext("停止播放音频..."); 
        button2.setenabled(false);//“暂停/继续”按钮不可用 
        button3.setenabled(false);//“停止”按钮不可用 
        button1.setenabled(true);//“播放”按钮可用 
      } 
    }); 

9、重写activity的ondestroy()方法

@override 
  protected void ondestroy() { 
    if(player.isplaying()){ 
      player.stop();//停止音频的播放 
    } 
    player.release();//释放资源 
    super.ondestroy(); 
  } 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网