本文介绍了如何发挥Shoutcast的在Android 2.2的流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在努力发挥Shoutcast的链接(.PLS)我成功了带MP3,我才知道Shoutcast的链接不工作的机器人。是否有可能玩这个链接的android http://stream.radiosai.net:8020/

 私人最终静态字符串RADIO_STATION_URL =htt​​p://stream.radiosai.net:8002/;
私人进度playSeekBar;
私人按钮buttonPlay;
私人按钮buttonStopPlay;
私人按钮buttonRecord;
私人按钮buttonSto precord;
私人的MediaPlayer播放器;
私人的InputStream recordingStream;
私人RecorderThread recorderThread;
私人布尔isRecording = FALSE;

/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);
    initializeUIElements();
    initializeMediaPlayer();
}

私人无效initializeUIElements(){
    playSeekBar =(进度)findViewById(R.id.progressBar1);
    playSeekBar.setMax(100);
    playSeekBar.setVisibility(View.INVISIBLE);

    buttonPlay =(按钮)findViewById(R.id.buttonPlay);
    buttonPlay.setOnClickListener(本);

    buttonStopPlay =(按钮)findViewById(R.id.buttonStopPlay);
    buttonStopPlay.setEnabled(假);
    buttonStopPlay.setOnClickListener(本);

    buttonRecord =(按钮)findViewById(R.id.buttonRecord);
    buttonRecord.setOnClickListener(本);

    buttonSto precord =(按钮)findViewById(R.id.buttonSto precord);
    buttonSto precord.setOnClickListener(本);
}

公共无效的onClick(视图v){
    如果(V == buttonPlay){
        startPlaying();
    }否则,如果(V == buttonStopPlay){
        stopPlaying();
    }否则,如果(V == buttonRecord){
        recorderThread =新RecorderThread();
        recorderThread.start();

        buttonRecord.setEnabled(假);
        buttonSto precord.setEnabled(真正的);
    }否则,如果(V == buttonSto precord){
        STO precording();
    }
}

私人无效startPlaying(){
    buttonStopPlay.setEnabled(真正的);
    buttonPlay.setEnabled(假);

    playSeekBar.setVisibility(View.VISIBLE);

    。玩家prepareAsync();

    player.setOn preparedListener(新在preparedListener(){

        公共无效于prepared(MediaPlayer的MP){
            player.start();
            player.pause();
            buttonRecord.setEnabled(真正的);
        }
    });

}

私人无效stopPlaying(){
    如果(player.isPlaying()){
        player.stop();
        player.release();
        initializeMediaPlayer();
    }

    buttonPlay.setEnabled(真正的);
    buttonStopPlay.setEnabled(假);
    playSeekBar.setVisibility(View.INVISIBLE);
    buttonRecord.setEnabled(假);
    buttonSto precord.setEnabled(假);
    STO precording();
}

私人无效initializeMediaPlayer(){
    球员=新的MediaPlayer();
    尝试 {
        player.reset();
        player.setDataSource(RADIO_STATION_URL);
    }赶上(抛出:IllegalArgumentException E){
        e.printStackTrace();
    }赶上(IllegalStateException异常E){
        e.printStackTrace();
    }赶上(IOException异常E){
        e.printStackTrace();
    }

    player.setOnBufferingUpdateListener(新OnBufferingUpdateListener(){

        公共无效onBufferingUpdate(MediaPlayer的熔点,INT百分比){
            playSeekBar.setSecondaryProgress(百分比);
            Log.i(缓冲,+百分比);
        }
    });
}

@覆盖
保护无效的onPause(){
    super.onPause();
    如果(player.isPlaying()){
        player.stop();
    }
}

