android示例之音乐播放器第四天

来源:互联网 发布:仓鼠翻译软件 编辑:程序博客网 时间:2024/06/05 06:10

音乐播放器时隔很久,今天回归了。每天可以心烦意乱,可以心猿意马,可以海阔天空,但是每天都要编点代码。

今天要完成的任务是点击一个mp3文件,然后播放。

课程中介绍的完成这个功能的形式是:点击列表中的一个文件,把这个文件对应的vo类放入到Intent中,并传递给一个新的Activity,这个Activity负责播放这个文件。

感觉这个形式有点复杂。。。总之,这个主要是实现功能,看代码,注释写的很详细。

布局文件:player.xml

<?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="horizontal"     android:paddingLeft="10dp"    android:paddingRight="10dp"    android:paddingTop="1dp"    android:paddingBottom="1dp">        <ImageButton         android:id="@+id/start"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/start"/>    <ImageButton         android:id="@+id/pause"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/pause"/>    <ImageButton         android:id="@+id/stop"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/stop"/></LinearLayout>

点击列表中的一个记录,把文件传递给播放文件的Activity:

protected void onListItemClick(ListView l, View v, int position, long id) {super.onListItemClick(l, v, position, id);Mp3Info mp3Info=mp3Infos.get(position);Intent intent=new Intent();intent.putExtra("mp3Info", mp3Info);intent.setClass(this, PlayerActivity.class);startActivity(intent);}

播放文件的Activity:PlayerActivity.java

package com.lyc.activity;import java.io.File;import android.app.Activity;import android.content.Intent;import android.media.MediaPlayer;import android.net.Uri;import android.os.Bundle;import android.os.Environment;import android.view.View;import android.view.View.OnClickListener;import android.widget.ImageButton;import com.lyc.vo.Mp3Info;public class PlayerActivity extends Activity{private ImageButton start;private ImageButton pause;private ImageButton stop;//播放媒体的对象private MediaPlayer mediaPlayer;//设置文件播放的状态private boolean isStart=false;private boolean isPause=false;private boolean isStop=false;//播放的对象private Mp3Info mp3Info=null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.player);//得到传递过来的IntentIntent intent=getIntent();mp3Info=(Mp3Info) intent.getSerializableExtra("mp3Info");start=(ImageButton)findViewById(R.id.start);pause=(ImageButton)findViewById(R.id.pause);stop=(ImageButton)findViewById(R.id.stop);start.setOnClickListener(new StartListener());pause.setOnClickListener(new PauseListener());stop.setOnClickListener(new StopListener());}/** * 点击开始按钮的监听类 * @author lyc */class StartListener implements OnClickListener{public void onClick(View v) {if(isStart==false){if(isPause==false){//如果mediaPlayer没有停止,而且没有暂停Uri uri=Uri.parse("file://"+getFilePath(mp3Info));mediaPlayer=MediaPlayer.create(PlayerActivity.this, uri);mediaPlayer.setLooping(false);mediaPlayer.start();isStart=true;isStop=false;isPause=false;}else{//如果mediaPlayer没有停止,而且暂停mediaPlayer.start();isStart=true;isPause=false;isStop=false;}}}}/** *点击暂停按钮的监听类  * @author lyc */class PauseListener implements OnClickListener{public void onClick(View v) {if(mediaPlayer!=null){if(isStop==false){if(isStart==false){//如果文件存在,并且没有停止,没有开始mediaPlayer.start();isStart=true;isPause=false;isStop=false;}else{//如果文件存在,并且没有停止,但是开始mediaPlayer.pause();isStart=false;isPause=true;isStop=false;}}}}}/** * 点击停止按钮的监听类 * @author lyc */class StopListener implements OnClickListener{public void onClick(View v) {if(mediaPlayer!=null){if(isStop==false){mediaPlayer.stop();mediaPlayer.release();isStart=false;isPause=false;isStop=true;}}}}/** * 通过文件对象得到文件所在SD卡的路径 * @param mp3Info * @return 文件所在SD卡的路径 */public String getFilePath(Mp3Info mp3Info){String SDPath=Environment.getExternalStorageDirectory().getAbsolutePath();String path=SDPath+File.separator+"mp3"+File.separator+mp3Info.getMp3Name();return path;}}

最后,新加入了Activity,要在AndroidManifest.xml中注册。

0 0
原创粉丝点击