标签:音乐播放器 mediaplayer
Android平台多媒体框架核心使用的是OpenCORE多媒体框架,在安卓系统中所有涉及音频视频的录制。解码。播放都是通过它来实现的。Android系统音频视频以及流媒体类型数据的播放有MediaPlayer类来完成。
下面进行一个实例来演示MediaPlayer的使用:
具体实现效果如下:
其中选项1,2,3分别是三种不同的音频加载方式:
方式1是内部加载,音频文件存放在/res/raw文件夹中,
方式2是本地加载,音频文件存放在本地SD卡中,
方式三为网络加载,音频文件从网络中获取。
xml文件代码如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请选择:" />
<RadioGroup
android:id="@+id/radiogroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/text1"
android:layout_marginTop="30dp">
<RadioButton
android:id="@+id/radio1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="选项1"/>
<RadioButton
android:id="@+id/radio2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="选项2"/>
<RadioButton
android:id="@+id/radio3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="选项3"/>
</RadioGroup>
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/radiogroup"
android:layout_marginTop="52dp"
android:text="你的选择是:" />
<SeekBar
android:id="@+id/seekbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/text2"
android:layout_marginTop="16dp" />
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="84dp"
android:text="停止" />
<Button
android:id="@+id/pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/stop"
android:layout_alignBottom="@+id/stop"
android:layout_centerHorizontal="true"
android:text="暂停" />
<Button
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/pause"
android:layout_alignBottom="@+id/pause"
android:layout_alignParentLeft="true"
android:text="开始" />
</RelativeLayout>
MainActivity代码如下:
public class MainActivity extends Activity implements OnSeekBarChangeListener{
private static final String music_name="music.mp3";
private static final String music_path="/res/raw/";
private static final String music_sdpath="/sdcard/huawei/";
private static final String music_network_url="http://sc1.111ttt.com/2015/5/11/05/104050035435.mp3";
private String music_play_path="";
private SeekBar seekbar=null;
private RadioGroup radiogroup;
private boolean progressflag=false;
private MediaPlayer mediaplayer;
private Timer timer;
private TimerTask timertask;
private Button button1,button2,button3;
private TextView text_path;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
music_component();
mediaplayer=new MediaPlayer();
button_handler();
seekbar.setOnSeekBarChangeListener(this);
//注册进度改变事件监听器
}
public void onProgressChanged(SeekBar seekbar, int arg1, boolean arg2) {
// TODO Auto-generated method stub
}
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
progressflag=true;
}
public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
mediaplayer.seekTo(seekbar.getProgress());
progressflag=false;
}
protected void onDestroy(){
if(mediaplayer!=null){
mediaplayer.release();
timer.cancel();
timertask.cancel();
}
super.onDestroy();
}
private void music_component(){
radiogroup=(RadioGroup)findViewById(R.id.radiogroup);
seekbar=(SeekBar)findViewById(R.id.seekbar);
button1=(Button)findViewById(R.id.play);
button2=(Button)findViewById(R.id.pause);
button3=(Button)findViewById(R.id.stop);
text_path=(TextView)findViewById(R.id.text2);
}
private void button_handler(){
radiogroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup arg0, int arg1) {
// TODO Auto-generated method stub
if(mediaplayer!=null){
mediaplayer.reset();
}
}
});
button1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
playmusic();
}
});
button2.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
pausemusic();
}
});
button3.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
stopmusic();
}
});
}
protected void stopmusic() {
// TODO Auto-generated method stub
if(mediaplayer!=null&&mediaplayer.isPlaying()){
mediaplayer.reset();
Toast.makeText(MainActivity.this, "播放结束.", Toast.LENGTH_SHORT).show();
timer.cancel();
timertask.cancel();
}
}
protected void pausemusic() {
// TODO Auto-generated method stub
if(mediaplayer!=null&&mediaplayer.isPlaying()){
Toast.makeText(MainActivity.this, "播放暂停.", Toast.LENGTH_SHORT).show();
mediaplayer.pause();
}else{
mediaplayer.start();
Toast.makeText(MainActivity.this, "继续播放.", Toast.LENGTH_SHORT).show();
}
}
private void playmusic() {
// TODO Auto-generated method stub
mediaplayer.reset();
switch (radiogroup.getCheckedRadioButtonId()) {
case R.id.radio1:
music_play_path="音乐来自于:"+music_path+music_name;
text_path.setText(music_play_path);
mediaplayer=mediaplayer.create(MainActivity.this, R.raw.music);
doPlayMusic(music_path+music_name,true);
break;
case R.id.radio2:
music_play_path="音乐来自于:"+music_sdpath+music_name;
text_path.setText(music_play_path);
doPlayMusic(music_sdpath+music_name,false);
break;
case R.id.radio3:
music_play_path="音乐来自于:"+"网络:"+music_network_url;
text_path.setText(music_play_path);
doPlayMusic(music_network_url,false);
break;
default:
break;
}
}
private void doPlayMusic(String musicpath,boolean is_res_way) {
// mp3路径和是否为内部资源加载方式,如果不是,就用setDataSource()方法
try {
if(!is_res_way){
mediaplayer.setDataSource(musicpath);
mediaplayer.prepare();
}
seekbar.setMax(mediaplayer.getDuration());
//设置进度条最大值
timer=new Timer();
timertask=new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
if(progressflag=true)
seekbar.setProgress(mediaplayer.getCurrentPosition());
//设置进度条为当前播放进度
}
};
timer.schedule(timertask,0,10);
//用计时器记录播放进度
mediaplayer.start();
mediaplayer.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer arg0) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "播放完成.", Toast.LENGTH_SHORT).show();
timer.cancel();
timertask.cancel();
seekbar.setProgress(0);
mediaplayer.reset();
}
});
//注册播放完成事件监听器
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
以上代码为实例源码,可以直接用,音乐文件的名字是"music.mp3"。
本文出自 “专属称号:菜鸟学安卓” 博客,请务必保留此出处http://bigcrab.blog.51cto.com/10626858/1709942
标签:音乐播放器 mediaplayer
原文地址:http://bigcrab.blog.51cto.com/10626858/1709942