问题描述
我打开了一个新项目 -
I have open a new project -
现在我想做的是——通过按下按钮,我想要播放一个 mp3 文件 - 而且每次按下按钮时,声音文件都会从头开始播放 - 所以假设 mp3 是 10 秒长,我按下按钮,它正在播放,4 秒后我再次按下按钮,声音将再次播放.
Now what I would like to do is this -By pressing on the button I want an mp3 file being played - and also that each time the button is pressed than the sound file will start playing from the start of it once again - so let's say that the mp3 is 10 sec long, and I pressed the button and it's playing and after 4 sec I pressed the button again than the sound will be played again.
现在我想知道的是——1- 我应该把 mp3 文件放在哪里?
Now what I would like to know is-1- Where should I put the mp3 file?
2-我必须添加什么代码才能在按下按钮时播放 mp3 文件(我们将 mp3 文件称为 click_sound.mp3)?
2-what code do I have to add in order that when the button is pressed than the mp3 file will be played (let's call the mp3 file click_sound.mp3)?
3- 我需要在代码中添加什么才能在每次按下按钮时再次播放声音?
3- What I need to add to the code in order that the sound will be played again each time I will pressed the button?
这是MainActivity.java的代码-
This is the code of the 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
and this is the 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>
推荐答案
您应该将 mp3 文件放在/assets 文件夹中.
You should put mp3 file in /assets folder.
将此代码放在 onCreate()
方法中 setContentView()
put this code inside onCreate()
method after setContentView()
final MediaPlayer mp = new MediaPlayer();
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(mp.isPlaying())
{
mp.stop();
}
try {
mp.reset();
AssetFileDescriptor afd;
afd = getAssets().openFd("AudioFile.mp3");
mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
mp.prepare();
mp.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
3.每次按下按钮都会再次播放声音.您不必为此编写任何额外的代码.
3.sound will be played again each time you press button. You don't have to write any extra code for that.
注意AudioFile.mp3是/assets文件夹下的mp3文件名
希望这个回答有帮助:)
Hope this answer is helpful:)
这篇关于android - 如何让按钮点击每次按下时播放声音文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!