如何在android中流式传输音频文件

时间:2016-05-13 04:30:59

标签: android

我正在尝试在android中传输音频文件,但每次我都会遇到此异常

java.io.IOException:准备失败:status = 0x1

我的歌曲url存储在服务器上。例如(http://Touchstoneesol.com/listen/ML1 (Section 2).mp3);

但是相同的代码正确地传递了此链接(http://android.programmerguru.com/wp-content/uploads/2013/04/hosannatelugu.mp3);

现在我陷入困境,代码中是否存在问题,或者我的歌曲链接是否已损坏。

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!", Toast.LENGTH_LONG).show();
            } catch (SecurityException e) {
                Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
            } catch (IllegalStateException e) {
                Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", 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!", Toast.LENGTH_LONG).show();
            } catch (IOException e) {
                Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
            }
            mPlayer.start();
        }
    });

1 个答案:

答案 0 :(得分:0)

struct seg{ 
 int sum;
};
seg tree[4*N + 1];

这是包含按钮的xml。

    public class StreamAudioDemo extends Activity implements OnClickListener,
OnPreparedListener, OnErrorListener, OnCompletionListener {

    MediaPlayer mp;
    ProgressDialog pd;
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button bt = (Button)findViewById(R.id.play);
    bt.setOnClickListener(this);
}

   @Override
   public void onPrepared(MediaPlayer mp) {
       Log.i("StreamAudioDemo", "prepare finished");
       pd.setMessage("Playing.....");
       mp.start();
  }

  @Override
  public void onClick(View v) {
       try
        {
            pd = new ProgressDialog(this);
            pd.setMessage("Buffering.....");
            pd.show();
            mp = new MediaPlayer();
            mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mp.setOnPreparedListener(this);
            mp.setOnErrorListener(this);
            mp.setDataSource("http://www.robtowns.com/music/blind_willie.mp3");
            mp.prepareAsync();
            mp.setOnCompletionListener(this);
        }
        catch(Exception e)
        {
            Log.e("StreamAudioDemo", e.getMessage());
        }
    }

    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
      pd.dismiss();
      return false;
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        pd.dismiss();A
        Toast.makeText(getApplicationContext(), "Completed", Toast.LENGTH_LONG).show();     
    }
}