【问题标题】:Streaming audio from website to app将音频从网站流式传输到应用程序
【发布时间】:2015-06-29 00:41:41
【问题描述】:

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

这是我目前所拥有的。用 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 文件,

【问题讨论】:

    标签: android streaming m3u


    【解决方案1】:

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

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

    【讨论】:

    • 我不太确定如何实现这一点。我以前从来没有弄乱过媒体播放器。
    • 我仍然无法让它工作。它没有播放我放在 m3u 文件中的测试声音。
    猜你喜欢
    • 2014-03-21
    • 2012-07-20
    • 2016-02-08
    • 2015-11-22
    • 1970-01-01
    • 2014-01-28
    • 1970-01-01
    • 2012-07-29
    • 2013-12-20
    相关资源
    最近更新 更多