我创建了带有导航抽屉的Android应用程序来流式传输在线广播电台但是当我按下播放按钮开始服务时没有声音。如何解决这个问题?
后台服务
public class BackgroundService extends Service implements OnCompletionListener
{
MediaPlayer mediaPlayer;
private String STREAM_URL = "http://178.149.88.126:8010/";
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate()
{
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
String radio = sharedPreferences.getString("station", "100");
if (radio != null && radio.equals("100"))
STREAM_URL = "http://178.149.88.126:8000/";
if (radio != null && radio.equals("200"))
STREAM_URL = "http://178.149.88.126:8010/";
mediaPlayer = new MediaPlayer();
try
{
mediaPlayer.setDataSource(STREAM_URL);
} catch (IOException e)
{
e.printStackTrace();
}
mediaPlayer.setOnCompletionListener(this);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
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();
}
}
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);
}
}
主要片段
public class Main extends Fragment
{
private ImageButton buttonPlay;
private MediaPlayer mPlayer;
Intent playbackServiceIntent;
private SeekBar volumeSeekbar = null;
private AudioManager audioManager = null;
Calendar calendar = Calendar.getInstance();
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.stop);
buttonPlay.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
if (Integer.parseInt(buttonPlay.getTag().toString()) == 1)
{
buttonPlay.setImageResource(R.drawable.play);
view.setTag(0);
getActivity().startService(playbackServiceIntent);
Log.e("Play", "onPlay");
Toast.makeText(getActivity(), "Play", Toast.LENGTH_SHORT).show();
}
else
{
buttonPlay.setImageResource(R.drawable.stop);
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.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);
}
});
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
MainActivity
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener
{
NavigationView navigationView = null;
Toolbar toolbar = null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
setTheme(R.style.AppTheme_AppBarOverlay);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initial fragment
Main main = new Main();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, main);
fragmentTransaction.commit();
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
if(Build.VERSION.SDK_INT >= 21)
{
Window window = this.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(this.getResources().getColor(R.color.colorPrimary));
}
Intent serviceIntent = new Intent(this, NotificationService.class);
serviceIntent.setAction("FRS");
startService(serviceIntent);
}
boolean doubleBackToExitPressedOnce = false;
@Override
public void onBackPressed()
{
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
boolean confirmation_dialog = sharedPreferences.getBoolean("dialog",false);
if(confirmation_dialog)
{
//Checking for fragment count on backstack
if (getSupportFragmentManager().getBackStackEntryCount() > 0)
{
getSupportFragmentManager().popBackStack();
} else if (!doubleBackToExitPressedOnce)
{
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Please click BACK again to exit.", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable()
{
@Override
public void run()
{
doubleBackToExitPressedOnce = false;
}
}, 2000);
} else
{
super.onBackPressed();
return;
}
}
else
{
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START))
{
drawer.closeDrawer(GravityCompat.START);
} else
{
super.onBackPressed();
}
}
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item)
{
// Handle navigation view item clicks here.
int id = item.getItemId();
Intent intent;
if(id == R.id.home)
{
Main main = new Main();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, main);
fragmentTransaction.commit();
}
if(id == R.id.settings)
{
intent = new Intent(this, SettingsActivity.class);
startActivity(intent);
/*Settings settings = new Settings();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, settings);
fragmentTransaction.commit();*/
}
if(id == R.id.about)
{
intent = new Intent(this, About.class);
startActivity(intent);
}
if(id == R.id.send)
{
Feedback feedback = new Feedback();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, feedback);
fragmentTransaction.commit();
}
if(id == R.id.share)
{
try
{
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_SUBJECT, "Audiophileradio");
String sAux = "\nLet me recommend you this application\n\n";
sAux = sAux + "https://play.google.com/store/apps/details?id=Orion.Soft \n\n";
i.putExtra(Intent.EXTRA_TEXT, sAux);
startActivity(Intent.createChooser(i, "choose one"));
} catch (Exception e)
{
//e.toString();
}
}
if(id == R.id.exit)
{
//stopService(playbackServiceIntent);
finish();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
@Override
public void onDestroy()
{
super.onDestroy();
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotificationManager.cancel(0);
}
}
答案 0 :(得分:0)
我已经解决了这个问题。我忘了在AndroidManifest.xml中添加rbindlist(mget(ls(pattern = "dump\\d+")))[, .(c= sum(c)), .(a, b)]