'MediaPlayer on Android Wear OS. Why do I get a IOException after prepare. Prepare failed status=0x1

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



Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jason von Jolkos