停止重复的无线电流动android

时间:2016-01-06 13:38:18

标签: android radio

我的应用有问题。我有一个流媒体广播电台的应用程序。当用户单击gridview的项目时,流开始。为了停止流,我做了一个按钮(停止),当流开始时出现。当用户单击gridview的相同项目多次时会出现问题,因为当单击停止按钮时,流不会停止,我该如何解决?

public class MainActivity extends ActionBarActivity {
private GridView grid;
private InterstitialAd interstitial, inter2;
private TextView name;
private ProgressBar bar;
private ImageView stop;
private RelativeLayout rela;
private RadioPlayerManager mRadioManager;
private String[] web = {
        "Radio RDS",
        "Rai 1 Radio",
        "Radio R101",
        "Radio RTL102.5",
        "Radio Virgin",
        "Radio RMC",
        "Radio Antenna1",
        "Radio 105",
        "Radio Milano",
        "Radio Onda Libera",
};
private int[] imageId = {
        R.drawable.playy,
        R.drawable.playy,
        R.drawable.playy,
        R.drawable.playy,
        R.drawable.playy,
        R.drawable.playy,
        R.drawable.playy,
        R.drawable.playy,
        R.drawable.playy,
        R.drawable.playy,
        R.drawable.playy,
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mRadioManager = new RadioPlayerManager();
    rela = (RelativeLayout) findViewById(R.id.relativeLayout);
    bar = (ProgressBar) findViewById(R.id.progressBar);
    stop = (ImageView) findViewById(R.id.imageView);
    name = (TextView) findViewById(R.id.textView3);
    grid = (GridView) findViewById(R.id.gridView1);
    bar.setVisibility(View.GONE);
    rela.setVisibility(View.GONE);
    CustomGrid adapter = new CustomGrid(MainActivity.this, web, imageId);
    grid.setAdapter(adapter);
    ads();
    listeners();
    firstRun();
    animations();
}

private void ads() {
    interstitial = new InterstitialAd(this);
    interstitial.setAdUnitId("ca-app-pub-5426545253667840/9595287619");
    inter2 = new InterstitialAd(this);
    inter2.setAdUnitId("ca-app-pub-5426545253667840/9595287619");
    AdRequest adRequest1 = new AdRequest.Builder().build();
    inter2.loadAd(adRequest1);
    inter2.setAdListener(new AdListener() {
        public void onAdLoaded() {
            displayInterstitial();
        }
    });
    AdRequest adRequest2 = new AdRequest.Builder().build();
    interstitial.loadAd(adRequest2);
    interstitial.setAdListener(new AdListener() {
        public void onAdLoaded() {
            displayInterstitial();
        }

        public void onAdClosed() {
            AdRequest adRequest = new AdRequest.Builder()
                    .addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();
            // Load the interstitial ad again
            interstitial.loadAd(adRequest);
        }
    });
}

public void displayInterstitial() {
    if (interstitial.isLoaded()) {
    }
}

private void firstRun() {
    boolean firstrun = getSharedPreferences("PREFERENCE", MODE_PRIVATE).getBoolean("firstrun", true);
    if (firstrun) {
        new android.app.AlertDialog.Builder(this)
                .setTitle("Radio Italia") //set the Title text
                .setMessage("Ciao! " +
                        "Con Radio Italia puoi ascoltare la maggior parte delle radio italiane gratis, senza nessun costo." +
                        "Radio Italia ha al suo interno degli spot(ads) che aiutano lo sviluppatore a continuare il suo lavoro." +
                        "Se ti piace l'app non dimenticarti di darli 5 stele su Play Store. Grazie!")
                .setNeutralButton("OK!", null).show(); //Sets the button type
    }
    // Save the state with shared preferences
    getSharedPreferences("PREFERENCE", MODE_PRIVATE)
            .edit()
            .putBoolean("firstrun", false)
            .commit();
}

private void listeners() {
    grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            rela.setVisibility(View.VISIBLE);
            bar.setVisibility(View.VISIBLE);
            if (position == 0) {

                mRadioManager.play("http://www.rds.it:8000/stream", mediaPlayerCallback);
                name.setText("Stai ascoltando RDS");
                bar.setVisibility(View.INVISIBLE);
            }
            if (position == 1) {
                mRadioManager.play("http://icestreaming.rai.it/1.mp3", mediaPlayerCallback);
                name.setText("Stai ascoltando Rai 1");
                bar.setVisibility(View.INVISIBLE);
            }
            if (position == 2) {
                mRadioManager.play("http://str30.creacast.com/r101a", mediaPlayerCallback);
                name.setText("Stai ascoltando R101");
                bar.setVisibility(View.INVISIBLE);
            }
            if (position == 3) {
                mRadioManager.play("http://shoutcast.rtl.it:3010/stream/1/", mediaPlayerCallback);
                name.setText("Stai ascoltando RTL102.5");
                bar.setVisibility(View.INVISIBLE);
            }
            if (position == 4) {
                mRadioManager.play("http://shoutcast.unitedradio.it:1301/", mediaPlayerCallback);
                name.setText("Stai ascoltando Virgin Radio");
                bar.setVisibility(View.INVISIBLE);
            }
            if (position == 5) {
                mRadioManager.play("http://shoutcast.unitedradio.it:1103/", mediaPlayerCallback);
                name.setText("Stai ascoltando RMC");
                bar.setVisibility(View.INVISIBLE);
            }
            if (position == 6) {
                mRadioManager.play("http://s3.mediastreaming.it:7568/", mediaPlayerCallback);
                name.setText("Stai ascoltando Antenna1");
                bar.setVisibility(View.INVISIBLE);
            }
            if (position == 7) {
                mRadioManager.play("http://shoutcast.unitedradio.it:1101/", mediaPlayerCallback);
                name.setText("Stai ascoltando Radio105");
                bar.setVisibility(View.INVISIBLE);
            }
            if (position == 8) {
                mRadioManager.play("http://sh1.inmystream.info:8175/", mediaPlayerCallback);
                name.setText("Stai ascoltando Radio Milano");
                bar.setVisibility(View.INVISIBLE);
            }
            if (position == 9) {
                mRadioManager.play("http://s6.mediastreaming.it:9064/", mediaPlayerCallback);
                name.setText("Stai ascoltando Onda Libera");
                bar.setVisibility(View.INVISIBLE);
            } else {
                mRadioManager.stop();
            }
        }

