0

I'm new to Android development and trying to make a ShoutCast radio app. So far the progress was good until I saw a ViewPager tutorial and now I'm stuck with it. Here is the question:

In first fragment I want my radio stream plays. I can get the stream now but it is in an activity class so I can't do the same thing in my fragment. And in my second fragment I want another activity to run etc.

http://www.youtube.com/watch?v=C6_1KznO95A this is the tutorial I'm trying to implement (if you want I can put the code and the library here.) And here is the code I get the stream and the other things:

PS: I can write your name in the special thanks part of the app if you help me with this problem :)

package com.example.radyo;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;

import com.spoledge.aacdecoder.AACPlayer;


import net.moraleboost.streamscraper.ScrapeException;
import net.moraleboost.streamscraper.Scraper;
import net.moraleboost.streamscraper.Stream;
import net.moraleboost.streamscraper.scraper.ShoutCastScraper;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class myMain extends Activity implements OnClickListener, OnTouchListener {

    private AACPlayer player;
    private Scraper scraper;
    List<Stream> streams;
    private ImageView onButton;
    private ImageView offButton;
    private TextView currPlayView;
    private static final int HELLO_ID = 1;
    NotificationManager mNotificationManager;
    Notification notification;
    int icon;
    private static final String TAG = "MainActivity";
    private boolean headSetPlugged = false;
    private MusicIntentReceiver myReceiver;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.activity_main);

        initializeUIElements();
        initializeMediaPlayer();

        String ns = Context.NOTIFICATION_SERVICE;
        mNotificationManager = (NotificationManager) getSystemService(ns);

        icon = R.drawable.rbnot;
        CharSequence tickerText = "Radyo Bilkent";
        long when = System.currentTimeMillis();

        notification = new Notification(icon, tickerText, when);
        notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;   
        Intent notificationIntent = new Intent(this, myMain.class);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        Context context = getApplicationContext();
        CharSequence contentTitle = "Radyo Bilkent";
        CharSequence contentText = currPlayView.getText();

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

        mNotificationManager.notify(HELLO_ID, notification);

        myReceiver = new MusicIntentReceiver();



    }

    private void initializeUIElements() 
    {
        onButton = (ImageView) findViewById(R.id.onButton);
        onButton.setOnTouchListener(this);
        offButton = (ImageView) findViewById(R.id.offButton);
        offButton.setOnTouchListener(this);

        currPlayView = (TextView) findViewById(R.id.textView1);
    }


    private void startPlaying() {

        try {
            player.playAsync("http://sunucu2.radyolarburada.com:5000/", 32);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
    }

    private void stopPlaying() 
    {
        player.stop();   
    }

    private void initializeMediaPlayer() {

        player = new AACPlayer();   
        scraper = new ShoutCastScraper();
        try {
            streams = scraper.scrape(new URI("http://sunucu2.radyolarburada.com:5000/"));
            String str = "asd";
            for(Stream stream : streams)
            {
                str = stream.getCurrentSong();
                currPlayView.setText("Now Playing: " + str);
            }
        } catch (ScrapeException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (URISyntaxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } 

    @Override
    public void onBackPressed() {

        new AlertDialog.Builder(this)
        .setTitle("Exit")
        .setMessage("Are you sure to exit from this fabulous radio station??")
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) { 
                player.stop();

                try {
                    this.finalize();
                } catch (Throwable e) {
                    e.printStackTrace();
                }

                myMain.this.finish();
                mNotificationManager.cancel(HELLO_ID);
            }
         })
        .setNegativeButton("No", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) { 
                Toast.makeText(getApplicationContext(), "Happy to see you here again!", Toast.LENGTH_LONG);
            }
         })
         .show();
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        if (v == onButton)
        {
            startPlaying();
            offButton.setVisibility(View.VISIBLE);
            onButton.setVisibility(View.INVISIBLE);
        }

        if (v == offButton)
        {
            stopPlaying();
            onButton.setVisibility(View.VISIBLE);
            offButton.setVisibility(View.INVISIBLE);
        }

        return false;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }

    private class MusicIntentReceiver extends BroadcastReceiver {
        @Override public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
                int state = intent.getIntExtra("state", -1);
                switch (state) {
                case 0:
                    headSetPlugged = false;
                    break;
                case 1:
                    headSetPlugged = true;
                    break;
                default:
                    headSetPlugged = false;
                }
            }
        }
    }

    public void onResume() {
        IntentFilter filter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
        registerReceiver(myReceiver, filter);
        super.onResume();
    }

    @Override 
    public void onPause() {
        unregisterReceiver(myReceiver);
        super.onPause();
    }
}
4

0 回答 0