0

我的应用程序包含从 url 加载 mp3 格式数据的无线电流功能。我根据以下代码播放直播电台:

  public class StreamingMp3Player extends Activity implements OnClickListener, OnTouchListener, OnCompletionListener, OnBufferingUpdateListener{

     private ImageButton buttonPlayPause;
        private SeekBar seekBarProgress;
        public EditText editTextSongURL;
        private MediaPlayer mediaPlayer;
        // this value contains the song duration in milliseconds. Look at getDuration() method in MediaPlayer class
        private int mediaFileLengthInMilliseconds; 

        private final Handler handler = new Handler();

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            initView();
        }

        /** This method initialise all the views in project*/
        private void initView() {
            buttonPlayPause = (ImageButton)findViewById(R.id.ButtonTestPlayPause);
            buttonPlayPause.setOnClickListener(this);

            seekBarProgress = (SeekBar)findViewById(R.id.SeekBarTestPlay);  
            seekBarProgress.setMax(99); // It means 100% .0-99
            seekBarProgress.setOnTouchListener(this);
            editTextSongURL = (EditText)findViewById(R.id.EditTextSongURL);
            editTextSongURL.setText(R.string.testsong_20_sec);

            mediaPlayer = new MediaPlayer();
            mediaPlayer.setOnBufferingUpdateListener(this);
            mediaPlayer.setOnCompletionListener(this);
        }

        /** Method which updates the SeekBar primary progress by current song playing position*/
        private void primarySeekBarProgressUpdater() {
            seekBarProgress.setProgress((int)(((float)mediaPlayer.getCurrentPosition()/mediaFileLengthInMilliseconds)*100)); // This math construction give a percentage of "was playing"/"song length"
            if (mediaPlayer.isPlaying()) {
                Runnable notification = new Runnable() {
                    public void run() {
                        primarySeekBarProgressUpdater();
                    }
                };
                handler.postDelayed(notification,1000);
            }
        }

        @Override
        public void onClick(View v) {
            if(v.getId() == R.id.ButtonTestPlayPause){
                 /** ImageButton onClick event handler. Method which start/pause mediaplayer playing */
                try {
                    mediaPlayer.setDataSource(editTextSongURL.getText().toString()); // setup song from http://www.hrupin.com/wp-content/uploads/mp3/testsong_20_sec.mp3 URL to mediaplayer data source
                    mediaPlayer.prepare(); // you must call this method after setup the datasource in setDataSource method. After calling prepare() the instance of MediaPlayer starts load data from URL to internal buffer. 
                } catch (Exception e) {
                    e.printStackTrace();
                }

                mediaFileLengthInMilliseconds = mediaPlayer.getDuration(); // gets the song length in milliseconds from URL

                if(!mediaPlayer.isPlaying()){
                    mediaPlayer.start();
                    buttonPlayPause.setImageResource(R.drawable.button_pause);
                }else {
                    mediaPlayer.pause();
                    buttonPlayPause.setImageResource(R.drawable.button_play);
                }

                primarySeekBarProgressUpdater();
            }
        }

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(v.getId() == R.id.SeekBarTestPlay){
                /** Seekbar onTouch event handler. Method which seeks MediaPlayer to seekBar primary progress position*/
                if(mediaPlayer.isPlaying()){
                    SeekBar sb = (SeekBar)v;
                    int playPositionInMillisecconds = (mediaFileLengthInMilliseconds / 100) * sb.getProgress();
                    mediaPlayer.seekTo(playPositionInMillisecconds);
                }
            }
            return false;
        }

        @Override
        public void onCompletion(MediaPlayer mp) {
             /** MediaPlayer onCompletion event handler. Method which calls then song playing is complete*/
            buttonPlayPause.setImageResource(R.drawable.button_play);
        }

        @Override
        public void onBufferingUpdate(MediaPlayer mp, int percent) {
            /** Method which updates the SeekBar secondary progress by current song loading from URL position*/
            seekBarProgress.setSecondaryProgress(percent);
        }
    }

我的问题是这段代码不适用于大约一小时长度的 mp3 url。有人知道如何播放冗长的 mp3 url

刚刚尝试从答案 1 更新:

public class Mp3 extends Activity implements OnClickListener {

    Button button;
    private MediaPlayer mediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.button);


        button = (Button)findViewById(R.id.button1);
        button.setOnClickListener(this);


        mediaPlayer = new MediaPlayer();


    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        switch(v.getId()){

        case R.id.button1:

            startSound("http://str2.creacast.com:80/pha-michy1");

            break;


        }
    }


    public void startSound(final String URL) {
        button.setText("STOP");
          new Thread() {
            public void run() {
                try {
                    Uri uri = Uri.parse(URL);

                    mediaPlayer = MediaPlayer.create(Mp3.this, uri);
                    mediaPlayer.start();
                    mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
                        @Override
                        public void onCompletion(MediaPlayer arg0) {
                            button.setEnabled(true);
                            button.setText("PLAY");
                        }
                    });
                } catch (Exception e) {

                }
               // myProgressDialog.dismiss();
            }
          }.start();
        }




}

