1

I've made a radio app that works without any problems on Android 5 and above. The problem is: Multiple users have reported me that the app crashes on OnePlus devices, which is weird because on the same Android version but on a different device, it works without any problems.

I've managed to find a friend that has an OnePlus however he's from another country, so I can't get the device to debug. However, I've sent him a debug apk and he sent me this Image

My guess is that the problem starts here:

private void initializeMediaPlayer() {
    player = new MediaPlayer();
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        player.setAudioAttributes(new AudioAttributes.Builder()
            .setUsage(AudioAttributes.USAGE_MEDIA)
            .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
            .build());
    } else {
        player.setAudioStreamType(AudioManager.STREAM_MUSIC);
    }
    try {
        player.setDataSource("http://91.121.139.194:8157/stream.mp3");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Does OnePlus handle MediaPlayer in a different way? What could be the problem? I've looked for that error online but everything I found was about encoding the source which I've already done.

UPDATE: After some more debugging, we also found out the exact path to the Shoutcast MP3 file. After I've set the Data Source to the exact path, there are no errors. No logcat errors and no debug console errors... Yet it doesn't work.

Marius
  • 21
  • 4
  • can you find if the encode path which is setted as source path is legit or not.use debugger or logger to get the encode path and try it.or check if encoded path is same in oneplus or aother devices. – Sahdeep Singh Mar 09 '19 at 19:10
  • @SahdeepSingh yes. I've debugged it by printing the encodedPath in a log to the Debug Console and it is the same. However, I have an update on the situation. I'll edit the post. – Marius Mar 09 '19 at 19:52
  • @SahdeepSingh see the update – Marius Mar 09 '19 at 19:57
  • Possible duplicate of [Access denied finding property "vendor.debug.egl.swapinterval" i am keep getting this error in android studio when i run my app](https://stackoverflow.com/questions/52855807/access-denied-finding-property-vendor-debug-egl-swapinterval-i-am-keep-getting) – Martin Zeitler Mar 09 '19 at 20:43
  • besides, [how to ask](https://stackoverflow.com/help/how-to-ask) clearly states not to post images of code & errors. I'll be able to identify duplicates nevertheless, even if more effort is required to do so (no matter if this was intentional or not). – Martin Zeitler Mar 09 '19 at 20:51
  • and concerning the error you cannot explain; try serving the file via `HTTPS` or add a network security policy, because `HTTP` violates the default policy (at least on later versions of Android). if it's not this, then it might be duplicate of this: https://stackoverflow.com/a/50805222/549372 – Martin Zeitler Mar 09 '19 at 20:55
  • @MartinZeitler that error is there because the screenshot was from a debug log based on the release apk, not on the debug apk. That error is no longer present in the debug console. As I stated in the update, now there aren't any errors neither in the logcat or debug console yet it doesn't play... – Marius Mar 09 '19 at 22:46
  • @MartinZeitler And yes... I know about the fact that it states not to post images of errors but as said in the original post, I don't have an OnePlus but I found a friend willingly to debug for me. I don't have the errors in plain text because he sent me only screenshots... – Marius Mar 09 '19 at 22:48
  • @MartinZeitler There is no way to send the stream through HTTPS because the radio host I have doesn't provide that. I will try to add a network security policy and see if that solves it. – Marius Mar 09 '19 at 23:03
  • @MartinZeitler I tryed your approach. Now I get mediaplayer error (1 -2147483648). Logcat says nothing else than the constructor of setDataSource seems to work, there's an httpUrlConnection object instantiated later that seems to work with null cookies, and nothing more than the error code that appears some seconds later – Marius Mar 10 '19 at 11:59

1 Answers1

0

it expects Context & Uri as arguments - and not the String representation of an URL. besides, you are using deprecated methods (which are nevertheless still stated in the examples) and never call .prepare(), therefore the MediaPlayer will be stuck in state initialized, but will not enter the prepared state, which is the precondition for the playback.

player.setDataSource(context, Uri.parse("http://91.121.139.194:8157/stream.mp3"));
player.prepare(); // this is a synchronous call (see the diagram).
player.start();

see MediaPlayer and MediaPlayer overview, which explains this all in detail.


PS: shoutcast? can also be used in combination with stunnel, in order to provide SSL encryption - so that the default network security policy would be met, without the requirement for an exclusion.

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • `initializeMediaPlayer()` is called `OnCreate()` then when I hit the Play button, it calls the `buttonplayaction()`.. I'm doing some IF checks there and if the requirement is met `(!player.isPlaying() && !noInternetBoolean)`, it calls the `startPlaying()`. Here if I get the audio focus, I'm doing the `player.prepareAsync()` and player.start OnPreparedListener. I need to use deprecated methods because I target Android 5 and above. However I've implemented both methods (for audio focus and one of them is in here, as I've seen you've spotted it). – Marius Mar 10 '19 at 17:29
  • I've tried to set the context before but I run into a problem. The file is an audio stream. If I set the context, MediaPlayer is trying to open the file on client side and it throws me a warning: Couldn't open file on client side. Trying server side. – Marius Mar 10 '19 at 17:32
  • @Marius there's nothing wrong with that. as it says "trying server-side". unless you provide a [MVCE](https://stackoverflow.com/help/mcve) you probably won't get any accurate answers, because you are withholding relevant information, eg. if this was synchronous or asynchronous. and even without the `Context`, it should still be an `Uri`... local files are ordinary referenced as `Uri.fromFile()`. this would at least permit sorting out network-work issues for a test. – Martin Zeitler Mar 10 '19 at 20:15
  • @Marius and for the backwards compatibility, you should use two independent implementations. eg. one alike this: https://stackoverflow.com/questions/11504809/how-to-shoutcast-streamming-with-mediaplayer-with-android-2-3-3 (as you have it) and one which uses current methods. mixing these two implementations might be the actual problem (because this makes no sense, unless checking which API level it runs against). – Martin Zeitler Mar 10 '19 at 20:18
  • `.setDataSource` was deprecated starting with android Nougat and it was replaced with `.setContentType`. That's why I'm checking if the android version is Nougat or newer to implement the proper atributes. The player works on all Android versions I've tested it on. Samsung S4 (5.1), Asus Zenfone 3 (8.0), Lenovo Vibe B (6.0), Huawei P9 (8.1 I think), Samsung S8 (So it works on 9.0 too). Samsung J4+ (8.1). It doesn't work only on OnePlus devices. If you want to, I can provide you a private link to the full code so you can better take a look. Code is too big to put it here. – Marius Mar 11 '19 at 23:14
  • @Marius where do you have this information from? the documentation does not say so - and when looking at what these methods do, this does not make the least sense. there were just a few methods added in API 23 & 26, and `.setAudioStreamType()` had been deprecated. don't even think that this is a OnePlus issue, which might be a religion, but running code against the wrong API level (which the question does not disclose), or passing wrongful arguments. the most easy might be to find a service which offers OnePlus devices for remote testing. – Martin Zeitler Mar 11 '19 at 23:29