声音文件作为Android的MediaPlayer的变量 [英] Sound file as variable in android MediaPlayer
问题描述
而在学习的Android / Java的的过程中,我想创建一个函数,可以从原始文件夹中起到specefic声音。
我试图定义声音文件作为一个字符串,所以该函数可以被重用。
但是我坚持正与无法解析符号。
公共类MainActivity扩展ActionBarActivity {
MediaPlayer的播放器;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
playSound(dogBark);
}公共无效playSound(字符串音效档){
玩家= MediaPlayer.create(MainActivity.this,R.raw.soundFile); //无法解析符号'音效档
player.setLooping(真);
player.start();}
...
我相信这是一个缺乏基本-的知识问题,但任何帮助是极大的AP preciated。
编辑:
以上功能效果很好,如果我添加实际的声音文件中的函数:
=玩家MediaPLayer.create(MainActivity.this,R.raw.dogBark);
不过,我所要做的就是定义声音文件时,我调用函数来代替:
playSound(dogBark);
您可以做到这一点的方式,它工作正常,我:
了playMP3(泰坦尼克号);
添加此方法:
私人无效了playMP3(字符串nameOfFile){
的MediaPlayer MPLAYER = MediaPlayer.create(此,getResources()则getIdentifier(nameOfFile,原始,getPackageName()));
mPlayer.start();
}
它会为你明确的工作。
While in the process of learning android/java i wanted to create a function that could play a specefic sound from the raw folder.
I am attempting to define the sound-file as a string, so that the function can be reused. However i am stuck with "Cannot resolve symbol".
public class MainActivity extends ActionBarActivity {
MediaPlayer player;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playSound("dogBark");
}
public void playSound(String soundFile) {
player = MediaPlayer.create(MainActivity.this,R.raw.soundFile); // Cannot resolve symbol 'soundFile'
player.setLooping(true);
player.start();
}
...
I am sure this is a basic lack-of-knowledge issue, but any help is greatly appreciated.
Edit:
The above function works well if i add the actual sound file in the function:
player = MediaPLayer.create(MainActivity.this,R.raw.dogBark);
But what i am trying to do is to define the sound file when i call the function instead:
playSound("dogBark");
You can do this way, it works fine for me:
playMp3("titanic");
Add this method:
private void playMp3(String nameOfFile){
MediaPlayer mPlayer = MediaPlayer.create(this, getResources().getIdentifier(nameOfFile, "raw", getPackageName()));
mPlayer.start();
}
It would definite work for you.
这篇关于声音文件作为Android的MediaPlayer的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!