I have a small (200kb) mp3 in the res/raw folder of my android app. I am trying to run it in an emulator from Eclipse. It is recognized as a resource in the R file but when I try to prepare/start, my activity crashes! Was there something else I needed to change, perhaps in the manifest?
我在我的Android应用程序的res / raw文件夹中有一个小(200kb)的mp3。我试图在Eclipse的模拟器中运行它。它被认为是R文件中的资源,但是当我尝试准备/启动时,我的活动崩溃了!还有其他我需要改变的东西,也许是在清单中吗?
MediaPlayer mPlayer = MediaPlayer.create(FakeCallScreen.this, R.raw.mysoundfile); try { mPlayer.prepare(); mPlayer.start(); } catch (IOException e) { // handle this later }
3 个解决方案
#1
64
When starting the activity i.e on onCreate put the following code.
当启动活动时,即在onCreate上放置以下代码。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MediaPlayer mPlayer = MediaPlayer.create(FakeCallScreen.this, R.raw.mysoundfile);
mPlayer.start();
}
When stopping the activity i.e on onDestroy put the following code.
当停止活动时,即在onDestroy上输入以下代码。
public void onDestroy() {
mPlayer.stop();
super.onDestroy();
}
Hope it helps :)
希望能帮助到你 :)
#2
7
You'll likely prefer to use the SoundPool class. It reduces latency when it's time to play the sound, and offers other niceties like being able to prioritise sounds when there are too many to play at once.
您可能更喜欢使用SoundPool类。它可以减少播放声音时的延迟,并提供其他一些细节,例如当有太多可以同时播放时能够优先处理声音。
From the docs:
来自文档:
A SoundPool is a collection of samples that can be loaded into memory from a resource inside the APK or from a file in the file system. The SoundPool library uses the MediaPlayer service to decode the audio into a raw 16-bit PCM mono or stereo stream. This allows applications to ship with compressed streams without having to suffer the CPU load and latency of decompressing during playback.
SoundPool是一组样本,可以从APK内部的资源或文件系统中的文件加载到内存中。 SoundPool库使用MediaPlayer服务将音频解码为原始的16位PCM单声道或立体声流。这允许应用程序附带压缩流,而不必承受CPU负载和播放期间解压缩的延迟。
For example:
/**
* How many sounds can be played at once.
*/
private static final int MAX_SOUND_POOL_STREAMS = 4;
/**
* Modify this as part of your own priority scheme. Higher numbers mean higher
* priority. If you don't care, it's okay to use the same priority for every
* sound.
*/
private static final int NORMAL_PRIORITY = 10;
private int mySoundId;
@Override
public void setupContent() {
this.soundPool = new SoundPool(MAX_SOUND_POOL_STREAMS,
AudioManager.STREAM_MUSIC, 100);
this.mySoundId = this.soundPool.load(this.getApplicationContext(),
R.raw.mySound, 1);
}
@Override
private void playMySound() {
this.soundPool.play(this.mySoundId, 1, 1, NORMAL_PRIORITY, 0, 1);
}
#3
2
this is a static method I use in my projects. I add it to my Utils class:
这是我在项目中使用的静态方法。我将它添加到我的Utils类:
public static void playSound(final Context context, final SoundType type)
{
new Thread(new Runnable()
{
@Override
public void run()
{
MediaPlayer mediaPlayer = new MediaPlayer();
int resId = -1;
switch (type)
{
case INCOMING_NOTIFICATION:
resId=R.raw.noti_sound;
break;
case SEND_BETTING_SLIP:
resId=R.raw.slip_sent;
break;
case TRIVIA_RIGHT_ANSWER:
resId=R.raw.game_bonus;
break;
case TRIVIA_WRONG_ANSWER:
resId=R.raw.whistle_referee_trivia_bad_answer;
break;
}
if (resId != -1)
{
mediaPlayer = MediaPlayer.create(context, resId);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setLooping(false);
mediaPlayer.start();
while (mediaPlayer.isPlaying() == true)
{
}
}
}
}).start();
}
}
now I defind an Enum (SoundType) and placed the mp3 files in raw folder under res folder.
现在我定义一个Enum(SoundType)并将mp3文件放在res文件夹下的raw文件夹中。
#1
64
When starting the activity i.e on onCreate put the following code.
当启动活动时,即在onCreate上放置以下代码。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MediaPlayer mPlayer = MediaPlayer.create(FakeCallScreen.this, R.raw.mysoundfile);
mPlayer.start();
}
When stopping the activity i.e on onDestroy put the following code.
当停止活动时,即在onDestroy上输入以下代码。
public void onDestroy() {
mPlayer.stop();
super.onDestroy();
}
Hope it helps :)
希望能帮助到你 :)
#2
7
You'll likely prefer to use the SoundPool class. It reduces latency when it's time to play the sound, and offers other niceties like being able to prioritise sounds when there are too many to play at once.
您可能更喜欢使用SoundPool类。它可以减少播放声音时的延迟,并提供其他一些细节,例如当有太多可以同时播放时能够优先处理声音。
From the docs:
来自文档:
A SoundPool is a collection of samples that can be loaded into memory from a resource inside the APK or from a file in the file system. The SoundPool library uses the MediaPlayer service to decode the audio into a raw 16-bit PCM mono or stereo stream. This allows applications to ship with compressed streams without having to suffer the CPU load and latency of decompressing during playback.
SoundPool是一组样本,可以从APK内部的资源或文件系统中的文件加载到内存中。 SoundPool库使用MediaPlayer服务将音频解码为原始的16位PCM单声道或立体声流。这允许应用程序附带压缩流,而不必承受CPU负载和播放期间解压缩的延迟。
For example:
/**
* How many sounds can be played at once.
*/
private static final int MAX_SOUND_POOL_STREAMS = 4;
/**
* Modify this as part of your own priority scheme. Higher numbers mean higher
* priority. If you don't care, it's okay to use the same priority for every
* sound.
*/
private static final int NORMAL_PRIORITY = 10;
private int mySoundId;
@Override
public void setupContent() {
this.soundPool = new SoundPool(MAX_SOUND_POOL_STREAMS,
AudioManager.STREAM_MUSIC, 100);
this.mySoundId = this.soundPool.load(this.getApplicationContext(),
R.raw.mySound, 1);
}
@Override
private void playMySound() {
this.soundPool.play(this.mySoundId, 1, 1, NORMAL_PRIORITY, 0, 1);
}
#3
2
this is a static method I use in my projects. I add it to my Utils class:
这是我在项目中使用的静态方法。我将它添加到我的Utils类:
public static void playSound(final Context context, final SoundType type)
{
new Thread(new Runnable()
{
@Override
public void run()
{
MediaPlayer mediaPlayer = new MediaPlayer();
int resId = -1;
switch (type)
{
case INCOMING_NOTIFICATION:
resId=R.raw.noti_sound;
break;
case SEND_BETTING_SLIP:
resId=R.raw.slip_sent;
break;
case TRIVIA_RIGHT_ANSWER:
resId=R.raw.game_bonus;
break;
case TRIVIA_WRONG_ANSWER:
resId=R.raw.whistle_referee_trivia_bad_answer;
break;
}
if (resId != -1)
{
mediaPlayer = MediaPlayer.create(context, resId);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setLooping(false);
mediaPlayer.start();
while (mediaPlayer.isPlaying() == true)
{
}
}
}
}).start();
}
}
now I defind an Enum (SoundType) and placed the mp3 files in raw folder under res folder.
现在我定义一个Enum(SoundType)并将mp3文件放在res文件夹下的raw文件夹中。