0

i have a problem: I'm trying to create a radio app streaming for android, with this code:

package com.radio.radiostar;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import java.io.IOException;


 public class MainActivity extends Activity implements OnClickListener {

private final static String RADIO_STATION_URL = "myurl";

private ProgressBar playSeekBar;

private Button buttonPlay;

private Button buttonStopPlay;

private MediaPlayer player;

/** 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() {



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

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

}

public void onClick(View v) {
    if (v == buttonPlay) {
        startPlaying();
    } else if (v == buttonStopPlay) {
        stopPlaying();

    }
}

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

}

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

    buttonPlay.setEnabled(true);
    buttonStopPlay.setEnabled(false);
    playSeekBar.setVisibility(View.INVISIBLE);

}

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


};

I have changed here the real url of radio in string 18. The problem: when i tap on the play button, the app go in force close :/

this is the activity_main

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.radio.radiostar.MainActivity" >

    <MediaController
        android:id="@+id/mediaController1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

    </MediaController>
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true" >

    <Button
        android:id="@+id/buttonPlay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Play" />

    <Button
        android:id="@+id/buttonStopPlay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Stop" />
</LinearLayout>

</RelativeLayout>

Can you help me? I have tried a lot of combination, but nothing... Any helps? Thanks in advance, Fabio

Edit: This is the "logcat"

http://hastebin.com/oxoduleyub.avrasm

edit 2: I have an error initializing the seekbar: it seems that i must add a progressbar. Where i can find him?

Fabio Pinna
  • 55
  • 2
  • 9
  • For better responses, I recommend you edit your question with any relevant lines from [logcat](http://stackoverflow.com/questions/3643395/how-to-get-android-crash-logs). – ZoogieZork Oct 24 '14 at 18:36
  • You're not initializing your `playSeekBar` which means the call to `playSeekBar.setVisibility(View.INVISIBLE);` in your `startPlaying()` method will throw an unhandled `NullPointerException`. – Squonk Oct 24 '14 at 18:56
  • Thanks to both of them. Can I know how to initialize the seek bar? And I ll edit with logcat errors ;) – Fabio Pinna Oct 24 '14 at 19:47
  • Added logcat and new problem ;) – Fabio Pinna Oct 24 '14 at 20:00
  • Solved! Thanks Guys :D Added the progressbar and now work! – Fabio Pinna Oct 24 '14 at 20:04

0 Answers0