本次编程运用了MediaPlayer类,实现对手机歌曲的读取。
1.上下首播放;
2,。音量的设置;
3.播放进度的显示。
代码:MediaPlayerActivity.java
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import com.briup.R;
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.Toast;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
public class MediaPlayerActivity extends Activity
{
MediaPlayer mp;
SeekBar bar;
SeekBar vbar;
TextView currentTime;
TextView allTime;
Handler handler = new Handler();
File path;
List<String> files;
String currentfile;
TextView filenameTv;
TextView txtLrc;// 歌词
String lrccon;// 歌词内容
// 子线程监听进度的改变
private Runnable thread = new Runnable()
{
@Override
public void run()
{
updateTextView();
playNext(true);
showLrc();
handler.postDelayed(thread, 1000);
}
};
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
filenameTv = (TextView) findViewById(R.id.filename);
allTime = (TextView) findViewById(R.id.all);
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
path = Environment.getExternalStorageDirectory();
} else
{
Toast.makeText(this, "请插入sdcard", Toast.LENGTH_SHORT).show();
return;
}
// 获取所有音乐文件
files = FileUtil.getFiles(path);
mp = new MediaPlayer();
// 刚开始播放时初始化
play(this.getIntent().getStringExtra("file"));
// 显示歌词
txtLrc = (TextView) findViewById(R.id.lrc);
isExistLrc();
// 控制音轨
bar = (SeekBar) findViewById(R.id.bar);
bar.setMax(mp.getDuration());
currentTime = (TextView) findViewById(R.id.current);
currentTime.setText("0:00");
new Thread(thread).start();
bar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
@Override
public void onStopTrackingTouch(SeekBar seekBar)
{
}
@Override
public void onStartTrackingTouch(SeekBar seekBar)
{
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
if (fromUser)
{
mp.seekTo(progress);
}
}
});
// 控制音量
vbar = (SeekBar) findViewById(R.id.vbar);
final AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
vbar.setMax(am.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
vbar.setProgress(am.getStreamVolume(AudioManager.STREAM_MUSIC));
vbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
@Override
public void onStopTrackingTouch(SeekBar seekBar)
{
}
@Override
public void onStartTrackingTouch(SeekBar seekBar)
{
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
am.setStreamVolume(AudioManager.STREAM_MUSIC, vbar.getProgress(), 0);
}
});
final ImageView play = (ImageView) findViewById(R.id.play);
final ImageView pre = (ImageView) findViewById(R.id.pre);
final ImageView next = (ImageView) findViewById(R.id.next);
final ImageView stop = (ImageView) findViewById(R.id.stop);
ImageView volumn = (ImageView) findViewById(R.id.volumn);
// 播放
play.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
if (!mp.isPlaying())
{
mp.start();
play.setImageResource(R.drawable.pause);
} else if (mp.isPlaying())
{
mp.pause();
play.setImageResource(R.drawable.play);
}
}
});
// 停止
stop.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
mp.stop();
play.setImageResource(R.drawable.play);
try
{
mp.prepare();
} catch (Exception e)
{
e.printStackTrace();
}
}
});
// 上一首
pre.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
int pos = files.indexOf(currentfile);
if (pos - 1 >= 0)
{
play(files.get(pos - 1));
mp.start();
play.setImageResource(R.drawable.pause);
} else
Toast.makeText(MediaPlayerActivity.this, "没有歌曲", Toast.LENGTH_SHORT).show();
}
});
// 下一首
next.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// 播放下一首歌
playNextMusic();
play.setImageResource(R.drawable.pause);
}
});
}
// 播放
private void play(String filename)
{
mp.reset();
try
{
// Log.i("MediaPlayerActivity","文件内存:"+files.size()+"");
// Log.i("MediaPlayerActivity", "第一首歌:"+filename);
mp.setDataSource(path + "/" + filename);
currentfile = filename;
filenameTv.setText(currentfile);
mp.prepare();
int m = mp.getDuration() / 1000;
int s = m / 60;
int add = m % 60;
allTime.setText(s + ":" + add);
lrccon = PlayLrc.read(path, currentfile);
} catch (Exception e)
{
e.printStackTrace();
}
}
public void onDestroy()
{
mp.release();
mp = null;
super.onDestroy();
}
/**
* 更新进度
*/
private void updateTextView()
{
if (mp != null)
{
int m = mp.getCurrentPosition() / 1000;
int s = m / 60;
int add = m % 60;
if (add < 10)
currentTime.setText(s + ":0" + add);
else
currentTime.setText(s + ":" + add);
bar.setProgress(mp.getCurrentPosition());
}
}
/**
* 播放下一首
*
* @param flag
* true:表示自动播放下一首
*/
private void playNext(boolean flag)
{
if (!flag)
{
playNextMusic();
} else
{
if (currentTime.getText().equals(allTime.getText()))
{
playNextMusic();
}
}
}
/**
* 播放一首歌
*/
private void playNextMusic()
{
int pos = files.indexOf(currentfile);
if (pos + 1 < files.size())
{
play(files.get(pos + 1));
mp.start();
} else
{
play(files.get(0));
mp.start();
}
isExistLrc();
}
/**
* 判断歌词是否存在
*/
private void isExistLrc()
{
if (PlayLrc.read(path, currentfile).length() == 0)
txtLrc.setText("歌词不存在");
}
/**
* 显示歌词
*/
private void showLrc()
{
BufferedReader br = new BufferedReader(new StringReader(lrccon));
String temp;
try
{
while ((temp = br.readLine()) != null)
{
if (mp != null && temp.substring(temp.indexOf("]") + 1).length() > 0)
{
Log.i("test", "显示歌词线程进来了");
String time = PlayLrc.formatMS(mp.getCurrentPosition());
if (temp.contains(time))
{
Log.i("test", "歌词进来了");
txtLrc.setText(temp.substring(temp.indexOf("]") + 1));
br.close();
}
}
}
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
功能界面:
程序中又增加了歌曲列表的展示:
代码:MainList.java
import java.io.File;
import com.briup.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainList extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
ListView list = (ListView) findViewById(R.id.list);
if (getPath() == null)
alert("信息提示","请插入sd卡!");
else if(FileUtil.getFiles(getPath())==null)
alert("信息提示","sd卡没有歌曲");
else{
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, FileUtil.getFiles(getPath()));
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Intent intent=new Intent(MainList.this,MediaPlayerActivity.class);
intent.putExtra("file",((TextView)view).getText());
startActivity(intent);
}
});
}
}
//弹出对话框
private void alert(String title,String msg)
{
AlertDialog.Builder dialog = new Builder(this);
dialog.setTitle(title);
dialog.setMessage(msg);
dialog.setPositiveButton("退出", new OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
}
});
dialog.create().show();
}
private File getPath()
{
File path = null;
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
path = Environment.getExternalStorageDirectory();
}
return path;
}
}
列表界面:ListView
列表类展示:
Main.xml代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dip" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/ll"
android:layout_marginBottom="10dip"
android:orientation="vertical" >
<TextView
android:id="@+id/filename"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="音乐播放器" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/bg" />
<TextView
android:id="@+id/lrc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:id="@+id/ll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/current"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0:00" />
<SeekBar
android:id="@+id/bar"
android:layout_width="240dip"
android:layout_height="8px"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:progressDrawable="@drawable/seekbar_img"
android:thumb="@drawable/bthumb" />
<TextView
android:id="@+id/all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0:00" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dip"
android:gravity="center_vertical"
android:orientation="horizontal" >
<ImageView
android:id="@+id/pre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:src="@drawable/pre" />
<ImageView
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:src="@drawable/play" />
<ImageView
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:src="@drawable/reset" />
<ImageView
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:src="@drawable/next" />
<ImageView
android:id="@+id/volumn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:src="@drawable/volumn" />
<SeekBar
android:id="@+id/vbar"
android:layout_width="94dip"
android:layout_height="6px"
android:layout_marginLeft="5dip"
android:progressDrawable="@drawable/vseekbar_img"
android:thumb="@drawable/thumb" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
List.xml代码;
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="5dip" >
<TextView
android:id="@+id/filename"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="歌曲列表" />
<ListView android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="@android:color/white"
></ListView>
<Button android:id="@+id/exit"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:text="退出"
android:layout_centerInParent="true"
android:background="@android:color/background_dark"/>
</RelativeLayout>