您好,登錄后才能下訂單哦!
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"。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。