Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
393 views
in Technique[技术] by (71.8m points)

java - Android MediaPlayer not preparing. Error (1, -4)

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 Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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");


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...