MediaPlayer: Couldn't open file on client side; Error(-38,0) and more

巧了我就是萌 提交于 2020-01-12 14:27:07

问题


I'm learning Android and I've created an activity that has two buttons:A ToggleButton(Play/Pause) and a Button(Next). I have two songs that would like to be cycled upon pressing Next. I have an array

int [] songs={R.raw.song1,R.raw.song2};

I overrode the onClick in my Activity. The first song plays fine; but after pressing Next, I get the following errors:

Couldn't open file on client side, trying server side

E/MediaPlayer(3107): start called in state 4

E/MediaPlayer(3107): error (-38, 0)

E/MediaPlayer(3107): Error (-38,0)

E/MediaPlayer(3107): error (1, -2147483648)

E/MediaPlayer(3107): Error (1,-2147483648)

In onCreate(Bundle...),

if(mp!=null) mp.release();
  mp=MediaPlayer.create(this, songs[count]);

Here's my onClick(View v) method:

public void onClick(View view) {    
    Log.v(TAG,"ID:"+view.getId());
    switch (view.getId()) {
    case R.id.playerbutton:  //ToggleButton
        if(state==0) {
            mp.start();
            state=1;
        }
        else if(state==1) {
            state=0;
            mp.pause();
        }   
    break;

    case R.id.next:  //Next button
        Log.v(TAG,"Next button pressed!");
        count=(count+1)%2;  //Have only two songs
        mp.reset();
        try {
            mp.setDataSource(this, Uri.parse("android.resource://com.example.myfirstapp"+songs[count]));
            mp.setOnPreparedListener(this);
            mp.prepareAsync();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        mp.start();
    break;
    }
}

Basically, I'm doing this for every press of the Next button: -reset mp (i.e. the MediaPlayer object) -set a new data source for playing the next song -start mp

As for using setOnPreparedListener or prepareAsync, I read this SO question.

Where am I going wrong?


回答1:


I believe you are setting the datasource wrong.

Change it to:

mp.setDataSource(this, Uri.parse("android.resource://com.example.myfirstapp/"+songs[count]));

Notice the trailing slash after the package name.



来源:https://stackoverflow.com/questions/17865836/mediaplayer-couldnt-open-file-on-client-side-error-38-0-and-more

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!