本文编写于 777 天前,最后修改于 743 天前,其中某些信息可能已经过时。
效果图:

事前准备
首先要准备音频文件并在res文件夹内新建“raw”文件夹。然后放入音频即可
我这里准备的是练习时长两年半的个人练习生(
[Meting]
[Music server="netease" id="444267215" type="song"/]
[/Meting]
核心代码
Java代码中,主要使用MediaPlayer的方法完成对raw文件夹下的音频文件的播放。
playFromRaw
private void playFromRaw() {
mper = MediaPlayer.create(this, R.raw.swin);
mper.start();
}
当按下btn_start按钮(开始播放)的时候:
number++;
playFromRaw();
if (mper.isPlaying()) {
Toast.makeText(MainActivity.this, "鸡已经在太美了",Toast.LENGTH_SHORT).show();
}
txvName.setText("鸡你太美" + "x" + number);
tv_geci.setVisibility(View.VISIBLE);
tv_geci.setText("这里放歌词")
当按下btn_stop按钮(别打篮球了!)时:
if (mper.isPlaying()) {
mper.stop();
Toast.makeText(MainActivity.this,"如果你点开始点多了,是停不下来的.....(我也不知道为什么不行)",Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "先开始播放呗?",Toast.LENGTH_SHORT).show();
}
UI界面
接下来是界面。
页面中含有按钮x2、Spinner控件x1。
所以我的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" >
<Button
android:id="@+id/btnPickAudio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="开始播放" />
<TextView
android:id="@+id/txvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/btnPickAudio"
android:text="文件名:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/txvUri"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/txvName"
android:text="路径:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="@+id/sp_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/btnPickAudio"
android:entries="@array/music" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/btn_stop"
android:layout_alignRight="@+id/btn_stop"
android:layout_below="@+id/txvUri" >
<TextView
android:id="@+id/tv_geci"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/txvUri"
android:textAppearance="?android:attr/textAppearanceSmall" />
</ScrollView>
<WebView
android:id="@+id/mt_web_view"
android:layout_width="200sp"
android:layout_height="200sp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/btn_stop" />
<Button
android:id="@+id/btn_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="NMD!快停下!别打篮球了!" />
</RelativeLayout>
就是这样,打包apk之后发给朋友整蛊一下吧(前提是关系特别好的好 兄 弟)