我是Android开发新手并尝试制作ShoutCast广播应用。到目前为止,在我看到ViewPager教程之前,进展很顺利,现在我已经坚持了下来。这是一个问题:
在第一个片段中,我希望我的无线电流播放。我现在可以获得流,但它在一个活动类中,所以我不能在我的片段中做同样的事情。在我的第二个片段中,我想要另一个活动等等。
http://www.youtube.com/watch?v=C6_1KznO95A这是我正在尝试实现的教程(如果你想我可以把代码和库放在这里。) 以下是我获取流的代码和其他内容:
PS:如果你帮我解决这个问题,我可以在应用程序的特别感谢部分写下你的名字:)
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();
}
}