42

I followed this to Play Stream Radio In android

Here Its working Fine But Player Loading Bit Slow after clicking I need to wait for 30+ seconds time

But I am getting This error In console

MediaPlayer: setDataSource IOException happend : 
java.io.FileNotFoundException: No content provider: http://www.example.com:8000/live.ogg
at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1074)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:927)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:854)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1087)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1061)
at org.oucho.radio.Player.playLaunch(Player.java:237)
at org.oucho.radio.Playlist.onPostExecute(Playlist.java:98)
at org.oucho.radio.Playlist.onPostExecute(Playlist.java:35)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5951)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)

In the Link You can see all files like player etc

Due to this error My stream Is slow. Please any one help me on this type

Here this error is not with .ogg file I tried with .mp3 and Just /live

http://www.example.com:8000/beet.ogg
http://www.example.com:8000/mouthorgan.mp3
http://www.example.com:8000/live

The Audio is playing but after this error It is taking some 30 seconds time Some times it is taking too long ....When I plays Its showing this error and then its connecting to server.. and playing

please help me to fix this

Jonas
  • 121,568
  • 97
  • 310
  • 388
Whats Going On
  • 1,379
  • 6
  • 20
  • 49
  • Just change HTTP to HTTPS. More details here: https://stackoverflow.com/questions/45940861/android-8-cleartext-http-traffic-not-permitted – Egor tepikin May 17 '21 at 09:18

4 Answers4

52

Go to this file https://github.com/Old-Geek/Radio/blob/master/app/src/main/java/org/oucho/radio/Player.java#L234 and change

player.setDataSource(context, Uri.parse(url));

to

player.setDataSource(url)

The problem is that void setDataSource (String path) Sets the data source (file-path or http/rtsp URL) to use.

path String: the path of the file, or the http/rtsp URL of the stream you want to play

the github code uses void setDataSource (Context context, Uri uri) which assumes uri to be of some form of ContentProvider

context Context: the Context to use when resolving the Uri
uri Uri: the Content URI of the data you want to play

Anurag Singh
  • 6,140
  • 2
  • 31
  • 47
  • Thanks @Anurag Singh .. Now error Gone But I am getting This warning's `02-13 15:24:20.233 16769-16787/org.sss.text.liveradio D/MediaHTTPConnection: filterOutInternalHeaders: key=User-Agent, val= stagefright/1.2 (Linux;Android 5.1.1) 02-13 15:24:20.243 16769-16786/org.sss.text.liveradio D/MediaHTTPConnection: proxy null port 0 02-13 15:24:38.203 16769-16769/org.sss.text.liveradio E/MediaPlayer: Should have subtitle controller already set 02-13 15:24:54.743 16769-16769/org.sss.text.liveradio E/MediaPlayer-JNI: QCMediaPlayer mediaplayer NOT present ` and Its playing Same **30sec dilay** – Whats Going On Feb 13 '17 at 09:58
  • Thanks Your answer worked.... But still its littlebit delay I will check and confirm bounty,,,, – Whats Going On Feb 18 '17 at 05:23
  • I need to justice both of you So Almost both u guys helped me So I have distributed them bounty and answer thanks for spending your effort and time on my question – Whats Going On Feb 18 '17 at 05:27
23

java.io.FileNotFoundException: No content provider: http://www.example.com:8000/live.ogg

Because its trying load as a file from device contentprovider based on the context and you are passing as a URL.

Seems your issue is in setting datasource to mediaplayer. As you are trying to play url or streaming which required to setDataSource method without context.

I was facing same issue while playing live streaming. and below code resolved my issue.

