Android一个简单的音乐播放器MyMusic

先看实现之后的效果:

未播放:(注意播放按钮,点击暂停或者停止按钮,就会还原播放按钮)

播放中:(播放按钮变为暂停按钮)

要吃饭,所以并没有实现歌曲列表和歌词滚动功能,提示:歌曲列表可以用ListView显示出来,歌词可以用文件读取,将歌词放在一个txt文件里,然后读取显示在textView中,至于滚动,还在探索中……

我这是从SDcard里读取音乐文件的,你也可以在项目中新建一个raw文件夹,把音乐文件放在raw文件夹中,一般用这样的方法做游戏的配音功能!

项目的结构如图:

main.xml布局代码:

Code:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <AbsoluteLayout  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     xmlns:android="http://schemas.android.com/apk/res/android"  
  7.     >  
  8. <TextView  
  9.     android:id="@+id/myTextView"  
  10.     android:layout_width="wrap_content"  
  11.     android:layout_height="wrap_content"  
  12.     android:text="TextView"  
  13.     android:layout_x="19px"  
  14.     android:layout_y="12px"  
  15.     >  
  16. </TextView>  
  17. <ImageButton  
  18.     android:background="@drawable/before_xml"  
  19.     android:id="@+id/imageButtonBefore"  
  20.     android:layout_width="wrap_content"  
  21.     android:layout_height="wrap_content"  
  22.     android:layout_x="20px"  
  23.     android:layout_y="285px"  
  24.     >  
  25. </ImageButton>  
  26. <ImageButton  
  27.     android:background="@drawable/play"  
  28.     android:id="@+id/imageButtonPlay_Pause"  
  29.     android:layout_width="wrap_content"  
  30.     android:layout_height="wrap_content"  
  31.     android:layout_x="70px"  
  32.     android:layout_y="285px"  
  33.     >  
  34. </ImageButton>  
  35. <ImageButton  
  36.     android:background="@drawable/stop_xml"  
  37.     android:id="@+id/imageButtonStop"  
  38.     android:layout_width="wrap_content"  
  39.     android:layout_height="wrap_content"  
  40.     android:layout_x="120px"  
  41.     android:layout_y="285px"  
  42.     >  
  43. </ImageButton>  
  44. <ImageButton  
  45.     android:background="@drawable/next_xml"  
  46.     android:id="@+id/imageButtonNext"  
  47.     android:layout_width="wrap_content"  
  48.     android:layout_height="wrap_content"  
  49.     android:layout_x="170px"  
  50.     android:layout_y="285px"  
  51.     >  
  52. </ImageButton>  
  53. <ImageView  
  54.     android:id="@+id/imageView"  
  55.     android:layout_width="wrap_content"  
  56.     android:layout_height="wrap_content"  
  57.     android:layout_x="36px"  
  58.     android:layout_y="83px"  
  59.     android:src="@drawable/android21"  
  60.     >  
  61. </ImageView>  
  62. </AbsoluteLayout>  

MainActivity的Java代码HelloMusic.java

