java主程序代码
package com.yarin.android.Examples_07_01;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import android.app.ListActivity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SeekBar;
public class Activity01 extends ListActivity
{
//暂停标志位,暂停后可以让他终止,不放标志位,暂停是终止不了的,playing只是表示播放不能代表是暂停
private boolean if_pause=false;
//未开始不允许暂停开始标志位
private boolean if_star=false;
//播放进度条
private SeekBar skb_audio=null;
private Timer mTimer;
private TimerTask mTimerTask;
/* 几个操作按钮 */
private ImageButton mFrontImageButton = null;
private ImageButton mStopImageButton = null;
private ImageButton mStartImageButton = null;
private ImageButton mPauseImageButton = null;
private ImageButton mNextImageButton = null;
private Button button1=null;
/* MediaPlayer对象 */
public MediaPlayer mMediaPlayer = null;
/* 播放列表 */
private List<String> mMusicList = new ArrayList<String>();
/* 当前播放歌曲的索引 */
private int currentListItme = 0;
/* 音乐的路径 */
private static final String MUSIC_PATH = new String("/sdcard/");
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
musicList();
mMediaPlayer = new MediaPlayer();
/* 更新显示播放列表 */
mTimer = new Timer();
mTimerTask = new TimerTask() {
@Override
public void run() {
if(mMediaPlayer.getVideoHeight()==0) {
skb_audio.setProgress(mMediaPlayer.getCurrentPosition());
}
}
};
mTimer.schedule(mTimerTask, 0, 10);
/* 构建MediaPlayer对象 */
mFrontImageButton = (ImageButton) findViewById(R.id.LastImageButton);
mStopImageButton = (ImageButton) findViewById(R.id.StopImageButton);
mStartImageButton = (ImageButton) findViewById(R.id.StartImageButton);
mPauseImageButton = (ImageButton) findViewById(R.id.PauseImageButton);
mNextImageButton = (ImageButton) findViewById(R.id.NextImageButton);
button1=(Button)findViewById(R.id.button1);
skb_audio=(SeekBar) findViewById(R.id.SeekBar01);
skb_audio.setOnSeekBarChangeListener(new SeekBarChangeEvent());
button1.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent=new Intent();
intent.setClass(Activity01.this, A.class);
startActivity(intent);
}});
//停止按钮
mStopImageButton.setOnClickListener(new ImageButton.OnClickListener()
{
@Override
public void onClick(View v)
{
if_star=false;
/* 是否正在播放 */
if (mMediaPlayer.isPlaying())
{
//重置MediaPlayer到初始状态
mMediaPlayer.reset();
}else{
if(if_pause){
mMediaPlayer.reset();
}
}
}
});
//开始按钮
mStartImageButton.setOnClickListener(new ImageButton.OnClickListener()
{
@Override
public void onClick(View v)
{
// playMusic(MUSIC_PATH + mMusicList.get(currentListItme));
if_star=true;
}
});
//暂停
mPauseImageButton.setOnClickListener(new ImageButton.OnClickListener()
{
public void onClick(View view)
{
if (mMediaPlayer.isPlaying())
{
/* 暂停 */
mMediaPlayer.pause();
if_pause=true;
}
else
{
if(if_star){
/* 开始播放 */
mMediaPlayer.start();
if_pause=false;
}
}
}
});
//下一首
mNextImageButton.setOnClickListener(new ImageButton.OnClickListener()
{
@Override
public void onClick(View arg0)
{
nextMusic();
}
});
//上一首
mFrontImageButton.setOnClickListener(new ImageButton.OnClickListener()
{
@Override
public void onClick(View arg0)
{
FrontMusic();
}
});
}
//屏蔽返回键的方法
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ( keyCode == KeyEvent.KEYCODE_BACK)
{
mMediaPlayer.stop();
this.finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
/* 当我们点击列表时,播放被点击的音乐 */
protected void onListItemClick(ListView l, View v, int position, long id)
{
currentListItme = position;
playMusic(MUSIC_PATH + mMusicList.get(position));
}
/* 播放列表 */
public void musicList()
{
//取得指定位置的文件设置显示到播放列表
File home = new File(MUSIC_PATH);
if (home.listFiles(new MusicFilter()).length > 0)
{
for (File file : home.listFiles(new MusicFilter()))
{
mMusicList.add(file.getName());
}
ArrayAdapter<String> musicList = new ArrayAdapter<String>(Activity01.this,R.layout.musicitme, mMusicList);
setListAdapter(musicList);
}
}
private void playMusic(String path)
{
try
{
/* 重置MediaPlayer */
mMediaPlayer.reset();
/* 设置要播放的文件的路径 */
mMediaPlayer.setDataSource(path);
/* 设置读取自带的影音*/
// mMediaPlayer.create(this, R.raw.test);
/* 设置读取网络影音*/
// mMediaPlayer.setDataSource("http://www.citynorth.cn/music/confucius.mp3");
/* 准备播放 */
mMediaPlayer.prepare();
/* 开始播放 */
mMediaPlayer.start();
skb_audio.setMax(mMediaPlayer.getDuration());
mMediaPlayer.setOnCompletionListener(new OnCompletionListener()
{
public void onCompletion(MediaPlayer arg0)
{
//播放完成一首之后进行下一首
nextMusic();
}
});
}catch (IOException e){}
}
/* 下一首 */
private void nextMusic()
{
if (++currentListItme >= mMusicList.size())
{
currentListItme = 0;
}
else
{
playMusic(MUSIC_PATH + mMusicList.get(currentListItme));
}
}
/* 上一首 */
private void FrontMusic()
{
if (--currentListItme<= 0)
{
currentListItme=mMusicList.size();
}
else
{
playMusic(MUSIC_PATH + mMusicList.get(currentListItme));
}
}
A类代码
package com.yarin.android.Examples_07_01;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class A extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
TextView tv=new TextView(this);
tv.setText("A页面是验证页面");
setContentView(tv);
}
}
main.xml代码
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
<SeekBar android:id="@+id/SeekBar01" android:layout_height="wrap_content"
android:layout_width="fill_parent" android:layout_y="150dip"></SeekBar>
<ImageButton
android:id="@+id/LastImageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_y="70px"
android:src="@drawable/last"/>
<ImageButton
android:id="@+id/StopImageButton"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_y="70px"
android:src="@drawable/stop" android:layout_x="60dip"/>
<ImageButton
android:id="@+id/StartImageButton"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_y="70px"
android:src="@drawable/start" android:layout_x="120dip"/>
<ImageButton
android:id="@+id/PauseImageButton"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_y="70px"
android:src="@drawable/pause" android:layout_x="180dip"/>
<ImageButton
android:id="@+id/NextImageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_y="70px"
android:src="@drawable/next" android:layout_x="240dip"/>
<Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_y="320dip"></Button>
<SurfaceView android:id="@+id/SurfaceView01"
android:layout_width="fill_parent" android:layout_height="100dip" android:layout_y="200dip"></SurfaceView>
<Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_y="320dip" android:layout_x="100dip" android:text="播放"></Button>
</AbsoluteLayout>
musicitme.xml代码
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/TextView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"/>
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
mMediaPlayer.seekTo(seekBar.getProgress());
}
}
}
/* 过滤文件类型 */
class MusicFilter implements FilenameFilter
{
public boolean accept(File dir, String name)
{
//这里还可以设置其他格式的音乐文件
return (name.endsWith(".mp3"));
}
}
里面的播放还有一些小bug,用的时候把代码改成竖屏显示的,方法都在了,自己可以根据那不好,加几个标志位改一下的。