PlayerScreen

        public class PlayerScreen extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.player);
            startService(new Intent(this, MediaPlayerService.class));
            findViewById(R.id.btnChangeTrack).setOnClickListener(clickListener);
            findViewById(R.id.btnStartMediaPlayer).setOnClickListener(clickListener);
            findViewById(R.id.btnStopMediaPlayer).setOnClickListener(clickListener);
            ToggleButton toggleButton = (ToggleButton) findViewById(R.id.togglePauseResume);
            toggleButton.setOnCheckedChangeListener(checkedChangeListener);
            /*
            * To get url which is passing from the previous activity listitem click.
            * If url which is pass from listitem click is not empty it will start player
            * */
            String url = getIntent().getStringExtra("url");
            if (!TextUtils.isEmpty(url))
                startMediaPlayer(url);

        }

        private ToggleButton.OnCheckedChangeListener checkedChangeListener = new ToggleButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (!isChecked) {
                    Intent intent = new Intent();
                    intent.setAction(MediaPlayerService.BROADCAST_TO_SERVICE);
                    intent.putExtra(MediaPlayerService.PLAYER_FUNCTION_TYPE, MediaPlayerService.PAUSE_MEDIA_PLAYER);
                    sendBroadcast(intent);
                } else {
                    Intent intent = new Intent();
                    intent.setAction(MediaPlayerService.BROADCAST_TO_SERVICE);
                    intent.putExtra(MediaPlayerService.PLAYER_FUNCTION_TYPE, MediaPlayerService.RESUME_MEDIA_PLAYER);
                    sendBroadcast(intent);
                }
            }
        };
        private View.OnClickListener clickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent;
                switch (v.getId()) {
                    case R.id.btnChangeTrack:
                        intent = new Intent();
                        intent.setAction(MediaPlayerService.BROADCAST_TO_SERVICE);
                        intent.putExtra(MediaPlayerService.PLAYER_FUNCTION_TYPE, MediaPlayerService.CHANGE_PLAYER_TRACK);
                        intent.putExtra(MediaPlayerService.PLAYER_TRACK_URL, "http://www.example.com:8000/live");
                        sendBroadcast(intent);
                        break;
                    case R.id.btnStartMediaPlayer:
                        startMediaPlayer("http://www.example.com:8000/beet.ogg");
//startMediaPlayer("http://108.163.197.114:8071/listen.pls");
                        break;
                    case R.id.btnStopMediaPlayer:
                        intent = new Intent();
                        intent.setAction(MediaPlayerService.BROADCAST_TO_SERVICE);
                        intent.putExtra(MediaPlayerService.PLAYER_FUNCTION_TYPE, MediaPlayerService.STOP_MEDIA_PLAYER);
                        sendBroadcast(intent);
                        break;

                }
            }
        };

        @Override
        protected void onResume() {
            super.onResume();
            registerReceiver(receiverFromservice, new IntentFilter(MediaPlayerService.SERVICE_TO_ACTIVITY));
        }

        private String currentPlayerStatus = "N/A";
        private BroadcastReceiver receiverFromservice = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                if (MediaPlayerService.SERVICE_TO_ACTIVITY.equalsIgnoreCase(action)) {
                    /*
                    * To get current status of player
                    * */
                    currentPlayerStatus = intent.getStringExtra(MediaPlayerService.PLAYER_STATUS_KEY);
                    Log.e("Player Mode", "" + currentPlayerStatus);
                }
            }
        };

        @Override
        protected void onPause() {
            super.onPause();
            unregisterReceiver(receiverFromservice);
        }

        /**
         * TO start media player.It will send broadcast to Service & from service player will start
         *
         * @param url
         */
        public void startMediaPlayer(String url) {
            Intent intent = new Intent();
            intent.setAction(MediaPlayerService.BROADCAST_TO_SERVICE);
            intent.putExtra(MediaPlayerService.PLAYER_FUNCTION_TYPE, MediaPlayerService.PLAY_MEDIA_PLAYER);
            intent.putExtra(MediaPlayerService.PLAYER_TRACK_URL, url);
            sendBroadcast(intent);
        }
    }

MediaPlayerService

