无线电流媒体应用程序崩溃

时间:2017-10-27 11:38:58

标签: android streaming radio

我想创建一个无线电流媒体应用。我按照教程开发了这个应用程序,但它总是崩溃,我不知道为什么。错误消息并没有真正帮助我解决这个问题。

df_original['c'] = pd.concat([df1,df2])

}

我的activity_main.xml,

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

};

但我总是收到以下错误消息:

<?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>

我错过了什么吗?该应用程序总是崩溃。 提前致谢

0 个答案:

没有答案