Android 从网站到应用程序的流式音频

Android 从网站到应用程序的流式音频,android,streaming,m3u,Android,Streaming,M3u,我正在为一个项目建立一个原型。我希望能够将音乐从我的网站流到应用程序 这是我到目前为止所拥有的。用m3u试试吧,因为我真的不知道还能用什么 Button buttonPlay; Button buttonStop; MediaPlayer mPlayer; String url = "http://www.*******.com/*****/files.m3u"; @Override protected void onCreate(Bundle savedInstanceState) {

我正在为一个项目建立一个原型。我希望能够将音乐从我的网站流到应用程序

这是我到目前为止所拥有的。用m3u试试吧,因为我真的不知道还能用什么

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();
            }
        }
    });

他们的问题是在准备之后抛出IOexception。现在我在某个地方读到,你必须下载m3u文件,然后逐行阅读。如果是这样,有没有更好的办法?我正在尝试制作一种用于测试目的的广播应用程序,它将随机播放歌曲,而不是按顺序播放,所以我不想硬编码它们的MP3文件,

你不能只准备一个流,因为它需要异步下载。你需要这样做

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();
        }
    }
});

我不太确定如何实现这一点。我以前从来没有弄坏过mediaPlayer。我仍然无法让它工作。它没有播放我放在m3u文件中的测试声音。