public class MediaPlayerService extends Service {
    public static final String BROADCAST_TO_SERVICE = "com.mediaplayer.playerfunction";
    public static final String SERVICE_TO_ACTIVITY = "com.mediaplayer.currentPlayerStatus";
    public static final String PLAYER_FUNCTION_TYPE = "playerfunction";
    public static final String PLAYER_TRACK_URL = "trackURL";
    public static final int PLAY_MEDIA_PLAYER = 1;
    public static final int PAUSE_MEDIA_PLAYER = 2;
    public static final int RESUME_MEDIA_PLAYER = 3;
    public static final int STOP_MEDIA_PLAYER = 4;
    public static final int CHANGE_PLAYER_TRACK = 5;
    public static final String PLAYER_STATUS_KEY = "PlayerCurrentStatus";

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        IntentFilter intentFilter = new IntentFilter(BROADCAST_TO_SERVICE);
        registerReceiver(playerReceiver, intentFilter);
        if (mPlayer != null && mPlayer.isPlaying()) {
            sendPlayerStatus("playing");
        }
        return START_STICKY;
    }

    private BroadcastReceiver playerReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (BROADCAST_TO_SERVICE.equalsIgnoreCase(action)) {
                String trackURL = intent.hasExtra(PLAYER_TRACK_URL) ? intent.getStringExtra(PLAYER_TRACK_URL) : "";
                int function = intent.getIntExtra(PLAYER_FUNCTION_TYPE, 0);
                switch (function) {
                    case CHANGE_PLAYER_TRACK:
                        changeTrack(trackURL);
                        break;
                    case STOP_MEDIA_PLAYER:
                        stopPlayer();
                        break;
                    case PLAY_MEDIA_PLAYER:
                        startMediaPlayer(trackURL);
                        break;
                    case PAUSE_MEDIA_PLAYER:
                        pausePlayer();
                        break;
                    case RESUME_MEDIA_PLAYER:
                        resumePlayer();
                        break;
                }

            }
        }
    };
    private MediaPlayer mPlayer;

    private void pausePlayer() {
        if (mPlayer != null && mPlayer.isPlaying()) {
            mPlayer.pause();
            sendPlayerStatus("pause");
        }
    }

    private void resumePlayer() {
        if (mPlayer != null && !mPlayer.isPlaying()) {
            mPlayer.start();
            sendPlayerStatus("playing");
        }
    }

    private void changeTrack(String url) {
        stopPlayer();
        startMediaPlayer(url);

    }

    private void stopPlayer() {
        if (mPlayer != null) {
            mPlayer.stop();
            mPlayer.release();
            mPlayer = null;
            sendPlayerStatus("stopped");

        }
    }

    public void startMediaPlayer(String url) {
        if (TextUtils.isEmpty(url))
            return;
        if (mPlayer == null)
            mPlayer = new MediaPlayer();
        try {
            mPlayer.setDataSource(url);
            mPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {

                @Override
                public boolean onError(MediaPlayer mp, int what, int extra) {
                    if (extra == MediaPlayer.MEDIA_ERROR_SERVER_DIED
                            || extra == MediaPlayer.MEDIA_ERROR_MALFORMED) {
                        sendPlayerStatus("erroronplaying");
                    } else if (extra == MediaPlayer.MEDIA_ERROR_IO) {
                        sendPlayerStatus("erroronplaying");
                        return false;
                    }
                    return false;
                }
            });
            mPlayer.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {

                public void onBufferingUpdate(MediaPlayer mp, int percent) {
                    Log.e("onBufferingUpdate", "" + percent);

                }
            });
            mPlayer.prepareAsync();
            mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

                public void onPrepared(MediaPlayer mp) {
                    mPlayer.start();
                    sendPlayerStatus("playing");
                }
            });
            mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

                @Override
                public void onCompletion(MediaPlayer mp) {
                    Log.e("onCompletion", "Yes");
                    sendPlayerStatus("completed");
                }
            });
            mPlayer.setOnInfoListener(new MediaPlayer.OnInfoListener() {
                @Override
                public boolean onInfo(MediaPlayer mp, int what, int extra) {
                    return false;
                }
            });
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void sendPlayerStatus(String status) {
        Intent intent = new Intent();
        intent.setAction(SERVICE_TO_ACTIVITY);
        intent.putExtra(PLAYER_STATUS_KEY, status);
        sendBroadcast(intent);
    }
}

player.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/section_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/ab_tool"
        android:text="Home" />

    <Button
        android:id="@+id/btnStartMediaPlayer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/section_label"
        android:text="Start Player" />


    <ToggleButton
        android:id="@+id/togglePauseResume"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btnStartMediaPlayer"
        android:checked="true"
        android:textOff="Resume"
        android:textOn="Pause" />

    <Button
        android:id="@+id/btnChangeTrack"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/togglePauseResume"
        android:text="Chanage Track" />

    <Button
        android:id="@+id/btnStopMediaPlayer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btnChangeTrack"
        android:text="STOP" />
</RelativeLayout>

Manifest

