先看实现之后的效果:
未播放:(注意播放按钮,点击暂停或者停止按钮,就会还原播放按钮)
播放中:(播放按钮变为暂停按钮)
要吃饭,所以并没有实现歌曲列表和歌词滚动功能,提示:歌曲列表可以用ListView显示出来,歌词可以用文件读取,将歌词放在一个txt文件里,然后读取显示在textView中,至于滚动,还在探索中……
我这是从SDcard里读取音乐文件的,你也可以在项目中新建一个raw文件夹,把音乐文件放在raw文件夹中,一般用这样的方法做游戏的配音功能!
项目的结构如图:
main.xml布局代码:
- <?xml version="1.0" encoding="utf-8"?>
- <AbsoluteLayout
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- xmlns:android="http://schemas.android.com/apk/res/android"
- >
- <TextView
- android:id="@+id/myTextView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="TextView"
- android:layout_x="19px"
- android:layout_y="12px"
- >
- </TextView>
- <ImageButton
- android:background="@drawable/before_xml"
- android:id="@+id/imageButtonBefore"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_x="20px"
- android:layout_y="285px"
- >
- </ImageButton>
- <ImageButton
- android:background="@drawable/play"
- android:id="@+id/imageButtonPlay_Pause"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_x="70px"
- android:layout_y="285px"
- >
- </ImageButton>
- <ImageButton
- android:background="@drawable/stop_xml"
- android:id="@+id/imageButtonStop"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_x="120px"
- android:layout_y="285px"
- >
- </ImageButton>
- <ImageButton
- android:background="@drawable/next_xml"
- android:id="@+id/imageButtonNext"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_x="170px"
- android:layout_y="285px"
- >
- </ImageButton>
- <ImageView
- android:id="@+id/imageView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_x="36px"
- android:layout_y="83px"
- android:src="@drawable/android21"
- >
- </ImageView>
- </AbsoluteLayout>
MainActivity的Java代码HelloMusic.java
- package com.myMusic.test;
- import java.io.IOException;
- import android.app.Activity;
- import android.media.MediaPlayer;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.ImageButton;
- public class HelloMusic extends Activity implements ImageButton.OnClickListener {
- private ImageButton imageButtonBefore, imageButtonPlay_Pause,
- imageButtonNext, imageButtonStop;
- private boolean flag = true;
- public MediaPlayer mediaPlayer = null;
- public String path = new String("/sdcard/huohua.mp3");
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- mediaPlayer = new MediaPlayer();
- findView();
- setListener();
- }
- public void findView() {
- imageButtonBefore = (ImageButton) findViewById(R.id.imageButtonBefore);
- imageButtonPlay_Pause = (ImageButton) findViewById(R.id.imageButtonPlay_Pause);
- imageButtonNext = (ImageButton) findViewById(R.id.imageButtonNext);
- imageButtonStop = (ImageButton) findViewById(R.id.imageButtonStop);
- }
- public void setListener() {
- imageButtonBefore.setOnClickListener(this);
- imageButtonPlay_Pause.setOnClickListener(this);
- imageButtonNext.setOnClickListener(this);
- imageButtonStop.setOnClickListener(this);
- }
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.imageButtonBefore:
- Log.e("======", "Before");
- break;
- case R.id.imageButtonNext:
- Log.e("======", "Next");
- break;
- case R.id.imageButtonPlay_Pause:
- if (flag) {
- imageButtonPlay_Pause.setBackgroundResource(R.drawable.pause);
- playMusic(path);
- flag = false;
- } else {
- if (mediaPlayer.isPlaying()) {
- imageButtonPlay_Pause
- .setBackgroundResource(R.drawable.play);
- mediaPlayer.pause();
- }
- flag = true;
- }
- break;
- case R.id.imageButtonStop:
- if (mediaPlayer.isPlaying()) {
- mediaPlayer.stop();
- mediaPlayer.reset();
- //将MediaPlayer初始化,注意,这如果初始化之后,
- //在播放的时候必须重新设置dataSource和调用prepare();
- imageButtonPlay_Pause.setBackgroundResource(R.drawable.play);
- flag = true;
- }
- Log.e("======", "Stop");
- }
- }
- private void playMusic(String path) {
- // mediaPlayer.reset();
- if (flag) {
- try {
- mediaPlayer.setDataSource(path);
- mediaPlayer.prepare();
- } catch (IllegalArgumentException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IllegalStateException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- mediaPlayer.start();
- } else {
- mediaPlayer.start();
- }
- }
- }
before_xml.xml
- <?xml version="1.0" encoding="utf-8"?>
- <selector
- xmlns:android="http://schemas.android.com/apk/res/android">
- <item
- android:state_pressed="true"
- android:drawable="@drawable/before_pressed"
- />
- <item
- android:state_window_focused="false"
- android:drawable="@drawable/before"
- />
- <item
- android:state_focused="true"
- android:state_enabled="true"
- android:drawable="@drawable/before_focused"
- />
- <item
- android:state_focused="true"
- android:drawable="@drawable/before_focused"
- />
- </selector>
剩下的next_xml.xml、stop_xml.xml和before_xml.xml基本一样,这里有个暂时没有搞定的问题,不知如何设置play_pause_xml.xml的focused、pressed导致的图标变换效果。
最终效果基本还行,如果把我刚才说的那两个功能再加上在线音乐播放,一个完整的播放器就可以发布到MM上去了,加油……
接着研究我的APIDemos……