试图将mp3从服务器流式传输到我的应用程序

时间:2014-04-15 21:44:39

标签: url stream mp3

我尝试用我的应用程序从网上播放一些音乐,但是当我启动应用程序时,应用程序崩溃了。 Eclipse没有发现任何错误。有人可以查看代码,也许可以找到问题吗?先感谢您。

package daanklijn.music;

import java.io.IOException;

import pcsalt.example.customlistviewdemo2.R;
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.app.ActionBar;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.os.Build;

public class AfrojackActivity extends Activity {

static MediaPlayer mPlayer;
Button buttonPlay;
Button buttonStop;
String url = "http://android.programmerguru.com/wp-content/uploads/2013/04/hosannatelugu.mp3"; 

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_afrojack);

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

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

protected void onDestroy() {
    super.onDestroy();
    // TODO Auto-generated method stub
    if (mPlayer != null) {
        mPlayer.release();
        mPlayer = null;
    }
}

}

1 个答案:

答案 0 :(得分:0)

添加一些'}'在错误的地方,改变它之后就有效了。