Java 如何播放 .wav 声音?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2458833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do i get a .wav sound to play?
提问by Dan
Im making an app, and i want it to make a sound when a activity is opened , the sound file is in R.raw.sound_file
, if someone could do some example code to make my app play a sound that would be great.
我正在制作一个应用程序,我希望它在活动打开时发出声音,声音文件在R.raw.sound_file
,如果有人可以做一些示例代码让我的应用程序播放很棒的声音。
回答by ayman
doesn't the android.media.MediaPlayer
class do this?
未在android.media.MediaPlayer
类做到这一点?
Reference: http://developer.android.com/reference/android/media/MediaPlayer.html
参考:http: //developer.android.com/reference/android/media/MediaPlayer.html
Example: http://developer.android.com/guide/topics/media/index.html
示例:http: //developer.android.com/guide/topics/media/index.html
Step 2 of the example says:
示例的第 2 步说:
MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_file_1);
mp.start();
In your case, I'd use the onStart()
inside your Activity class:
在你的情况下,我会onStart()
在你的 Activity 类中使用:
public class MyActivity extends Activity {
...
protected void onStart() {
super.onStart();
MediaPlayer mp = MediaPlayer.create(this, R.raw.sound_file_1);
mp.start();
}
...
}
回答by Gal Bracha
I had the same problem. this worked for me by using the application context as follows:
我有同样的问题。这通过使用应用程序上下文对我有用,如下所示:
public class MyActivity extends Activity {
...
protected void onStart() {
super.onStart();
Context appContext = getApplicationContext();
MediaPlayer mp = MediaPlayer.create(appContext , R.raw.sound_file_1);
mp.start();
}
...
}
Also, don't forget to call mp.release() once you're done
另外,不要忘记在完成后调用 mp.release()
Another,preferred option is to use the SoundPool class
另一个首选选项是使用 SoundPool 类
回答by Michael G. Workman
I have experience using the MediaPlayer object for an android app I created, and I discovered the following:
我有将 MediaPlayer 对象用于我创建的 android 应用程序的经验,我发现了以下内容:
Wav files have problems in MediaPlayer if they have a bitrate of 32kbps, but wav files of higher bit rates seem to play ok, even if it is a large wav file it will play ok as long as it has the higher bitrate.
If at all possible, use mp3 files for your audio, I encountered no problems whatsoever with mp3 audio files using the MediaPlayer object, so that is the best way to go, using google there are lots of different kinds of mp3 sounds available free from rings and dings, to dog barks, and cat meows, or whatever kind of sound you are looking for.
如果 wav 文件的比特率为 32kbps,则它们在 MediaPlayer 中会出现问题,但比特率较高的 wav 文件似乎可以正常播放,即使是大型 wav 文件,只要具有较高的比特率,它也可以正常播放。
如果可能的话,为您的音频使用 mp3 文件,我在使用 MediaPlayer 对象的 mp3 音频文件方面没有遇到任何问题,所以这是最好的方法,使用谷歌有很多不同种类的 mp3 声音可以从戒指中免费获得和叮当声,狗吠声,猫叫声,或您正在寻找的任何类型的声音。
回答by yaircarreno
Try with my code, It's works perfectly. Also you need to have the sound file .wav en res/raw
试试我的代码,它完美地工作。你还需要有声音文件 .wav en res/raw
public class PianoActivity extends Activity {
private MediaPlayer mediaPlayer = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_piano);
setupUI();
}
@Override
protected void onPause() {
super.onPause();
if (mediaPlayer != null) {
mediaPlayer.release();
mediaPlayer = null;
}
}
private void setupUI() {
findViewById(R.id.doo).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
managerOfSound();
}
});
}
private void managerOfSound() {
mediaPlayer = MediaPlayer.create(this, R.raw.doo);
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
} else {
mediaPlayer.stop();
}
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mp.reset();
mp.release();
}
});
}
}
}