<activity android:name=".PlayerScreen">

    <intent-filter>
        <action android:name="android.intent.action.MAIN" />


        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<service android:name=".MediaPlayerService"></service>

To get data from streaming url header data you can [check this answer][2]

For testing purpose here i have used two urls

UPDATE PlayerActivity

public class PlayerScreen extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.player);
        startService(new Intent(this, MediaPlayerService.class));
        /*
        * To get url which is passing from the previous activity listitem click.
        * If url which is pass from listitem click is not empty it will start player
        * */
        String url = getIntent().getStringExtra("url");
        if (!TextUtils.isEmpty(url))
            startMediaPlayer(url);

    }

    @Override
    protected void onResume() {
        super.onResume();
        registerReceiver(receiverFromservice, new IntentFilter(MediaPlayerService.SERVICE_TO_ACTIVITY));
    }

    private String currentPlayerStatus = "N/A";
    private BroadcastReceiver receiverFromservice = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (MediaPlayerService.SERVICE_TO_ACTIVITY.equalsIgnoreCase(action)) {
                /*
                * To get current status of player
                * */
                currentPlayerStatus = intent.getStringExtra(MediaPlayerService.PLAYER_STATUS_KEY);
                Log.e("Player Mode", "" + currentPlayerStatus);
            }
        }
    };

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(receiverFromservice);
    }

    /**
     * TO start media player.It will send broadcast to Service & from service player will start
     *
     * @param url
     */
    public void startMediaPlayer(String url) {
        Intent intent = new Intent();
        intent.setAction(MediaPlayerService.BROADCAST_TO_SERVICE);
        intent.putExtra(MediaPlayerService.PLAYER_FUNCTION_TYPE, MediaPlayerService.PLAY_MEDIA_PLAYER);
        intent.putExtra(MediaPlayerService.PLAYER_TRACK_URL, url);
        sendBroadcast(intent);
    }
}

let me know if anything.

user1140237
  • 5,015
  • 1
  • 28
  • 56
  • thanks @user1140237 sir... I think this is fine But I need to check my radio Service Now Its down.. It will take 2 days to renew.... till that if possible Can you suggest me how to use [this in TabLayout](http://stackoverflow.com/questions/42153697/android-changing-a-normal-layout-to-tab-layout-error-in-image-buttons-and-other) – Whats Going On Feb 13 '17 at 09:21
  • @MLN you can test above code with any streaming url like from shoutcast [streaming url](http://91.121.164.210:8380/stream). URL which has mp3 or ogg codec. – user1140237 Feb 14 '17 at 06:09
  • thanks Is there any code sample at git-hub or something like that of your code... Here I tried missing some files ... Actually my code is making 30 sec delay so is there any code – Whats Going On Feb 14 '17 at 06:27
  • @MLN nope theres not sample on git sorry... But what is missing let me know – user1140237 Feb 14 '17 at 06:28
  • I am getting Error at `isPlaying` cannot resolve.. and some other in `MediaPlayerService` so I am asking for any github or example etc.. for testing – Whats Going On Feb 14 '17 at 08:03
  • @MLN reason for 30 seconds delay is might be not streaming url or its taking time to connection or might be buffering time – user1140237 Feb 15 '17 at 05:45
  • I need to justice both of you So Almost both u guys helped me So I have distributed them bounty and answer thanks for spending your effort and time on my question – Whats Going On Feb 18 '17 at 05:28
  • Hope that you can solve my problem. This is similar to what I'm facing right now. Can you please look into this [post](https://stackoverflow.com/q/44447813/7004388) and guide me. I'm stuck there. Need help badly. – coderpc Jun 09 '17 at 16:52
  • @user1140237 mine problem is similar one but my audio stream API has basic authentication hence I am using the public void setDataSource (Context context, Uri uri, Map headers) I am getting the same exception as mentioned in the question, Playing works well little delayed but works, Can you tell me what may be the possible reason? – Prashant Sep 24 '18 at 05:37
7

On Android 9+ the clear net http trafic may cause this problem. Check this one: Android 8: Cleartext HTTP traffic not permitted

Yazon2006
  • 3,684
  • 3
  • 25
  • 36
2

This error also happens if this internet permission is not in the Manifest.xml

<uses-permission android:name="android.permission.INTERNET" />
s-hunter
  • 24,172
  • 16
  • 88
  • 130