gpt4 book ai didi

按下主页/后退按钮后,Android 音乐播放器服务无法播放

转载 作者:行者123 更新时间:2023-11-29 21:19:14 25 4
gpt4 key购买 nike

我正在尝试在 android 中编写音乐播放器代码。我在服务中编写了 MediaPlayer 代码。代码如下:

package com.example.audioservice;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service {
static MediaPlayer mp;



@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
System.out.println("in MyService onCreate()");

mp=MediaPlayer.create(getApplicationContext(),R.raw.subanallah);

}

@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
System.out.println("in onService onBind()");
return null;
}



@Override
public int onStartCommand(Intent intent,int flags, int startId) {
super.onStart(intent, startId);
Log.d("log", "In onStart.");
System.out.println("in MyService onStartCommand()");

mp.start();
return Service.START_STICKY;
}

}

并从 Activity 调用服务

Intent s=new Intent(this,MyService.class);
startService(s);

在 list 文件中添加服务

<service android:name="com.example.audioservice.MyService"></service>

但是当按下主页/后退按钮时应用程序再次重新启动。我的要求是当按下主页/后退按钮时应用程序应该继续播放音乐。请帮助我。

最佳答案

编辑完整代码

逐步执行此操作::

创建 activity_main.xml 并粘贴

<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<Button
android:id="@+id/playId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/stopId"
android:layout_marginRight="53dp"
android:text="Play" />

<Button
android:id="@+id/stopId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="48dp"
android:layout_marginTop="50dp"
android:text="Stop" />

之后创建 MyService 类并粘贴它

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;

public class MyService extends Service {

MediaPlayer mp;
@Override
public IBinder onBind(Intent arg0) {

return null;

}

@Override
public void onCreate()
{
super.onCreate();
mp = MediaPlayer.create(getApplicationContext(), R.raw.ram3);

}

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
mp.start();
mp.setLooping(true);
return 0;

}

@Override
public void onDestroy()
{
mp.release();
super.onDestroy();

}

}

然后创建 MainActivity1 类并粘贴它

import android.os.Bundle;

导入android.app.Activity;导入 android.content.Intent;导入 android.view.View;导入 android.widget.Button;

public class MainActivity1 extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Button play, stop;

play = (Button) findViewById(R.id.playId);
stop = (Button) findViewById(R.id.stopId);
play.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

Intent service = new Intent(MainActivity1.this, MyService.class);

startService(service);

}

});

stop.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

Intent name = new Intent(MainActivity1.this, MyService.class);

stopService(name);

}

});

}

}

最后一步是粘贴以下代码AndroidManifest.XML

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity1"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<service
android:name=".MyService"
android:enabled="true" >
</service>
</application>

它必须有效,这一点毫无疑问。

关于按下主页/后退按钮后,Android 音乐播放器服务无法播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20947867/

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