背景音乐的Andr​​oid背景音乐、Andr、oid

2023-09-12 06:22:20 作者:继续颓废

欧凯,这是我的问题。我在那里香港专业教育学院成功地创造出媒体播放器播放的音乐在后台所有的时间一个服务类。这里是code:

 包com.test.brzoracunanje;
进口android.app.Service;
进口android.content.Intent;
进口android.media.MediaPlayer;
进口android.os.IBinder;

公共类BackgroundSoundService延伸服务{
私有静态最后字符串变量= NULL;
MediaPlayer的播放器;
公众的IBinder onBind(意向为arg0){

    返回null;
}
@覆盖
公共无效的onCreate(){
    // TODO自动生成方法存根
    super.onCreate();
       球员= MediaPlayer.create(这一点,R.raw.test_cbr);
        player.setLooping(真正的); //设置循环
        player.setVolume(100,100);
        player.start();
}
@覆盖
公共无效ONSTART(意向意图,诠释startId){
    super.onStart(意向,startId);
}
公共无效的onDestroy(){
    // TODO自动生成方法存根
    super.onDestroy();
}

保护无效onNewIntent(){
    player.pause();
}
}
 

但现在我有问题,当我点击首页返回按钮。它仍然播放音乐。有谁知道如何解决这个问题呢?

这里是code我如何调用这个类的服务,我想播放音乐;

 意图SVC =新的意图(这一点,BackgroundSoundService.class);
    startService(SVC);
 

解决方案

如果你想播放背景音乐为您的应用程序而已,然后发挥它从你的应用程序/使用的AsyncTask类来为你做它启动一个线程。

服务的概念是在后台运行;通过背景,意义,通常是当你的应用程序用户界面是不可见。诚然,它可以用来就像你(如果你还记得停止它),但它只是不正确的,而且它消耗你不应该使用的资源。

如果你想peform任务在您的活动的背景下,使用AsyncTask的。

顺便说一句, ONSTART 是pcated德$ P $。当你使用的服务,实施 onStartCommand

更新:

我觉得这$​​ C $ C会为你工作。添加这个类(在您的活动类附后)。

 公共类BackgroundSound扩展的AsyncTask<虚空,虚空,虚空> {

    @覆盖
    保护无效doInBackground(虚空...... PARAMS){
        的MediaPlayer播放器= MediaPlayer.create(YourActivity.this,R.raw.test_cbr);
        player.setLooping(真正的); //设置循环
        player.setVolume(1.0F,1.0F);
        player.start();

        返回null;
    }

}
 

现在,为了控制音乐,保存annonymously创造它的你BackgroundSound对象来代替。声明它作为一个字段的活动:

  BackgroundSound mBackgroundSound =新BackgroundSound();
 

在您的活动的onResume方法,启动:

 公共无效onResume(){
    super.onResume();
    mBackgroundSound.execute(空);
}
 

和您的活动的的onPause方法,阻止它:

 公共无效的onPause(){
    super.onPause();
    mBackgroundSound.cancel(真正的);
}
 

这会工作。

Okey, this is my problem. I have one service class where Ive managed to create media player to play music in background all time. Here is code:

package com.test.brzoracunanje;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;

public class BackgroundSoundService extends Service {
private static final String TAG = null;
MediaPlayer player;
public IBinder onBind(Intent arg0) {

    return null;
}
@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
       player = MediaPlayer.create(this, R.raw.test_cbr);
        player.setLooping(true); // Set looping
        player.setVolume(100,100);
        player.start();
}
@Override
public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);
}
public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
}

protected void onNewIntent() {
    player.pause();
}
}

But now I have problem when I click on HOME, or BACK button. It still plays music. Does anyone knows how to solve that problem?

And here is code how i call this service on class where I want to play music;

  Intent svc=new Intent(this, BackgroundSoundService.class);
    startService(svc);

解决方案

If you want to play background music for your app only, then play it in a thread launched from your app/use AsyncTask class to do it for you.

The concept of services is to run in the background; By background, the meaning is usually when your app UI is NOT VISIBLE. True, it can be used just like you have (If you remember to stop it) but its just not right, and it consumes resources you shouldn't be using.

If you want to peform tasks on the background of your activity, use AsyncTask.

By the way, onStart is deprecated. When you do use services, implement onStartCommand.

UPDATE:

I think this code will work for you. Add this class (Enclosed in your activity class).

public class BackgroundSound extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {
        MediaPlayer player = MediaPlayer.create(YourActivity.this, R.raw.test_cbr); 
        player.setLooping(true); // Set looping 
        player.setVolume(1.0f, 1.0f); 
        player.start(); 

        return null;
    }

}

Now, in order to control the music, save your BackgroundSound object instead of creating it annonymously. Declare it as a field in your activity:

BackgroundSound mBackgroundSound = new BackgroundSound();

On your activity's onResume method, start it:

public void onResume() {
    super.onResume();
    mBackgroundSound.execute(null);
}

And on your activity's onPause method, stop it:

public void onPause() {
    super.onPause();
    mBackgroundSound.cancel(true);
}

This will work.