I want to create a radio streaming app. I am developed this app following a tutorial, but it always crashes and I don't know why. The error message doesn't really help me to solve this problem.
class HomeActivity extends Activity implements OnClickListener {
private final static String RADIO_STATION_URL =
"http://player.radiopilatus.ch/";
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.activity_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();
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.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 + "sample.mp3")));
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();
}
};
}
My activity_main.xml,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="Soucre: (http://shoutcast2.omroep.nl:8104/)"
android:layout_marginTop="10dip" android:gravity="center" />
<ProgressBar android:id="@+id/progressBar1"
android:indeterminateOnly="false" android:progressDrawable="@android:drawable/progress_horizontal"
android:indeterminateDrawable="@android:drawable/progress_indeterminate_horizontal"
android:minHeight="20dip" android:maxHeight="20dip"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_marginLeft="20dip" android:layout_marginRight="20dip"
android:layout_marginTop="10dip"></ProgressBar>
<LinearLayout android:id="@+id/linearLayout1"
android:layout_height="wrap_content" android:layout_width="match_parent"
android:layout_marginTop="20dip" android:gravity="center">
<Button android:text="Play" android:id="@+id/buttonPlay"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1"></Button>
<Button android:text="Stop" android:id="@+id/buttonStopPlay"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1"></Button>
</LinearLayout>
<LinearLayout android:id="@+id/linearLayout2"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:orientation="vertical" android:layout_marginTop="10dip">
<TextView android:text="Recording Controls" android:id="@+id/textView1"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:gravity="center_horizontal"></TextView>
<LinearLayout android:id="@+id/linearLayout3"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_marginTop="5dip" android:gravity="center_horizontal">
<Button android:text="Record" android:id="@+id/buttonRecord"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:enabled="false"></Button>
<Button android:text="Stop" android:id="@+id/buttonStopRecord"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:enabled="false"></Button>
</LinearLayout>
<TextView android:text="File Location: /sdcard/Songs/sample.mp3"
android:id="@+id/textView2" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_marginTop="5dip"
android:gravity="center_horizontal"></TextView>
</LinearLayout>
But I always get the following error message:
--------- beginning of system
10-26 20:42:01.609 2191-1731/com.google.android.googlequicksearchbox:search E/ActivityThread: Failed to find provider info for com.google.android.apps.gsa.testing.ui.audio.recorded
10-26 20:42:01.609 2191-2191/com.google.android.googlequicksearchbox:search I/MicroDetectionWorker: onReady
10-26 20:42:01.615 2191-1731/com.google.android.googlequicksearchbox:search I/MicrophoneInputStream: mic_close com.google.android.apps.gsa.staticplugins.z.c@424cee1
10-26 20:42:01.615 2191-4877/com.google.android.googlequicksearchbox:search I/MicroRecognitionRunner: Detection finished
10-26 20:42:01.615 2191-4877/com.google.android.googlequicksearchbox:search W/ErrorReporter: reportError [type: 211, code: 524300]: Error reading from input stream
10-26 20:42:01.616 2191-4877/com.google.android.googlequicksearchbox:search W/ErrorProcessor: onFatalError, processing error from engine(4)
com.google.android.apps.gsa.shared.speech.a.g: Error reading from input stream
at com.google.android.apps.gsa.staticplugins.recognizer.i.a.a(SourceFile:342)
at com.google.android.apps.gsa.staticplugins.recognizer.i.a$1.run(SourceFile:1367)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:428)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at com.google.android.apps.gsa.shared.util.concurrent.a.ak.run(SourceFile:66)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
at com.google.android.apps.gsa.shared.util.concurrent.a.ad$1.run(SourceFile:85)
Caused by: com.google.android.apps.gsa.shared.exception.GsaIOException: Error code: 393238 | Buffer overflow, no available space.
at com.google.android.apps.gsa.speech.audio.Tee.g(SourceFile:2531)
at com.google.android.apps.gsa.speech.audio.ap.read(SourceFile:555)
at java.io.InputStream.read(InputStream.java:101)
at com.google.android.apps.gsa.speech.audio.al.run(SourceFile:362)
at com.google.android.apps.gsa.speech.audio.ak$1.run(SourceFile:471)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:428)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at com.google.android.apps.gsa.shared.util.concurrent.a.ak.run(SourceFile:66)
at com.google.android.apps.gsa.shared.util.concurrent.a.ax.run(SourceFile:139)
at com.google.android.apps.gsa.shared.util.concurrent.a.ax.run(SourceFile:139)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
at com.google.android.apps.gsa.shared.util.concurrent.a.ad$1.run(SourceFile:85)
10-26 20:42:01.616 2191-2474/com.google.android.googlequicksearchbox:search I/MicroRecognitionRunner: Stopping hotword detection.
10-26 20:42:01.616 2191-4877/com.google.android.googlequicksearchbox:search I/AudioController: internalShutdown
10-26 20:42:01.619 2191-2191/com.google.android.googlequicksearchbox:search I/MicroDetector: Keeping mic open: false
10-26 20:42:01.619 2191-2191/com.google.android.googlequicksearchbox:search I/MicroDetectionWorker: #onError(false)
10-26 20:42:01.619 2191-4876/com.google.android.googlequicksearchbox:search
I/DeviceStateChecker: DeviceStateChecker cancelled
Did I miss something? The app always crashes. Thanks in advance