        ;
    });
    stop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mRadioManager.stop();
            rela.setVisibility(View.INVISIBLE);
            interstitial.show();

        }
    });
}

private void animations() {
            Animation anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.abc_fade_in);
            anim.setDuration(1500);
            name.setAnimation(anim);
            anim.setRepeatCount(Animation.INFINITE);
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu items for use in the action bar
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.menu_main, menu);
            return super.onCreateOptionsMenu(menu);
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            switch (item.getItemId()) {
                case R.id.share:
                    shareIt();
                    break;
                case R.id.play:
                    gp();
                    break;
                case R.id.like:
                    fb();
                    break;
                case R.id.plus:
                    plus();

                    break;
            }
            return true;
        }

        private void fb() {
            try {
                Intent fb = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://page/315814045239841"));
                startActivity(fb);
            } catch (Exception e) {
                Intent fb = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/Denis-Projects-315814045239841/"));
                startActivity(fb);
            }
        }

        private void shareIt() {
            String shareBody = "Ascult cele mai tari posturi de manele cu aplicatia Radio manele 2016. Descarc-o si tu,e gratuit! https://play.google.com/store/apps/details?id=com.denisprojects.radiomanele2016";
            Intent sharingIntent = new Intent(Intent.ACTION_SEND);
            sharingIntent.setType("text/plain");
            sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "Radio manele 2016");
            sharingIntent.putExtra(Intent.EXTRA_TEXT, shareBody);
            startActivity(Intent.createChooser(sharingIntent, "Distribuie"));
        }

        private void gp() {
            // TODO Auto-generated method stub
            Intent gp = new Intent(Intent.ACTION_VIEW);
            gp.setData(Uri.parse("https://play.google.com/store/apps/developer?id=DenisProjects"));
            startActivity(gp);
        }

        private void plus() {
            Intent plus = new Intent(Intent.ACTION_VIEW);
            plus.setData(Uri.parse("https://plus.google.com/u/0/102131527060290623635"));
            startActivity(plus);
        }
private MediaPlayerCallback mediaPlayerCallback = new MediaPlayerCallback() {
    @Override
    public void onMetadata(String value){
    }
    @Override
    public void onError() { rela.setVisibility(View.VISIBLE);
        name.setText("Errore!Scegli altra radio");
    }

    @Override
    public void onBuffering() {
    }

    @Override
    public void onPlaying() {

    }

    @Override
    public void onStopped() {
    }
};

}

public class RadioPlayerManager {
private MultiPlayer mMultiPlayer;
private MediaPlayerCallback mCallback;
private static String TAG = RadioPlayerManager.class.getSimpleName();

public RadioPlayerManager() {
    try {
        java.net.URL.setURLStreamHandlerFactory(new java.net.URLStreamHandlerFactory() {
            public java.net.URLStreamHandler createURLStreamHandler(String protocol) {
                Log.d(TAG, "Asking for stream handler for protocol: '" + protocol + "'");
                if ("icy".equals(protocol))
                    return new com.spoledge.aacdecoder.IcyURLStreamHandler();
                return null;
            }
        });
    } catch (Throwable t) {
        Log.w(TAG, "Cannot set the ICY URLStreamHandler - maybe already set ? - " + t);
    }
}


public void play(String url, MediaPlayerCallback callback) {
        mCallback = callback;
        mMultiPlayer = new MultiPlayer(mPlayerCallback, MultiPlayer.DEFAULT_AUDIO_BUFFER_CAPACITY_MS, MultiPlayer.DEFAULT_DECODE_BUFFER_CAPACITY_MS);
        mMultiPlayer.playAsync(url);
        mCallback.onBuffering();
}

public void stop() {
    mMultiPlayer.stop();
}

private PlayerCallback mPlayerCallback = new PlayerCallback() {
    @Override
    public void playerStarted() {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                mCallback.onPlaying();
            }
        });
    }

    @Override
    public void playerPCMFeedBuffer(boolean b, int i, int i1) {

    }

    @Override
    public void playerStopped(int i) {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                mCallback.onStopped();

            }
        });
    }

    @Override
    public void playerException(Throwable throwable) {
        mCallback.onError();
    }

    @Override
    public void playerMetadata(String key, final String value) {
        if ("StreamTitle".equals(key)) {
            new Handler(Looper.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    mCallback.onMetadata(value);
                }
            });
        }
    }

    @Override
    public void playerAudioTrackCreated(AudioTrack audioTrack) {

    }
    public boolean isPlaying(){
       mMultiPlayer.stop();
        return true;
    }
};

}

public interface MediaPlayerCallback {
void onMetadata(String value);
void onError();
void onBuffering();
void onPlaying();
void onStopped();

}

没人知道该怎么做?

1 个答案:

答案 0 :(得分:0)

如果所选的gridview项目已在播放,最好不要开始播放。所以你可以存储播放gridview项的值并在调用mRadioManager.play(“http://s6.mediastreaming.it:9064/”,mediaPlayerCallback)之前检查它;