简体   繁体   English

Android MediaPlayer未准备。 错误(1,-4)

[英]Android MediaPlayer not preparing. Error (1, -4)

So I've been trying to make a simple sound effect app for android. 因此,我一直在尝试为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. 错误代码为(1、4),1显然是未知错误。 I have used multiple sound files, one of which I know works as the player works when just using the mp.create( etc... ) 我使用了多个声音文件,我知道其中一个可以在仅使用mp.create(等...)时与播放器一起工作。

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. 编辑:所以我遵循了Alexis Cartier给出的链接示例,现在没有错误。 However, the FileinputStream never finishes. 但是,FileinputStream永远不会完成。 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 请参阅此问题的答案来修改您的代码: MediaPlayer.setDataSource导致IOException有效文件

But you can't do mp.setDataSource("R.raw.sound1"); 但是您不能执行mp.setDataSource("R.raw.sound1");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM