Click here to Skip to main content
15,881,852 members
Home / Discussions / Android
   

Android

 
GeneralRe: Android connect to MS SQL Server Pin
Richard MacCutchan18-May-17 22:12
mveRichard MacCutchan18-May-17 22:12 
QuestionRe: Android connect to MS SQL Server Pin
David Crow19-May-17 3:11
David Crow19-May-17 3:11 
AnswerRe: Android connect to MS SQL Server Pin
Richard MacCutchan19-May-17 6:33
mveRichard MacCutchan19-May-17 6:33 
GeneralRe: Android connect to MS SQL Server Pin
David Crow19-May-17 6:35
David Crow19-May-17 6:35 
Question(Short Video Demo) Fragment back navigation issue and overlapping fragments using a frame layout Pin
Daniel Antony Ali17-May-17 18:41
Daniel Antony Ali17-May-17 18:41 
QuestionAnyone can help Pin
wseng16-May-17 21:58
wseng16-May-17 21:58 
AnswerRe: Anyone can help Pin
Richard MacCutchan16-May-17 22:46
mveRichard MacCutchan16-May-17 22:46 
QuestionAndroid media player won't start Pin
Pavlex413-May-17 2:08
Pavlex413-May-17 2:08 
I have created android application to stream online radio stations but when I click start button to play radio it won't start.In service I read ip address of file from url and add it to string.When user selects radio station I add port to string with ip address.

Java
public class BackgroundService extends Service implements OnCompletionListener
    {
        MediaPlayer mediaPlayer;
        private String STREAM_URL;
        final String textSource = "http://audiophileradio.stream/Ip.txt";

        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }

        @Override
        public void onCreate()
        {

        }

        @Override
        public int onStartCommand(Intent intent, int flags, int startId)
        {
            new MyTask().execute();

            return START_STICKY;
        }

        public void onDestroy() {
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.stop();
            }
            mediaPlayer.release();
        }

        public void onCompletion(MediaPlayer _mediaPlayer) {
            stopSelf();
        }


        @Override
        public boolean onUnbind(Intent intent)
        {
            return super.onUnbind(intent);
        }

        private class MyTask extends AsyncTask<Void, Void, String>
        {
            String textResult;

            @Override
            protected String doInBackground(Void... params) {

                URL textUrl;

                try {
                    textUrl = new URL(textSource);

                    BufferedReader bufferReader
                            = new BufferedReader(new InputStreamReader(textUrl.openStream()));

                    String StringBuffer;
                    String stringText = "";
                    while ((StringBuffer = bufferReader.readLine()) != null) {
                        stringText += StringBuffer;
                    }
                    bufferReader.close();

                    textResult = stringText;
                    return textResult;
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                    textResult = e.toString();
                } catch (IOException e) {
                    e.printStackTrace();
                    textResult = e.toString();
                }

                return null;

            }

            @Override
            protected void onPostExecute(String result) {

                Log.d("DebugTag", "Value: " + textResult);

                super.onPostExecute(result);

                SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(BackgroundService.this);
                String radio = sharedPreferences.getString("station", "8000");

                if (radio != null)
                {
                    STREAM_URL += ":" + radio;
                }

                mediaPlayer = new MediaPlayer();

                if (!mediaPlayer.isPlaying())
                {
                    try
                    {
                        mediaPlayer.reset();
                        mediaPlayer.setDataSource(STREAM_URL);
                        mediaPlayer.prepareAsync();

                        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
                        {
                            @Override
                            public void onPrepared(MediaPlayer mp)
                            {
                                mediaPlayer.start();
                            }
                        });
                    } catch (IOException e)
                    {
                        e.printStackTrace();
                    }

                }

                mediaPlayer.setOnCompletionListener(BackgroundService.this);
            }
        }

    }


Main.java

Java
public class Main extends Fragment
{
    private ImageButton buttonPlay;
    private MediaPlayer mPlayer;
    Intent playbackServiceIntent;

    private SeekBar volumeSeekbar = null;
    private AudioManager audioManager = null;

    View view;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        view = inflater.inflate(R.layout.fragment_main,container,false);

        buttonPlay = (ImageButton) view.findViewById(R.id.buttonPlay);

        mPlayer = new MediaPlayer();

        initControls();

