Problem with audio stream on android

Asked

Viewed 387 times

4

My shorthand code is this:

MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

mediaPlayer.setDataSource("http://stm43.srvstm.com:8474/;");

mediaPlayer.prepareAsync();

mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            public void onPrepared(MediaPlayer mp) {
                mp.start();
            }
        });

mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            public boolean onError(MediaPlayer mp, int what, int extra) {
                mp.reset();
                Log.i("informacao", "erro "+ what + " -- " + extra);
                return false;
            }
        });

The logcat is returning me the following message:

? I/information: error 1 -- -2147483648

According to the Mediaplayer documentation the numbering error 1 is MEDIA_ERROR_UNKNOWN which is described as "Unspecified media player error." , the test that presented this error was done on an emulator with android 8.1 (Oreo), in versions 4.4.2 and 6.0 works normally, I would like to understand why it does not work in version 8.1 and what are the alternatives to make this stream work in the latest versions of android.

If the link is accessed this way http://stm43.srvstm.com:8474/Listen.pls in a browser, a file is downloaded and when opening the file . pls the stream works on the computer normally.

Information about the stream: (HE-AAC@ 64 kbps, 44.1 kHz)

  • I tested on android 8.1 and it works. The only thing that changed the code is to pass mediaPlayer.prepareAsync(); for after setOnPreparedListener().

  • What do you mean? I just tested again and the error message is the same, you used other resources? or only an Activity with this code?

  • 1

    Only one Activity with this code

  • Ok, I still do not understand, but I will do new tests in other emulators and other versions, later update here the results.

  • @ramaral I created a new project from scratch as you did and tested, worked normally. Then I found it strange since I was trying and looking for a solution for some time, after a few days trying to understand, I realized that the error was not in this code but in another part of the project, How can I proceed in this scenario? I edit the question stating the problem and how I solved, create an answer or answer here right here in the comments ?

  • If the reason for the problem is related to Mediaplayer and is something that can happen to others, you must respond. However, there is no harm in putting an answer, even if the circumstances are different.

Show 1 more comment

2 answers

3


The problem was generated due to a lack of attention, I was thinking that the problem was in this code and I forgot the context that it was inserted. I realized that in my project was implemented the network security configuration and that in my scenario ended up "hindering" the Mediaplayer to perform its function, to be able to realize this after testing the coding in a project without any configuration, started from scratch.

On my Androidmanifest.xml I had the following:

...
 <application
        android:networkSecurityConfig="@xml/network_security_config"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
...

and in my network_security_config.xml

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="false">
        <domain includeSubdomains="true">stm43.srvstm.com</domain>
    </domain-config>
</network-security-config>

The documentation explains that this configuration I used serves to ensure that all connections with Secure.example.com (in my case stm43.srvstm.com) are always performed by HTTPS to protect confidential traffic from hostile networks. Trying to understand a little more, within the explanation of the method:

NetworkSecurityPolicy.isCleartextTrafficPermitted()

I found the following, "When unencrypted text network traffic is not allowed, the platform components (e.g., HTTP stacks and FTP Downloadmanager, Mediaplayer) refuse requests for this process to use unencrypted text traffic. Third-party libraries are strongly encouraged to honor this setting as well." So some players I tested didn’t work either.

And the reason the app is only working with versions inferior to android 6.0 (API 23), is that these "trust the repository of Cas added by the user by default", already in the higher versions android was taking into account my network security configuration (network_security_config.xml).

Sources:

https://developer.android.com/training/articles/security-config?hl=pt-br

https://developer.android.com/reference/android/security/NetworkSecurityPolicy.html?hl=pt-br#isCleartextTrafficPermitted()

-1

Whoa, that’s all right!?

Ever heard of Exoplayer? if not, take a look. A fantastic and very simple tool to implement.

Playing videos and music is a popular activity on Android devices. The Android framework provides Mediaplayer, a quick solution to play media with minimal code. It also provides low-level media Apis such as Mediacodec, Mediadrm Audiotracke, which can be used to create custom media player solutions.

Exoplayer is an open-level, application-level media player built on Android’s low-level media Apis. This guide describes the Exoplayer library and its use. Refers to the code in Exoplayer’s main demo application to provide concrete examples. The guide covers the pros and cons of using Exoplayer. It shows how to use Exoplayer to play adaptive DASH, Smoothstreaming and HLS streams, as well as formats such as MP4, M4A, FMP4, Webm, MKV, MP3, Ogg, WAV, MPEG-TS, MPEG-PS, FLV and ADTS ( AAC). It also discusses Exoplayer events, messages, customization and DRM support.

Learn more in: https://google.github.io/ExoPlayer/

If you have any questions about the implementation we can help you, but it’s very simple and easy. It’s all detailed in the documentation. Check out some examples: https://github.com/google/ExoPlayer/tree/release-v2/demos

  • The content may be useful, but it doesn’t answer the question.

  • often it may be more feasible to implement such a tool, than to hammer on the problems. I see no problem in it, because it supports what it needs, supports API 19 up, and is easy to implement, it costs nothing to do a test and see if it fits and solves its problem.

  • The question I raise is that the answer does not answer the question. Exoplayer is only an alternative. Why not suggest you stop using Android and start using Ios? :)

  • Even because the code presented works on Android 8.1.

  • @Leonardofigueiredo Thanks for the attempt, but as already said does not answer the question, even because I had already tested with some other players and was having the same problem, never played, I was thinking it was some compatibility problem with the stream and then tried several other players and always unsuccessful. I already found the problem, soon I will put here the reason.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.