Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MediaPlayer.create returns null #3855

Open
PuntoDiGoccia opened this issue Apr 10, 2018 · 5 comments
Open

MediaPlayer.create returns null #3855

PuntoDiGoccia opened this issue Apr 10, 2018 · 5 comments

Comments

@PuntoDiGoccia
Copy link

PuntoDiGoccia commented Apr 10, 2018

Description

MediaPlayer.create always returns null.
I dug into Robolectric framework and looks like this ShadowMediaPlayer's method fails:

  @Implementation
  public void prepare() {
    checkStateException("prepare()", preparableStates);
    MediaInfo info = getMediaInfo();
    if (info.preparationDelay > 0) {
      SystemClock.sleep(info.preparationDelay);
    }
    invokePreparedListener();
  }

getMediaInfo() returns null, which throws a NullPointerException, which in turn returns null to MediaPlayer.create

Steps to Reproduce

        if (mediaPlayer == null) {
            mediaPlayer = new MediaPlayer();
        }

        if (!mediaPlayer.isPlaying()) {
            mediaPlayer = MediaPlayer.create(this, R.raw.push_sound);
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mediaPlayer.setLooping(true);
            mediaPlayer.start();
        }

Robolectric & Android Version

Robolectric 3.8
Gradle 4.1
Android Plugin 3.0.1
AndroidStudio 3.1.1

@mrkostua
Copy link

Also had problems using ShadowMediaPlayer, tried to create simple player with create() method and test it but it field with same NullPointer exception. Have you made any progress on it?

@PuntoDiGoccia
Copy link
Author

I inherited my own ShadowMediaPlayer to implement the create() method so that it adds the missing MediaInfo object:

`@Implements(MediaPlayer.class)
public class MediaPlayerShadow extends ShadowMediaPlayer{

@Implementation
public void __constructor__() {
    super.__constructor__();
}

@Implementation
public static MediaPlayer create(Context context, int resid){
    DataSource ds = DataSource.toDataSource(String.valueOf(resid));
    addMediaInfo(ds, new ShadowMediaPlayer.MediaInfo());

    MediaPlayer mp = new MediaPlayer();
    ShadowMediaPlayer shadow = Shadow.extract(mp);
    try {
        shadow.setDataSource(ds);
        shadow.setState(INITIALIZED);
        mp.prepare();
    } catch (Exception e) {
        return null;
    }

    return mp;
}

}`

@Kisty
Copy link

Kisty commented Aug 30, 2018

Hmm, I've encountered this bug. Would be great to add this to the library ShadowMediaPlayer.

@Kisty
Copy link

Kisty commented Aug 29, 2019

@PuntoDiGoccia can you reopen, please? This should be in the Robolectric framework really, shouldn't it, @xian?

@PuntoDiGoccia
Copy link
Author

@Kisty sure can do if it can improve the framework

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants