1

I would like to create an app on Wear OS which plays back an online stream. The following code works fine under Android but not on Android Wear OS. Does anyone has an idea, why I get the Prepare failed status.

MediaPlayer mediaPlayer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    binding = ActivityMainBinding.inflate(getLayoutInflater());
    setContentView(binding.getRoot());

    mTextView = binding.text;

    String url = "....the url like http://streamserver.com/stream";
    mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioAttributes(new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_MEDIA)
            .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
            .build());

    try {
        mediaPlayer.setDataSource(url);
        mediaPlayer.prepare();               //here the Exception takes place after around 15 seconds of waiting.
    } catch (IOException e) {
        e.printStackTrace();
    }

    mediaPlayer.start();
}

If I change the code to mediaPlayer.prepareAsync(); with mediaPlayer.setOnPreparedListener(...) it also doesn't work. Logcat shows: E/MediaPlayerNative: error (1, -2147483648)

Any idea, why it doesn't work on Wear OS but works on android?

Thanks

Jason

  • 1
    I can't answer definitively, but have you enabled internet permissions, and a specific network security policy with cleartext traffic permitted. https://stackoverflow.com/questions/55479574/android-mediaplayer-java-io-ioexception-prepare-failed-status-0x1 – Yuri Schimke Feb 18 '22 at 19:57
  • Also, have you tried ExoPlayer or Media3 (the new ExoPlayer)? They are more full featured libraries, and have a lot of additional logging hooks that can help you get more information out. – Yuri Schimke Feb 18 '22 at 19:58

1 Answers1

0

OK I found the solution and the error. The error takes place if the url is a HTTP connection instead of a HTTPS. If the source is only accessable from a HTTP connection there are at least 2 solution.

1.) that one worked for me. enter the following line in the manifest under application

   android:usesCleartextTraffic="true"

2.) add a network security file. info found under https://developer.android.com/training/articles/security-config.html

that solves the problem