标签:风飞雪未扬 从零开始学android 安卓播放视频 音频 android系统播放器
在win计算机中播放视频和音频往往会使用到计算机当中的windowsMediaPlayer播放器,而在Android中则同样会使用mediaplayer播放器对音频和视频进行播放。
下图是mediaplayer的生命周期
下面使用简单的例子来使用进行音频的播放
xim文件
<span style="font-size:18px;"><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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="16dp"
android:src="@drawable/play" />
<ImageButton
android:id="@+id/imageButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/imageButton2"
android:src="@drawable/stop" />
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/imageButton1"
android:layout_centerHorizontal="true"
android:src="@drawable/pause" />
<ImageButton
android:id="@+id/imageButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/imageButton1"
android:layout_below="@+id/imageButton1"
android:layout_marginTop="19dp"
android:src="@drawable/vioceup" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/imageButton4"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:text="等待播放音频文件"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageButton
android:id="@+id/imageButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/imageButton3"
android:layout_alignRight="@+id/imageButton3"
android:layout_alignTop="@+id/imageButton4"
android:src="@drawable/voicedown" />
</RelativeLayout>
</span>java文件
<span style="font-size:18px;">package com.example.mediaplayer;
import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private ImageButton play, stop, pause, voiceup, voicedown;// 设置ImageButton
private TextView info;// 设置信息提示栏
private MediaPlayer mediaPlayer = null;// 创建MediaPlayer对象
private Boolean PauseFlag = false;// 为play设置播放状态
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
play = (ImageButton) this.findViewById(R.id.imageButton1);
stop = (ImageButton) this.findViewById(R.id.imageButton3);
pause = (ImageButton) this.findViewById(R.id.imageButton2);
voicedown = (ImageButton) this.findViewById(R.id.imageButton5);
voiceup = (ImageButton) this.findViewById(R.id.imageButton4);
info = (TextView) this.findViewById(R.id.textView1);
// 获得音量操作对象
final AudioManager audio = (AudioManager) super
.getSystemService(Context.AUDIO_SERVICE);
// 播放按钮的监听
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 实例化MEdiaplay对象
mediaPlayer = MediaPlayer
.create(MainActivity.this, R.raw.media);
// 对播放完成设置监听操作
MainActivity.this.mediaPlayer
.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
// 播放完成后释放资源
mediaPlayer.release();
// 设置播放按钮的状态
play.setEnabled(true);
}
});
if (mediaPlayer != null) {
mediaPlayer.stop();
}
try {
// 进入播放装备状态
mediaPlayer.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(MainActivity.this, "播放出错", 2).show();
}
// 开始播放音频
mediaPlayer.start();
info.setText("正在播放音乐");
// 播放后不能对该首歌曲继续开启播放
play.setEnabled(false);
}
});
// 对暂停播放的监听
pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (mediaPlayer != null) {
// 对暂停状态进行判断,实现点击播放和再次点击暂停的功能
if (PauseFlag) {
mediaPlayer.start();
PauseFlag = false;
info.setText("正在播放音乐");
} else {
mediaPlayer.pause();
info.setText("停止播放音乐");
PauseFlag = true;
}
}
}
});
// 对停止按钮的监听
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (mediaPlayer != null) {
mediaPlayer.stop();
info.setText("停止播放音频");
play.setEnabled(true);
}
}
});
//对音量降低进行监听
voicedown.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
audio.adjustVolume(AudioManager.ADJUST_LOWER, 0);
Toast.makeText(MainActivity.this, "音量降低", 1).show();
}
});
//对音量提高进行监听
voiceup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
audio.adjustVolume(AudioManager.ADJUST_RAISE, 0);
Toast.makeText(MainActivity.this, "音量增加", 1).show();
}
});
}
}
</span>使用mediaplayer可以很方便的对音频进行控制,大家可以参照APi实现更多的功能。
下节预报:mediaplayer系统播放器(播放视频)
从零开始学android<Mediaplayer播放器组件(播放音频).四十八.>
标签:风飞雪未扬 从零开始学android 安卓播放视频 音频 android系统播放器
原文地址:http://blog.csdn.net/u013616976/article/details/40143651