How do I play an audio file in Android?
我有代码播放
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | package play.my.sound; import android.app.Activity; import android.media.AudioManager; import android.media.SoundPool; import android.media.SoundPool.OnLoadCompleteListener; import android.os.Bundle; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; public class PlaySound2Activity extends Activity { private SoundPool soundPool; private int soundID; boolean loaded = false; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); View view = findViewById(R.id.textView1); view.setOnClickListener((OnClickListener) this); // Set the hardware buttons to control the music this.setVolumeControlStream(AudioManager.STREAM_MUSIC); // Load the sound soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0); soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() { public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { loaded = true; } }); soundID = soundPool.load("sound1.ogg", 1); } public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { // Getting the user sound settings AudioManager audioManager = (AudioManager) getSystemService (AUDIO_SERVICE); float actualVolume = (float) audioManager .getStreamVolume(AudioManager.STREAM_MUSIC); float maxVolume = (float) audioManager .getStreamMaxVolume(AudioManager.STREAM_MUSIC); float volume = actualVolume / maxVolume; // Is the sound loaded already? if (loaded) { soundPool.play(soundID, volume, volume, 1, 0, 1f); Log.e("Test","Played sound"); } } return false; } } |
我想我有两个问题:
我把它放在
1 2 3 4 5 6 7 8 | <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:text="Click on the screen to start playing" android:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="fill_parent"></TextView> </LinearLayout> |
我不知道这是否正确。
我把文件
这是我的控制台:
1 2 3 4 5 6 7 8 | [2012-01-04 19:38:16 - PlaySound2] Failed to install PlaySound2.apk on device 'emulator-5554': timeout [2012-01-04 19:38:16 - PlaySound2] Launch canceled! [2012-01-04 19:47:33 - PlaySound2] Error in an XML file: aborting build. [2012-01-04 19:52:34 - PlaySound2] res\layout\main.xml:0: error: Resource entry main is already defined. [2012-01-04 19:52:34 - PlaySound2] res\layout\main.out.xml:0: Originally defined here. [2012-01-04 19:52:34 - PlaySound2] C:\Users atalia\workspace\PlaySound2 es\layout\main.out.xml:1: error: Error parsing XML: no element found |
我从"Android Sounds - Tutorial"中选择了这个例子。
我想播放一个音频文件,更具体地说是一个
我不知道在哪里可以找到关于MediaPlayer类允许的文件及其特性的信息(durantion,sample rate ......)你能告诉我在哪里可以找到这个吗?
在res文件夹中创建原始文件夹并将文件添加到其中。然后使用播放文件
1 | soundID = soundPool.load(R.raw.sound1, 1); |
并且还会看到这篇文章中的main.xml问题。
我之前从未见过Class SoundPool,但我建议使用MediaPlayer类:
1 2 3 4 | mMediaPlayer = new MediaPlayer(this, R.raw.yourfile); mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mMediaPlayer.prepare(); mMediaPlayer.start(); |
确保将文件放入PROJECT / res / raw /文件夹(如果不存在则创建它)
你的main.xml也有问题。你能发帖吗?
播放某种蜂鸣器的示例。在原始文件夹下的res文件夹中,我有一个buzzer.wav
播放此蜂鸣器的方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | /** * Method to play an alarm sound to signal half time or full time. */ private void playAlarm() { MediaPlayer mp = MediaPlayer.create(this, R.raw.buzzer); mp.start(); mp.setOnCompletionListener(new OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { mp.release(); } }); } |
首先,您需要在src main res raw目录中保存sound1.ogg文件
设置直接传递"sound1.ogg"文件,以这种方式传递文件R.raw.sound1
1 | soundID = soundPool.load(R.raw.sound1, 1); |
这不是如何实现OnClickListener,这是一个在类中实现的接口,而不是强制转换为。你这样做是为了快速修复,但是由于AudioManager首先运行所以你回想起来,所以你认为这是错误的。