gpt4 book ai didi

java - 尽管我将媒体播放器实现为服务,但服务在后台几秒钟后关闭(媒体播放器)?

转载 作者:行者123 更新时间:2023-12-02 10:43:02 26 4
gpt4 key购买 nike

我正在制作一个应用程序来从 URL 流式传输 mp3。我可以毫无问题地播放和停止媒体。 然后我尝试将媒体播放器作为服务运行。但几秒钟后流就停止了。我不知道出了什么问题。请帮我解决这个问题。谢谢。以下是我的 java 文件。

这是 MainActivity.java 文件

package com.example.yomal.rathumakarafm;

import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageButton;

import java.io.IOException;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


private ImageButton buttonStart;
private ImageButton buttonStop;





@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

buttonStart = (ImageButton) findViewById(R.id.buttonStart);
buttonStop = (ImageButton)findViewById(R.id.buttonSop);

buttonStart.setOnClickListener(this);
buttonStop.setOnClickListener(this);

}

@Override
public void onClick(View v) {

if(v == buttonStart){
startService(new Intent(this, RathuMakara.class));

}

else if (v == buttonStop){
stopService(new Intent(this, RathuMakara.class));
}

}
}

这是服务类文件 RathuMakara.java

package com.example.yomal.rathumakarafm;

import android.app.Service;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.support.annotation.Nullable;

import java.io.IOException;

public class RathuMakara extends Service {

private MediaPlayer rathu;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startID) {
String url = "http://206.189.34.189:8000/rathumakara.mp3";
MediaPlayer rathu = new MediaPlayer();
rathu.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
rathu.setDataSource(url);

try {
rathu.prepare();
} catch (IOException e) {
e.printStackTrace();
}

rathu.start();
} catch (IOException e) {

e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
}

return START_STICKY;
}

@Override
public void onDestroy() {

rathu.stop();
}

}

这是activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"


>



<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp"
android:orientation="horizontal"
android:layout_gravity="center_horizontal"
android:layout_weight="1">

<ImageButton
android:id="@+id/buttonStart"
android:layout_width="66dp"
android:layout_height="68dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="120dp"
android:background="@drawable/play"
android:backgroundTint="@color/colorAccent"
android:padding="10dp" />

<ImageButton
android:id="@+id/buttonSop"
android:layout_width="65dp"
android:layout_height="66dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="20dp"
android:background="@drawable/stop"
android:backgroundTint="@color/colorAccent" />


</LinearLayout>

</LinearLayout>







</RelativeLayout>

这是 AndroidManifest 文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yomal.rathumakarafm">

<uses-permission android:name="android.permission.INTERNET" />



<application
android:allowBackup="true"
android:icon="@drawable/logo_round"
android:label="@string/app_name"
android:roundIcon="@drawable/logo_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".SplashActivity"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"></activity>
<service android:name=".RathuMakara"
android:label="Rathu Makara"
android:process=":remote"/>
</application>

</manifest>

最佳答案

系统正在终止您的服务以释放资源。要使您的应用程序具有“更高优先级”,请使用 foreground service相反。

关于java - 尽管我将媒体播放器实现为服务,但服务在后台几秒钟后关闭(媒体播放器)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52795985/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com