如何在Android中播放音频文件?

Nat*_*iya 12 java android

我有代码播放.ogg音频文件,我从互联网上下载.我没有错误,所以我可以运行它,但随后应用程序崩溃:

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;
}
}
Run Code Online (Sandbox Code Playgroud)

我想我有两个问题:

  1. 我把它放在main.xml文件中:

    <?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>
    
    我不知道这是否正确.

  2. 我把文件sound1.ogg中的workspace->SoundPlay2文件夹,因为在res文件夹我有问题,而且,我试图把它存在两个资源文件夹.

这是我的控制台:

[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\Natalia\workspace\PlaySound2\res\layout\main.out.xml:1: error: Error parsing XML: no element found
Run Code Online (Sandbox Code Playgroud)

我从" Android Sounds - Tutorial "中选择了这个例子.我想播放一个音频文件,更具体地说,是一个.wav文件.

我不知道在哪里可以找到有关MediaPlayer类允许的文件及其特性的信息(durantion,sample rate ......)你能告诉我在哪里可以找到这个吗?

man*_*usg 6

在 res 文件夹中创建 raw 文件夹并将文件添加到其中。然后使用播放文件

soundID = soundPool.load(R.raw.sound1, 1);
Run Code Online (Sandbox Code Playgroud)

另请参阅这篇文章了解您的 main.xml 问题。


For*_*rce 5

我以前从未见过 SoundPool 类,但我建议使用 MediaPlayer 类:

mMediaPlayer = new MediaPlayer(this, R.raw.yourfile);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.prepare();
mMediaPlayer.start();
Run Code Online (Sandbox Code Playgroud)

确保将文件放入文件夹 PROJECT/res/raw/ (如果不存在则创建它)

您的 main.xml 也有问题。可以请您发布吗?

  • 说实话,如果你不知道要改什么,那说明你不理解自己的代码。删除与 Soundpool 交互的所有行,然后阅读以下内容:http://developer.android.com/reference/android/media/MediaPlayer.html (4认同)

Bob*_*son 3

播放某种蜂鸣器的示例。在raw文件夹下的res文件夹中我有一个buzzer.wav

蜂鸣器的播放方法:

     /**
 * 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();
        }

    });

}
Run Code Online (Sandbox Code Playgroud)