如何播放 .wav 声音?

发布于 2024-08-25 13:57:15 字数 91 浏览 6 评论 0原文

我正在制作一个应用程序,我希望它在打开活动时发出声音,声音文件位于 R.raw.sound_file 中,如果有人可以做一些示例代码来使我的应用程序播放声音那就太好了。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

孤芳又自赏 2024-09-01 13:57:15

android.media.MediaPlayer 类不这样做吗?

参考: http://developer.android.com/reference/android/media/MediaPlayer .html

示例:http://developer.android.com/guide /topics/media/index.html

该示例的第 2 步表示:

MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_file_1);
mp.start();

在您的情况下,我将在您的 Activity 类中使​​用 onStart()

public class MyActivity extends Activity {
   ... 
   protected void onStart() {
      super.onStart();
      MediaPlayer mp = MediaPlayer.create(this, R.raw.sound_file_1);
      mp.start();
   }
   ...
}

doesn't the android.media.MediaPlayer class do this?

Reference: http://developer.android.com/reference/android/media/MediaPlayer.html

Example: http://developer.android.com/guide/topics/media/index.html

Step 2 of the example says:

MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_file_1);
mp.start();

In your case, I'd use the onStart() inside your Activity class:

public class MyActivity extends Activity {
   ... 
   protected void onStart() {
      super.onStart();
      MediaPlayer mp = MediaPlayer.create(this, R.raw.sound_file_1);
      mp.start();
   }
   ...
}
夏至、离别 2024-09-01 13:57:15

我有在我创建的 Android 应用程序中使用 MediaPlayer 对象的经验,并且发现了以下内容:

  • 如果 Wav 文件的比特率为 32kbps,那么它们在 MediaPlayer 中就会出现问题,但更高比特率的 wav 文件似乎可以正常播放,甚至如果是大的 wav 文件,只要比特率较高,就可以正常播放。

  • 如果可能的话,使用 mp3 文件作为音频,我使用 MediaPlayer 对象的 mp3 音频文件没有遇到任何问题,所以这是最好的方法,使用 google 有很多不同类型的 mp3 声音可用没有铃声和叮当声、狗叫声、猫叫声或您正在寻找的任何类型的声音。

I have experience using the MediaPlayer object for an android app I created, and I discovered the following:

  • 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.

盗心人 2024-09-01 13:57:15

尝试使用我的代码,效果非常好。您还需要声音文件 .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();
        }
    });
}

}

Try with my code, It's works perfectly. Also you need to have the sound file .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();
        }
    });
}

}

温柔一刀 2024-09-01 13:57:15

我也有同样的问题。
这通过使用应用程序上下文对我有用,如下所示:

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

另外,完成后不要忘记调用 mp.release()

另一个首选选项是使用 SoundPool 类

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

Another,preferred option is to use the SoundPool class

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文