0

I'm in the process of building a prototype for a project. I want to be able to stream music from my website to the app.

Here is what I have so far. Trying it with an m3u because I honestly have no idea what else to use for this.

Button buttonPlay;
Button buttonStop;
MediaPlayer mPlayer;
String url = "http://www.*******.com/*****/files.m3u";

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

    buttonPlay = (Button) findViewById(R.id.play);
    buttonPlay.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {                 
            mPlayer = new MediaPlayer();
            mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            try {
                mPlayer.setDataSource(url);
            } catch (IllegalArgumentException e) {
                Toast.makeText(getApplicationContext(), "You might not set the URI correctly! Illegal Argument", Toast.LENGTH_LONG).show();
            } catch (SecurityException e) {
                Toast.makeText(getApplicationContext(), "You might not set the URI correctly! Security ", Toast.LENGTH_LONG).show();
            } catch (IllegalStateException e) {
                Toast.makeText(getApplicationContext(), "You might not set the URI correctly! Illegal Statement", Toast.LENGTH_LONG).show();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                mPlayer.prepare();
            } catch (IllegalStateException e) {
                Toast.makeText(getApplicationContext(), "You might not set the URI correctly! Prepare Illegal State", Toast.LENGTH_LONG).show();
            } catch (IOException e) {
                Toast.makeText(getApplicationContext(), "You might not set the URI correctly! Prepare IO", Toast.LENGTH_LONG).show();
            }
            mPlayer.start();
        }
    });
    buttonStop = (Button) findViewById(R.id.stop);
    buttonStop.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(mPlayer!=null && mPlayer.isPlaying()){
                mPlayer.stop();
            }
        }
    });

Them problem is it throws IOexception after prepare. Now I read somewhere you have to download m3u files and then read them line by line. If that is the case, is there a better way? I'm attempting to make sort of a radio app for testing purposes that will play songs at random, not in order, so I don't want to hard code them MP3 files,

jham
  • 53
  • 1
  • 6

1 Answers1

0

you cant just prepare a stream because it needs to download asynchronously. you will need to do this

Button buttonPlay;
Button buttonStop;
MediaPlayer mPlayer;
String url = "http://www.*******.com/*****/files.m3u";

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

buttonPlay = (Button) findViewById(R.id.play);
buttonPlay.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {                 
        mPlayer = new MediaPlayer();
        mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        try {
            mPlayer.setDataSource(url);
        } catch (IllegalArgumentException e) {
            Toast.makeText(getApplicationContext(), "You might not set the URI correctly! Illegal Argument", Toast.LENGTH_LONG).show();
        } catch (SecurityException e) {
            Toast.makeText(getApplicationContext(), "You might not set the URI correctly! Security ", Toast.LENGTH_LONG).show();
        } catch (IllegalStateException e) {
            Toast.makeText(getApplicationContext(), "You might not set the URI correctly! Illegal Statement", Toast.LENGTH_LONG).show();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
                mPlayer.prepareAsync();
              mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
             @Override
             public void onPrepared(MediaPlayer mp) {
                mPlayer.start();
            }
        };);
        } catch (IllegalStateException e) {
            Toast.makeText(getApplicationContext(), "You might not set the URI correctly! Prepare Illegal State", Toast.LENGTH_LONG).show();
        } catch (IOException e) {
            Toast.makeText(getApplicationContext(), "You might not set the URI correctly! Prepare IO", Toast.LENGTH_LONG).show();
        }
    }
});
buttonStop = (Button) findViewById(R.id.stop);
buttonStop.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(mPlayer!=null && mPlayer.isPlaying()){
            mPlayer.stop();
        }
    }
});
Tomer Shemesh
  • 10,278
  • 4
  • 21
  • 44
  • I'm not too sure how to implement this. I have never messed with the mediaPlayer before. – jham Apr 21 '15 at 17:55
  • I still can't get it to work. It's not playing the test sound I placed in the m3u file. – jham Apr 21 '15 at 18:17