私人无效的startRecording(){

    的BufferedOutputStream作家= NULL;
    尝试 {
        网址URL =新的URL(RADIO_STATION_URL);
        URLConnection的连接= url.openConnection();
        最后弦乐FOLDER_PATH =环境
                .getExternalStorageDirectory()。getAbsolutePath()
                +文件分割符+诗经;

        文件夹=新的文件(FOLDER_PATH);
        如果(!folder.exists()){
            folder.mkdir();
        }

        作家=新的BufferedOutputStream(新的FileOutputStream(新文件(
                FOLDER_PATH +文件分割符+listen.pls)));
        recordingStream = connection.getInputStream();

        最终诠释BUFFER_SIZE = 100;

        byte []的缓冲区=新的字节[BUFFER_SIZE];

        而(recordingStream.read(缓冲液,0,BUFFER_SIZE)!= -1
                &功放;&安培; isRecording){
            writer.write(缓冲液,0,BUFFER_SIZE);
            writer.flush();
        }

    }赶上(MalformedURLException异常E){
        e.printStackTrace();
    }赶上(IOException异常E){
        e.printStackTrace();
    } 最后 {
        尝试 {
            recordingStream.close();
            writer.flush();
            writer.close();
        }赶上(IOException异常E){
            e.printStackTrace();
        }
    }
}

私人无效STO precording(){
    buttonSto precord.setEnabled(假);
    buttonRecord.setEnabled(真正的);
    尝试 {
        isRecording = FALSE;
        如果(recordingStream!= NULL){
            recordingStream.close();
        }
    }赶上(IOException异常E){
        e.printStackTrace();
    }
}

私有类RecorderThread继承Thread {
    @覆盖
    公共无效的run(){
        isRecording = TRUE;
        的StartRecording();
    }

};
}
 

解决方案

试试这个code,detail是这里:

 进口android.app.Activity;
进口android.media.AudioManager;
进口android.media.MediaPlayer;
进口android.net.Uri;
进口android.os.Bundle;
进口android.util.Log;
进口android.view.View;
进口android.widget.Button;

