问题描述
所以,我一直在努力使Android的一个简单的声音效果的应用程序。
下面是相关code:
公共静态最后弦乐LOG_TAG =BCA;公共MediaPlayer的熔点;@覆盖
公共无效的onCreate(捆绑savedInstanceState)
{
Log.v(LOG_TAG,创造);
super.onCreate(savedInstanceState);
的setContentView(R.layout.main_list); 熔点为新的MediaPlayer();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
Log.v(LOG_TAG,集流类型);
播放声音();
}公共无效playSound()
{
尝试{
mp.setDataSource(R.raw.sound1);
Log.v(LOG_TAG,设置数据源);
mp.setOn preparedListener(本);
mp.setOnErrorListener(本);
。MP prepareAsync();
Log.v(LOG_TAG,preparing);
}
赶上(抛出:IllegalArgumentException五){
e.printStackTrace();
}
赶上(IllegalStateException异常五){
e.printStackTrace();
}
赶上(IOException异常五){
e.printStackTrace();
}
}在prepared公共无效(MediaPlayer的媒体播放器)
{
Log.v(LOG_TAG,说完preparing;启动);
mp.start();
Log.v(LOG_TAG开始音乐);
}公共布尔的onError(MediaPlayer的熔点,诠释E,INT F)
{
Log.v(LOG_TAG,有错误);
Log.v(LOG_TAG,MP ++ E ++ f)项;
mp.reset();
返回true;
}
基本上它获取到一组设置数据源标签,但从未完成preparing。错误code是(1,4)1显然是一个未知的错误。我已经使用了多种声音文件,其中一个我知道的作品为玩家只要使用mp.create工作时(等)
我不知道是怎么回事
在此先感谢
编辑:
所以我遵循的亚历克西斯卡地亚给了,现在有没有错误链接的例子。但是,从来没有的FileInputStream完成。该方案似乎只是搪塞。
这是新的code:
公共无效playMusic()
{
档案文件=新的文件(R.raw.music1);
Log.v(LOG_TAG设置文件);
尝试{
Log.v(LOG_TAG,在try块);
的FileInputStream是=新的FileInputStream(文件);
Log.v(LOG_TAG,设置文件输入流);
mp.setDataSource(is.getFD());
Log.v(LOG_TAG,设置数据源);
mp.setOn preparedListener(本);
mp.setOnErrorListener(本);
Log.v(LOG_TAG设置prepared /错误监听器);
。MP prepareAsync();
Log.v(LOG_TAG,preparing);
}
请参阅此问题修改code的答案是:MediaPlayer.setDataSource导致IOException异常的有效文件
但你不能做 mp.setDataSource(R.raw.sound1);
So I've been trying to make a simple sound effect app for android.Here is the relevant code:
public static final String LOG_TAG = "BCA";
public MediaPlayer mp;
@Override
public void onCreate(Bundle savedInstanceState)
{
Log.v(LOG_TAG, "creating");
super.onCreate(savedInstanceState);
setContentView(R.layout.main_list);
mp = new MediaPlayer();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
Log.v(LOG_TAG, "set stream type");
playSound();
}
public void playSound()
{
try {
mp.setDataSource("R.raw.sound1");
Log.v(LOG_TAG, "set data source");
mp.setOnPreparedListener(this);
mp.setOnErrorListener(this);
mp.prepareAsync();
Log.v(LOG_TAG, "preparing");
}
catch (IllegalArgumentException e) {
e.printStackTrace();
}
catch (IllegalStateException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
public void onPrepared(MediaPlayer mediaPlayer)
{
Log.v(LOG_TAG, "finished preparing; starting");
mp.start();
Log.v(LOG_TAG, "started music");
}
public boolean onError(MediaPlayer mp, int e, int f)
{
Log.v(LOG_TAG, "There was an error");
Log.v(LOG_TAG, mp + " " + e + " " + f);
mp.reset();
return true;
}
Basically it gets to the set "set data source" tag but never finishes preparing. the error code is (1, 4) the 1 apparently being an unknown error. I have used multiple sound files, one of which I know works as the player works when just using the mp.create( etc... )
I'm not sure what's going on here
Thanks in advance
EDIT:So I followed the example of the link that Alexis Cartier gave and now there are no errors. However, the FileinputStream never finishes. The program just seems to stall.Here is the new code:
public void playMusic()
{
File file = new File("R.raw.music1");
Log.v(LOG_TAG, "set file");
try {
Log.v(LOG_TAG, "in try block");
FileInputStream is = new FileInputStream(file);
Log.v(LOG_TAG, "set file input stream");
mp.setDataSource(is.getFD());
Log.v(LOG_TAG, "set data source");
mp.setOnPreparedListener(this);
mp.setOnErrorListener(this);
Log.v(LOG_TAG, "set on prepared/error listeners");
mp.prepareAsync();
Log.v(LOG_TAG, "preparing");
}
See the answer of this question to modify your code : MediaPlayer.setDataSource causes IOException for valid file
But you can't do mp.setDataSource("R.raw.sound1");
这篇关于Android的MediaPlayer的不是preparing。误差(1,-4)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!