        buttonPlay.setTag(1);
        buttonPlay.setImageResource(R.drawable.play);
        buttonPlay.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                if (Integer.parseInt(buttonPlay.getTag().toString()) == 1)
                {
                    buttonPlay.setImageResource(R.drawable.stop);
                    view.setTag(0);

                    getActivity().startService(playbackServiceIntent);

                    Log.e("Play", "onStop");
                    Toast.makeText(getActivity(), "Play", Toast.LENGTH_SHORT).show();
                } else
                {
                    buttonPlay.setImageResource(R.drawable.play);
                    view.setTag(1);
                    mPlayer.stop();
                    getActivity().stopService(playbackServiceIntent);
                    Log.e("Stop", "onPlay");
                    Toast.makeText(getActivity(), "Stop", Toast.LENGTH_SHORT).show();
                }

            }
        });
        playbackServiceIntent = new Intent(getActivity(), BackgroundService.class);

        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState)
    {
        super.onViewCreated(view, savedInstanceState);

        getActivity().setTitle("Audiophileradio");
    }

    private void startService()
    {
        getActivity().startService(new Intent(getActivity(),BackgroundService.class));
    }
    private void stopService()
    {
        getActivity().stopService(new Intent(getActivity(),BackgroundService.class));
    }

    private void initControls()
    {
        try
        {
            volumeSeekbar = (SeekBar) view.findViewById(R.id.seekBar);
            audioManager = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
            volumeSeekbar.setMax(audioManager
                    .getStreamMaxVolume(AudioManager.STREAM_MUSIC));
            volumeSeekbar.setProgress(audioManager
                    .getStreamVolume(AudioManager.STREAM_MUSIC));
            //volumeSeekbar.setMax(100);
            volumeSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
            {
                @Override
                public void onStopTrackingTouch(SeekBar arg0)
                {
                }

                @Override
                public void onStartTrackingTouch(SeekBar arg0)
                {
                }

                @Override
                public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
                {
                    audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
                            progress, 0);

                    //volume.setText("Volume : " + progress + "%");
                }
            });
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}


modified 13-May-17 9:54am.

AnswerRe: Android media player won't start Pin
Pavlex413-May-17 5:13
Pavlex413-May-17 5:13 
QuestionAndroid read file from url Pin
Pavlex412-May-17 6:39
Pavlex412-May-17 6:39 
QuestionRe: Android read file from url Pin
David Crow12-May-17 10:06
David Crow12-May-17 10:06 
QuestionAndroid Shopping Cart Pin
Member 1319622012-May-17 2:52
Member 1319622012-May-17 2:52 
AnswerRe: Android Shopping Cart Pin
Richard MacCutchan12-May-17 3:13
mveRichard MacCutchan12-May-17 3:13 
QuestionRe: Android Shopping Cart Pin
David Crow12-May-17 9:48
David Crow12-May-17 9:48 
QuestionCorrect MVVM implementation to manually login to website, go to certain link in the website, read information from html element and display it in another View. Pin
JoJo8210-May-17 6:15
JoJo8210-May-17 6:15 
AnswerRe: Correct MVVM implementation to manually login to website, go to certain link in the website, read information from html element and display it in another View. Pin
Richard MacCutchan10-May-17 6:41
mveRichard MacCutchan10-May-17 6:41 
GeneralRe: Correct MVVM implementation to manually login to website, go to certain link in the website, read information from html element and display it in another View. Pin
JoJo8210-May-17 12:07
JoJo8210-May-17 12:07 
QuestionPrevious fragment visible under the new fragment issue Pin
Pavlex46-May-17 7:59
Pavlex46-May-17 7:59 
SuggestionRe: Previous fragment visible under the new fragment issue Pin
David Crow8-May-17 6:53
David Crow8-May-17 6:53 
QuestionHelp Pin
Member 1302311825-Apr-17 14:06
Member 1302311825-Apr-17 14:06 
QuestionRe: Help Pin
Richard MacCutchan25-Apr-17 21:28
mveRichard MacCutchan25-Apr-17 21:28 
AnswerRe: Help Pin
Member 1302311826-Apr-17 0:22
Member 1302311826-Apr-17 0:22 
QuestionRe: Help Pin
David Crow26-Apr-17 3:00
David Crow26-Apr-17 3:00 
AnswerRe: Help Pin
Member 1302311826-Apr-17 3:27
Member 1302311826-Apr-17 3:27 
SuggestionRe: Help Pin
David Crow26-Apr-17 3:36
David Crow26-Apr-17 3:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.