公共类SimpleMusicStream扩展活动器具
  MediaPlayer.OnCompletionListener,MediaPlayer.On preparedListener,
  MediaPlayer.OnErrorListener,MediaPlayer.OnBufferingUpdateListener {

 私人字符串变量=的getClass()getSimpleName()。
 私人MediaPlayer的MP = NULL;

 私人按钮来播放;
 私人按钮暂停;
 私人按钮停止;

 @覆盖
 公共无效的onCreate(包冰柱){
  super.onCreate(冰柱);
  的setContentView(R.layout.main);

  玩=(按钮)findViewById(R.id.play);
  暂停=(按钮)findViewById(R.id.pause);
  停止=(按钮)findViewById(R.id.stop);

  play.setOnClickListener(新View.OnClickListener(){
   公共无效的onClick(视图查看){
    玩();
   }
  });

  pause.setOnClickListener(新View.OnClickListener(){
   公共无效的onClick(视图查看){
    暂停();
   }
  });

  stop.setOnClickListener(新View.OnClickListener(){
   公共无效的onClick(视图查看){
    停止();
   }
  });
 }

 私人无效播放(){
  乌里myUri = Uri.parse(http://stream.radiosai.net:8002/);
  尝试 {
   如果(MP == NULL){
    this.mp =新的MediaPlayer();
   } 其他 {
    mp.stop();
    mp.reset();
   }
   mp.setDataSource(这一点,myUri); //进入初始化状态
   mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
   mp.setOn preparedListener(本);
   mp.setOnBufferingUpdateListener(本);

   mp.setOnErrorListener(本);
   熔点:prepareAsync();

   Log.d(TAG,LoadClip完成);
  }赶上(的Throwable T){
   Log.d(TAG,t.toString());
  }
 }

 @覆盖
 公共无效于prepared(MediaPlayer的MP){
  Log.d(TAG,流是prepared);
  mp.start();
 }

 私人无效暂停(){
  mp.pause();
 }

 私人无效停止(){
  mp.stop();

 }

 @覆盖
 公共无效的onDestroy(){
  super.onDestroy();
  停止();

 }

 公共无效onCompletion(MediaPlayer的MP){
  停止();
 }

 公共布尔的onError(MediaPlayer的熔点,诠释了什么,整型附加){
  StringBuilder的SB =新的StringBuilder();
  sb.append(Media Player错误:);
  开关(什么){
  案例MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK:
   sb.append(无效,无法渐进式播放);
   打破;
  案例MediaPlayer.MEDIA_ERROR_SERVER_DIED:
   sb.append(服务器死了);
   打破;
  案例MediaPlayer.MEDIA_ERROR_UNKNOWN:
   sb.append(未知);
   打破;
  默认:
   sb.append(非标();
   sb.append(什么);
   sb.append());
  }
  sb.append((+什么+));
  sb.append(额外);
  Log.e(TAG,sb.toString());
  返回true;
 }

 公共无效onBufferingUpdate(MediaPlayer的熔点,INT百分比){
  Log.d(TAG,PlayerService onBufferingUpdate:+百分之+%);
 }

    }
 

main.xml中:

 < XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
 的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
 机器人:layout_width =FILL_PARENT
 机器人:layout_height =FILL_PARENT>
 <按钮
  机器人:文本=播放
  机器人:ID =@ + ID /播放
  机器人:layout_width =WRAP_CONTENT
  机器人:layout_height =WRAP_CONTENT>< /按钮>
 <按钮
  机器人:文本=暂停
  机器人:ID =@ + ID /暂停
  机器人:layout_width =WRAP_CONTENT
  机器人:layout_height =WRAP_CONTENT>< /按钮>
 <按钮
  机器人:文本=停止
  机器人:ID =@ + ID /停止
  机器人:layout_width =WRAP_CONTENT
  机器人:layout_height =WRAP_CONTENT>< /按钮>
< / LinearLayout中>
 

i am struggling to play an shoutcast link(.pls) i succeed with mp3 and i came to know shoutcast link is not working in android. is there any possibility to play this link in android http://stream.radiosai.net:8020/

private final static String RADIO_STATION_URL = "http://stream.radiosai.net:8002/";
private ProgressBar playSeekBar;
private Button buttonPlay;
private Button buttonStopPlay;
private Button buttonRecord;
private Button buttonStopRecord;
private MediaPlayer player;
private InputStream recordingStream;
private RecorderThread recorderThread;
private boolean isRecording = false;

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

private void initializeUIElements() {
    playSeekBar = (ProgressBar) findViewById(R.id.progressBar1);
    playSeekBar.setMax(100);
    playSeekBar.setVisibility(View.INVISIBLE);

    buttonPlay = (Button) findViewById(R.id.buttonPlay);
    buttonPlay.setOnClickListener(this);

    buttonStopPlay = (Button) findViewById(R.id.buttonStopPlay);
    buttonStopPlay.setEnabled(false);
    buttonStopPlay.setOnClickListener(this);

    buttonRecord = (Button) findViewById(R.id.buttonRecord);
    buttonRecord.setOnClickListener(this);

    buttonStopRecord = (Button) findViewById(R.id.buttonStopRecord);
    buttonStopRecord.setOnClickListener(this);
}

public void onClick(View v) {
    if (v == buttonPlay) {
        startPlaying();
    } else if (v == buttonStopPlay) {
        stopPlaying();
    } else if (v == buttonRecord) {
        recorderThread = new RecorderThread();
        recorderThread.start();

        buttonRecord.setEnabled(false);
        buttonStopRecord.setEnabled(true);
    } else if (v == buttonStopRecord) {
        stopRecording();
    }
}

private void startPlaying() {
    buttonStopPlay.setEnabled(true);
    buttonPlay.setEnabled(false);

    playSeekBar.setVisibility(View.VISIBLE);

    player.prepareAsync();

    player.setOnPreparedListener(new OnPreparedListener() {

        public void onPrepared(MediaPlayer mp) {
            player.start();
            player.pause();
            buttonRecord.setEnabled(true);
        }
    });

}

private void stopPlaying() {
    if (player.isPlaying()) {
        player.stop();
        player.release();
        initializeMediaPlayer();
    }

    buttonPlay.setEnabled(true);
    buttonStopPlay.setEnabled(false);
    playSeekBar.setVisibility(View.INVISIBLE);
    buttonRecord.setEnabled(false);
    buttonStopRecord.setEnabled(false);
    stopRecording();
}

private void initializeMediaPlayer() {
    player = new MediaPlayer();
    try {
        player.reset();
        player.setDataSource(RADIO_STATION_URL);
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    player.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {

        public void onBufferingUpdate(MediaPlayer mp, int percent) {
            playSeekBar.setSecondaryProgress(percent);
            Log.i("Buffering", "" + percent);
        }
    });
}

@Override
protected void onPause() {
    super.onPause();
    if (player.isPlaying()) {
        player.stop();
    }
}

private void startRecording() {

    BufferedOutputStream writer = null;
    try {
        URL url = new URL(RADIO_STATION_URL);
        URLConnection connection = url.openConnection();
        final String FOLDER_PATH = Environment
                .getExternalStorageDirectory().getAbsolutePath()
                + File.separator + "Songs";

        File folder = new File(FOLDER_PATH);
        if (!folder.exists()) {
            folder.mkdir();
        }

        writer = new BufferedOutputStream(new FileOutputStream(new File(
                FOLDER_PATH + File.separator + "listen.pls")));
        recordingStream = connection.getInputStream();

        final int BUFFER_SIZE = 100;

        byte[] buffer = new byte[BUFFER_SIZE];

        while (recordingStream.read(buffer, 0, BUFFER_SIZE) != -1
                && isRecording) {
            writer.write(buffer, 0, BUFFER_SIZE);
            writer.flush();
        }

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            recordingStream.close();
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

private void stopRecording() {
    buttonStopRecord.setEnabled(false);
    buttonRecord.setEnabled(true);
    try {
        isRecording = false;
        if (recordingStream != null) {
            recordingStream.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private class RecorderThread extends Thread {
    @Override
    public void run() {
        isRecording = true;
        startRecording();
    }

};
}
解决方案

Try this code, detail is here:

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class SimpleMusicStream extends Activity implements
  MediaPlayer.OnCompletionListener, MediaPlayer.OnPreparedListener,
  MediaPlayer.OnErrorListener, MediaPlayer.OnBufferingUpdateListener {

 private String TAG = getClass().getSimpleName();
 private MediaPlayer mp = null;

 private Button play;
 private Button pause;
 private Button stop;

 @Override
 public void onCreate(Bundle icicle) {
  super.onCreate(icicle);
  setContentView(R.layout.main);

  play = (Button) findViewById(R.id.play);
  pause = (Button) findViewById(R.id.pause);
  stop = (Button) findViewById(R.id.stop);

  play.setOnClickListener(new View.OnClickListener() {
   public void onClick(View view) {
    play();
   }
  });

  pause.setOnClickListener(new View.OnClickListener() {
   public void onClick(View view) {
    pause();
   }
  });

  stop.setOnClickListener(new View.OnClickListener() {
   public void onClick(View view) {
    stop();
   }
  });
 }

 private void play() {
  Uri myUri = Uri.parse("http://stream.radiosai.net:8002/");
  try {
   if (mp == null) {
    this.mp = new MediaPlayer();
   } else {
    mp.stop();
    mp.reset();
   }
   mp.setDataSource(this, myUri); // Go to Initialized state
   mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
   mp.setOnPreparedListener(this);
   mp.setOnBufferingUpdateListener(this);

   mp.setOnErrorListener(this);
   mp.prepareAsync();

   Log.d(TAG, "LoadClip Done");
  } catch (Throwable t) {
   Log.d(TAG, t.toString());
  }
 }

 @Override
 public void onPrepared(MediaPlayer mp) {
  Log.d(TAG, "Stream is prepared");
  mp.start();
 }

 private void pause() {
  mp.pause();
 }

 private void stop() {
  mp.stop();

 }

 @Override
 public void onDestroy() {
  super.onDestroy();
  stop();

 }

 public void onCompletion(MediaPlayer mp) {
  stop();
 }

 public boolean onError(MediaPlayer mp, int what, int extra) {
  StringBuilder sb = new StringBuilder();
  sb.append("Media Player Error: ");
  switch (what) {
  case MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK:
   sb.append("Not Valid for Progressive Playback");
   break;
  case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
   sb.append("Server Died");
   break;
  case MediaPlayer.MEDIA_ERROR_UNKNOWN:
   sb.append("Unknown");
   break;
  default:
   sb.append(" Non standard (");
   sb.append(what);
   sb.append(")");
  }
  sb.append(" (" + what + ") ");
  sb.append(extra);
  Log.e(TAG, sb.toString());
  return true;
 }

 public void onBufferingUpdate(MediaPlayer mp, int percent) {
  Log.d(TAG, "PlayerService onBufferingUpdate : " + percent + "%");
 }

    }

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 <Button
  android:text="Play"
  android:id="@+id/play"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"></Button>
 <Button
  android:text="Pause"
  android:id="@+id/pause"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"></Button>
 <Button
  android:text="Stop"
  android:id="@+id/stop"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"></Button>
</LinearLayout>

这篇关于如何发挥Shoutcast的在Android 2.2的流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 20:17