可以使用MediaPlayer进行视频或音频的播放。
将Mp3存放在res/raw文件夹中。或者是在sdcard上新建文件夹,并使用文件进行读取。
准备三张图片,名字分别为: play、pause、stop。
将一个名为Mp3的文件放入res/raw文件夹中。
在main.xml中:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:id="@+id/info"
android:gravity="center_horizontal"
android:layout_marginTop="8dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:text="等待音频播放"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center_horizontal">
<ImageButton
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/play"/>
<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>
<SeekBar
android:id="@+id/seekbar"
android:layout_margin="8dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
在MyMediaPlayerDemo.java中:
package com.li.mediaplayer;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
public class MyMediaPlayerDemo extends Activity {
private ImageButton play = null ;
private ImageButton pause = null ;
private ImageButton stop = null ;
private TextView info = null ;
private MediaPlayer myMediaPlayer = null ;
private SeekBar seekbar = null ;
private boolean playFlag = true ; // 播放标记
private boolean pauseFlag = true ; // 暂停标记
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.info = (TextView) super.findViewById(R.id.info) ;
this.play = (ImageButton) super.findViewById(R.id.play) ;
this.pause = (ImageButton) super.findViewById(R.id.pause) ;
this.stop = (ImageButton) super.findViewById(R.id.stop) ;
this.seekbar = (SeekBar) super.findViewById(R.id.seekbar) ;
this.play.setOnClickListener(new PlayOnClickListener()) ;
this.pause.setOnClickListener(new PauseOnClickListener()) ;
this.stop.setOnClickListener(new StopOnClickListener()) ;
this.seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListenerImpl()) ;
}
private class PlayOnClickListener implements OnClickListener{
public void onClick(View v) {
MyMediaPlayerDemo.this.myMediaPlayer = MediaPlayer.create(
MyMediaPlayerDemo.this, R.raw.mp3); // 要播放的文件
if (MyMediaPlayerDemo.this.myMediaPlayer != null) {
MyMediaPlayerDemo.this.myMediaPlayer.stop(); // 停止操作
}
MyMediaPlayerDemo.this.myMediaPlayer.setOnCompletionListener(new OnCompletionListener(){
public void onCompletion(MediaPlayer mp) {
MyMediaPlayerDemo.this.playFlag = false ; // 播放完毕
MyMediaPlayerDemo.this.myMediaPlayer.release() ; // 释放资源
}}) ;
try {
MyMediaPlayerDemo.this.myMediaPlayer.prepare() ;
MyMediaPlayerDemo.this.myMediaPlayer.start() ;
MyMediaPlayerDemo.this.info.setText("正在播放音频文件...") ;
} catch (Exception e) {
MyMediaPlayerDemo.this.info.setText("文件播放出现异常," + e) ;
}
}
}
private class PauseOnClickListener implements OnClickListener{
public void onClick(View v) {
if(MyMediaPlayerDemo.this.myMediaPlayer != null) {
if (MyMediaPlayerDemo.this.pauseFlag) { // 现在暂停
MyMediaPlayerDemo.this.myMediaPlayer.start();
MyMediaPlayerDemo.this.pauseFlag = false ;
} else {
MyMediaPlayerDemo.this.myMediaPlayer.pause(); // 暂停
MyMediaPlayerDemo.this.pauseFlag = true ;
}
}
}
}
private class StopOnClickListener implements OnClickListener{
public void onClick(View v) {
if(MyMediaPlayerDemo.this.myMediaPlayer != null) {
MyMediaPlayerDemo.this.myMediaPlayer.stop() ; // 停止
MyMediaPlayerDemo.this.info.setText("停止播放音频文件...") ;
}
}
}
private class OnSeekBarChangeListenerImpl implements OnSeekBarChangeListener {
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
}
public void onStartTrackingTouch(SeekBar seekBar) {
}
public void onStopTrackingTouch(SeekBar seekBar) {
}
}
}