【发布时间】:2013-10-28 04:33:16
【问题描述】:
我打开了一个新项目-
现在我想做的是—— 通过按下按钮,我想要播放 mp3 文件 - 而且每次按下按钮时,声音文件都会再次从头开始播放 - 所以假设 mp3 的长度为 10 秒,而我按下按钮,它正在播放,4秒后我再次按下按钮,声音将再次播放。
现在我想知道的是—— 1- 我应该把 mp3 文件放在哪里?
2-我必须添加什么代码才能在按下按钮时播放 mp3 文件(我们称之为 mp3 文件 click_sound.mp3)?
3- 我需要在代码中添加什么,以便每次按下按钮时都会再次播放声音?
这是 MainActivity.java 的代码 -
package com.example.test1;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
这是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" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/play" />
</RelativeLayout>
【问题讨论】:
-
可耻的是有人决定取消我的评分——只是想理解一些事情——为什么仅仅因为某人不理解某事而惩罚他?!
-
你有数百个这样的例子,你不应该懒惰地在互联网上搜索它。好的开始就在这里:developer.android.com/reference/android/media/MediaPlayer.html
-
查看 Chris 的回答:“stackoverflow.com/questions/3369068/…”。希望对你有帮助