简体   繁体   中英

stream a live audio stream for android

I am new to android programming and therefore this might seem like an easy question for many but its been 2 days and i have searched almost everywhere on the internet but cant find a solution to my problem

i am trying to stream a link(which works when i post on chrome) using MediaPlayer class. Although i get audio on chrome, i never get anything when i run the app on my Samsung galaxy s4.i have already used internet permission for the app. here is the code i am using:

public class LiveKirtan extends Activity {

    MediaPlayer mp;
    String url;



        @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_live_kirtan);

        url = "http://radio2.sikhnet.com:8020/live"; 
        Uri myUri = Uri.parse(url);

          mp = new MediaPlayer();

           try {
            mp.setDataSource(this, myUri);
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

         } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

           mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
           mp.prepareAsync();
           mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

                @Override
                public void onPrepared(MediaPlayer player) {
                    player.start();

                }


            });
    }


}

Make sure to add the following permission to your manifest:

<uses-permission android:name="android.permission.INTERNET"/>

Also, Android does not support just any kind of streaming audio (especially pre kitkat), so make sure to check the compatibility of your stream here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM