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

时间:2018-10-13 18:23:51

标签: java android android-service

我正在制作一个从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));
        }

    }
}

这是Service类文件,它是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>

1 个答案:

答案 0 :(得分:1)

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

相关问题