Code:
  1. package com.myMusic.test;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import android.app.Activity;  
  6. import android.media.MediaPlayer;  
  7. import android.os.Bundle;  
  8. import android.util.Log;  
  9. import android.view.View;  
  10. import android.widget.ImageButton;  
  11.   
  12. public class HelloMusic extends Activity implements ImageButton.OnClickListener {  
  13.     private ImageButton imageButtonBefore, imageButtonPlay_Pause,  
  14.             imageButtonNext, imageButtonStop;  
  15.     private boolean flag = true;  
  16.     public MediaPlayer mediaPlayer = null;  
  17.     public String path = new String("/sdcard/huohua.mp3");  
  18.   
  19.     /** Called when the activity is first created. */  
  20.     @Override  
  21.     public void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.main);  
  24.   
  25.         mediaPlayer = new MediaPlayer();  
  26.   
  27.         findView();  
  28.         setListener();  
  29.     }  
  30.   
  31.     public void findView() {  
  32.         imageButtonBefore = (ImageButton) findViewById(R.id.imageButtonBefore);  
  33.         imageButtonPlay_Pause = (ImageButton) findViewById(R.id.imageButtonPlay_Pause);  
  34.         imageButtonNext = (ImageButton) findViewById(R.id.imageButtonNext);  
  35.         imageButtonStop = (ImageButton) findViewById(R.id.imageButtonStop);  
  36.     }  
  37.   
  38.     public void setListener() {  
  39.         imageButtonBefore.setOnClickListener(this);  
  40.         imageButtonPlay_Pause.setOnClickListener(this);  
  41.         imageButtonNext.setOnClickListener(this);  
  42.         imageButtonStop.setOnClickListener(this);  
  43.     }  
  44.   
  45.     public void onClick(View v) {  
  46.         switch (v.getId()) {  
  47.         case R.id.imageButtonBefore:  
  48.             Log.e("======""Before");  
  49.             break;  
  50.         case R.id.imageButtonNext:  
  51.             Log.e("======""Next");  
  52.             break;  
  53.         case R.id.imageButtonPlay_Pause:  
  54.             if (flag) {  
  55.                 imageButtonPlay_Pause.setBackgroundResource(R.drawable.pause);  
  56.                 playMusic(path);  
  57.                 flag = false;  
  58.             } else {  
  59.                 if (mediaPlayer.isPlaying()) {  
  60.                     imageButtonPlay_Pause  
  61.                             .setBackgroundResource(R.drawable.play);  
  62.                     mediaPlayer.pause();  
  63.                 }  
  64.                 flag = true;  
  65.             }  
  66.             break;  
  67.         case R.id.imageButtonStop:  
  68.             if (mediaPlayer.isPlaying()) {  
  69.                 mediaPlayer.stop();  
  70.                 mediaPlayer.reset();  
  71.                 //将MediaPlayer初始化,注意,这如果初始化之后,  
  72.                 //在播放的时候必须重新设置dataSource和调用prepare();  
  73.                 imageButtonPlay_Pause.setBackgroundResource(R.drawable.play);  
  74.                 flag = true;  
  75.             }  
  76.             Log.e("======""Stop");  
  77.         }  
  78.     }  
  79.   
  80.     private void playMusic(String path) {  
  81.         // mediaPlayer.reset();  
  82.         if (flag) {  
  83.   
  84.             try {  
  85.                 mediaPlayer.setDataSource(path);  
  86.                 mediaPlayer.prepare();  
  87.             } catch (IllegalArgumentException e) {  
  88.                 // TODO Auto-generated catch block  
  89.                 e.printStackTrace();  
  90.             } catch (IllegalStateException e) {  
  91.                 // TODO Auto-generated catch block  
  92.                 e.printStackTrace();  
  93.             } catch (IOException e) {  
  94.                 // TODO Auto-generated catch block  
  95.                 e.printStackTrace();  
  96.             }  
  97.   
  98.             mediaPlayer.start();  
  99.         } else {  
  100.             mediaPlayer.start();  
  101.         }  
  102.     }  
  103. }  

before_xml.xml

Code:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector  
  3.   xmlns:android="http://schemas.android.com/apk/res/android">  
  4.   <item  
  5.     android:state_pressed="true"  
  6.     android:drawable="@drawable/before_pressed"  
  7.     />  
  8.   <item  
  9.     android:state_window_focused="false"  
  10.     android:drawable="@drawable/before"  
  11.     />  
  12.   <item  
  13.     android:state_focused="true"  
  14.     android:state_enabled="true"  
  15.     android:drawable="@drawable/before_focused"  
  16.     />  
  17.   <item  
  18.     android:state_focused="true"  
  19.     android:drawable="@drawable/before_focused"  
  20.     />  
  21. </selector>  

剩下的next_xml.xml、stop_xml.xml和before_xml.xml基本一样,这里有个暂时没有搞定的问题,不知如何设置play_pause_xml.xml的focused、pressed导致的图标变换效果。

最终效果基本还行,如果把我刚才说的那两个功能再加上在线音乐播放,一个完整的播放器就可以发布到MM上去了,加油……

接着研究我的APIDemos……

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值