我的日志显示此错误。

05-24 09:09:02.652: I/MediaPlayer(1548): uri 
05-24 09:09:02.652: I/MediaPlayer(1548): path is null
05-24 09:09:02.652: D/MediaPlayer(1548): Couldn't open file on client side, trying server side
05-24 09:09:16.707: E/MediaPlayer(1548): error (1, -2147483648)
05-24 09:09:16.707: D/MediaPlayer(1548): create failed:
05-24 09:09:16.707: D/MediaPlayer(1548): java.io.IOException: Prepare failed.: status=0x1
05-24 09:09:16.707: D/MediaPlayer(1548):    at android.media.MediaPlayer.prepare(Native Method)
05-24 09:09:16.707: D/MediaPlayer(1548):    at android.media.MediaPlayer.create(MediaPlayer.java:636)
05-24 09:09:16.707: D/MediaPlayer(1548):    at android.media.MediaPlayer.create(MediaPlayer.java:613)
05-24 09:09:16.707: D/MediaPlayer(1548):    at com.hrupin.streamingmedia.Mp3$1.run(Mp3.java:60)

强文本*更新:当我尝试您的第二个网址时,我的日志中出现此错误。你能检查一下吗?*强文本

  05-24 09:59:14.398: D/dalvikvm(2228): Debugger has detached; object registry had 1 entries 05-24 09:59:14.582: E/MediaPlayer(2228): start called in state 4 05-24 09:59:14.582: E/MediaPlayer(2228): error (-38, 0) 05-24 09:59:14.585: E/MediaPlayer(2228): Error (-38,0) 05-24 09:59:14.632: E/MediaPlayer(2228): error (1, -1002) 05-24 09:59:14.683: E/MediaPlayer(2228): Error (1,-1002) 05-24 10:00:07.843: I/MediaPlayer(2371): uri is:http://www.usa8-vn.mixstream.net:8138 05-24 10:00:07.843: I/MediaPlayer(2371): path is null 05-24 10:00:07.847: D/MediaPlayer(2371): Couldn't open file on client side, trying server side 05-24 10:00:07.863: E/MediaPlayer(2371): error (1, -1002) 05-24 10:00:07.863: D/MediaPlayer(2371): create failed: 05-24 10:00:07.863: D/MediaPlayer(2371): java.io.IOException: Prepare failed.: status=0x1 05-24 10:00:07.863: D/MediaPlayer(2371): at android.media.MediaPlayer.prepare(Native Method) 05-24 10:00:07.863: D/MediaPlayer(2371): at android.media.MediaPlayer.create(MediaPlayer.java:636) 05-24 10:00:07.863: D/MediaPlayer(2371): at android.media.MediaPlayer.create(MediaPlayer.java:613) 05-24 10:00:07.863: D/MediaPlayer(2371): at com.hrupin.streamingmedia.Mp3$1.run(Mp3.java:68) 05-24 10:00:38.300: I/MediaPlayer(2411): uri is:http://usa8-vn.mixstream.net:8138 05-24 10:00:38.300: I/MediaPlayer(2411): path is null 05-24 10:00:38.300: D/MediaPlayer(2411): Couldn't open file on client side, trying server side
4

1 回答 1

0

试试这个代码,让我知道

public void startSound(final String URL) {
  bt.setText("STOP");
  new Thread() {
    public void run() {
        try {
            Uri uri = Uri.parse(URL);

            mp = MediaPlayer.create(PlaySound.this, uri);
            mp.start();
            mp.setOnCompletionListener(new OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer arg0) {
                    bt.setEnabled(true);
                    bt.setText("SŁUCHAJ");
                }
            });
        } catch (Exception e) {

        }
        myProgressDialog.dismiss();
    }
  }.start();
}

当我按下后退按钮时:

@Override
public void onBackPressed() {
    super.onBackPressed();
            mp.stop();
    }
}

来源从这里得到

也试试这个

   String url = "http://www.songblasts.com/songs/hindi/t/three-idiots/01-Aal_Izz_Well-(SongsBlasts.Com).mp3";
   MediaPlayer mp = new MediaPlayer();
  try {
     mp.setDataSource(url);
     mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
     mp.prepareAsync();
     mp.start();
    }
 catch(Exception e)
     {
         e.printStackTrace();
     }
于 2013-04-24T04:49:11.193 回答