MainActivity.java
package com.fisnail;
import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
/**
*
* @author 粪豆的蜗牛(fisnail@163.com)
*
*/
public class MainActivity extends Activity implements OnClickListener{
/**
* 播放按钮
*/
private Button btn_id_play;
/**
* 暂停按钮
*/
private Button btn_id_pause;
/**
* 停止按钮
*/
private Button btn_id_stop;
/**
* 增大音量
*/
private Button btn_id_raise;
/**
* 减小音量
*/
private Button btn_id_lower;
/**
* 声明MediaPlayer引用
*/
private MediaPlayer mediaPlayer;
/**
* 声明AudioManager引用
*/
private AudioManager audioManager;
/**
* 最大音量值
*/
private int maxVolume;
/**
* 当前音量值
*/
private int currentVolume;
/**
* 每次调整的音量幅度
*/
private int stepVolume;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_id_play =(Button) findViewById(R.id.btn_main_play);
btn_id_play.setOnClickListener(this);
btn_id_pause =(Button) findViewById(R.id.btn_main_pause);
btn_id_pause.setOnClickListener(this);
btn_id_stop =(Button) findViewById(R.id.btn_main_stop);
btn_id_stop.setOnClickListener(this);
btn_id_raise =(Button) findViewById(R.id.btn_main_raise);
btn_id_raise.setOnClickListener(this);
btn_id_lower =(Button) findViewById(R.id.btn_main_lower);
btn_id_lower.setOnClickListener(this);
mediaPlayer = new MediaPlayer();//创建MediaPlayer对象
try {
mediaPlayer.setDataSource("/sdcard/test.ogg");//设置要播放的资源
mediaPlayer.prepare();//进行缓冲准备
} catch (IOException e) {
e.printStackTrace();
}
//创建AudioManager对象引用
audioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
//获取最大音乐音量
maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
stepVolume =maxVolume/10;//每次调整音量幅度
}
@Override
public void onClick(View v) {
int tempVolume =0;
switch (v.getId()) {
case R.id.btn_main_play:
mediaPlayer.start();//播放音乐
Toast.makeText(getBaseContext(), "开始播放音乐", Toast.LENGTH_SHORT).show();
break;
case R.id.btn_main_pause:
mediaPlayer.pause();//暂停音乐
Toast.makeText(getBaseContext(), "暂停播放音乐", Toast.LENGTH_SHORT).show();
break;
case R.id.btn_main_stop:
mediaPlayer.stop();
try {
mediaPlayer.prepare();//进入准备状态
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(getBaseContext(), "停止播放音乐",
Toast.LENGTH_SHORT).show();
break;
case R.id.btn_main_raise:
//获取当前音量
currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
tempVolume =currentVolume+stepVolume;
//最大音量不得大于系统最大音量
currentVolume = tempVolume>maxVolume?maxVolume:tempVolume;
/**
* void android.media.AudioManager.setStreamVolume
* (int streamType, int index, int flags)
* streamType:声音类型
* index:设置音量的索引值,音量大小
* flags:标记,默认设置为AudioManager.FLAG_PLAY_SOUND
*/
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
currentVolume, AudioManager.FLAG_PLAY_SOUND);
Toast.makeText(getBaseContext(), "增大音量"+currentVolume,
Toast.LENGTH_SHORT).show();
break;
case R.id.btn_main_lower:
//获取当前音量
currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
tempVolume =currentVolume-stepVolume;
currentVolume = tempVolume>0?tempVolume:0;//最大音量不得大于系统最大音量
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume,
AudioManager.FLAG_PLAY_SOUND);
Toast.makeText(getBaseContext(), "减小音量"+currentVolume,
Toast.LENGTH_SHORT).show();
break;
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.fisnail"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.fisnail.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.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"
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" >
<!--title -->
<TextView
android:id="@+id/tv_main_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tv_main_title_text"
android:layout_centerHorizontal="true" />
<!--播放按钮 -->
<Button android:id="@+id/btn_main_play"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tv_main_title"
android:text="@string/btn_main_play_text"/>
<!--暂停按钮 -->
<Button android:id="@+id/btn_main_pause"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/btn_main_play"
android:text="@string/btn_main_pause_text"/>
<!--停止按钮 -->
<Button android:id="@+id/btn_main_stop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/btn_main_pause"
android:text="@string/btn_main_stop_text"/>
<!--增大音量按钮 -->
<Button android:id="@+id/btn_main_raise"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/btn_main_stop"
android:text="@string/btn_main_raise_text"/>
<!--减小音量按钮 -->
<Button android:id="@+id/btn_main_lower"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/btn_main_raise"
android:text="@string/btn_main_lower_text"/>
</RelativeLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">简易播放器</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="tv_main_title_text">简单音乐播放器</string>
<string name="btn_main_play_text">播放音乐</string>
<string name="btn_main_pause_text">暂停音乐</string>
<string name="btn_main_stop_text">停止音乐</string>
<string name="btn_main_raise_text">增大音量</string>
<string name="btn_main_lower_text">减小音量</string>
</resources>
遇到的问题:
播放音乐过程中,如果当音量减少至0时,报E/MP3Extractor(34): Unable to resync. Signalling end of stream.错,百度谷歌后,有网友说是Android2.2的BUG,有网友说,是少权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
加入这两个权限后,虽然当音量减少至0时不再报错,但是音频文件,还是会偶尔退出,报错依旧是:E/MP3Extractor(34): Unable to resync. Signalling end of stream.继续各种查,网上有网友建议将.mp3文件转换成.ogg文件,试后发现问题依旧存在。
PS;这个问题还未解决,希望网友给予提示,谢谢!