应用程序不启动流式广播的背景音乐
我开发了一个在线流媒体广播android应用程序。 我的应用程序在所有电台上工作正常,但背景音乐存在问题。 当我最小化我的应用程序或移动屏幕锁定或尝试返回我的应用程序主页时,背景音乐熄灭没有错误消息。
我有一个管理背景音乐的课程
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class Station extends AppCompatActivity {
Button b_play;
MediaPlayer mediaPlayer;
Boolean prepared = false;
Boolean started = false;
private Button btn;
private Boolean playPause;
private boolean initialStage = true;
Toolbar mtoolbar;
ImageView flag;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_station);
mtoolbar = (Toolbar) findViewById(R.id.toolbar1);
flag = (ImageView) findViewById(R.id.imageView2);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
mtoolbar.setTitle(bundle.getString("Station"));
if (mtoolbar.getTitle().toString().equalsIgnoreCase("Gujrat")) {
flag.setImageDrawable(ContextCompat.getDrawable(Station.this,
R.drawable.favorites1));
String stream = "http://162.244.80.118:4900/;stream.mp3";
btn = (Button) findViewById(R.id.btn);
btn.setEnabled(false);
btn.setTag("LOADING");
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setLooping(true);
new PlayerTask().execute(stream);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view){
if(started){
started = false;
mediaPlayer.pause();
btn.setText("PLAY");
}else{
started = true;
mediaPlayer.start();
btn.setText("PAUSE");
}
}
});
} else if (mtoolbar.getTitle().toString().equalsIgnoreCase("Lahore")) {
flag.setImageDrawable(ContextCompat.getDrawable(Station.this,
R.drawable.station));
String stream = "http://162.244.80.118:3058/;stream.mp3";
btn = (Button) findViewById(R.id.btn);
btn.setEnabled(false);
btn.setTag("LOADING");
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
new PlayerTask().execute(stream);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view){
if(started){
started = false;
mediaPlayer.pause();
btn.setText("PLAY");
}else{
started = true;
mediaPlayer.start();
btn.setText("PAUSE");
}
}
});
}
}
}
private class PlayerTask extends AsyncTask<String, Void, Boolean> {
@Override
protected Boolean doInBackground(String... strings) {
Boolean prepared = false;
try {
mediaPlayer.setDataSource(strings[0]);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
initialStage = true;
playPause = false;
btn.setText("Launch Streaming");
mediaPlayer.stop();
mediaPlayer.reset();
}
});
mediaPlayer.prepare();
prepared = true;
} catch (Exception e) {
Log.e("MyAudioStreamingApp", e.getMessage());
prepared = false;
}
return prepared;
}
@Override
protected void onPostExecute(Boolean aBoolean) {
super.onPostExecute(aBoolean);
btn.setEnabled(true);
btn.setTag("PLAY");
}
}
@Override
protected void onPause(){
super.onPause();
if(started){
mediaPlayer.pause();
}
}
@Override
protected void onResume() {
super.onResume();
if (started){
mediaPlayer.start();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if(prepared){
mediaPlayer.release();
}
}
}
我的XML文件是
<?xml version="1.0" encoding="utf-8"?>
RelativeLayout xmlns:android =“http://schemas.android.com/apk/res/android”
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.ukhyips.httpwww.ayanradio.Station">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/toolbar1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="@color/colorPrimary"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
/>
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<Button
android:id="@+id/btn"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:text="PLAY"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="14dp" />
/的RelativeLayout>
这是我的清单文件代码
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission-sdk-23 android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Station"></activity>
<service android:enabled="true" android:name="Station" />
</application>
当您最小化我的应用程序或移动屏幕锁定时,背景音乐会关闭,因为您的应用程序转到onPause()状态,请参阅活动生命周期(https://developer.android.com/guide/components/activities/activity-lifecycle .html),所以如果你想在后台播放你的音乐,那么使用Service(https://developer.android.com/guide/components/services.html),什么服务是让你的应用程序在后台运行,并执行任务背景。
链接地址: http://www.djcxy.com/p/68565.html上一篇: App not starting background music of my streaming radio