扩展服务的类的成员

时间:2014-05-16 05:25:59

标签: android service android-mediaplayer foreground

我的应用有3项活动(MainPageActivityFavoritesActivitySettingsActivity)和一项服务(StreamService)。

MainPageActivityFavoritesActivity都有两个停靠在底部的按钮:一个用于播放/停止一个流(互联网收音机),另一个用于收听当前歌曲。当各个活动处理音乐控件时,这两种方法都运行良好,但我决定将它们移到Service扩展的类中,因为它是音乐播放器的常用做法。 以下是一些代码段:

MainPageActivity:

public void play (View v)
{
    Intent intent = new Intent(MainPageActivity.this, EdenService.class);
    if (!service.isPlaying())
    {
        startService(intent);
        btnPlay.setText("Stop");
    }
    else
    {
        stopService(intent);
        btnPlay.setText("Play");
    }
}

public void fave (View v)
{
    try
    {
        String currentSong = txtCurrentSong.getText().toString();
        if (!db.alreadyFavorited(currentSong))
        {
            if (!currentSong.isEmpty() || !currentSong.equals("Unavailable"))
            {
                db.addAnonymousFavorite(currentSong);
                Toast.makeText(getApplicationContext(), "Song has been added to favorites.", Toast.LENGTH_LONG).show();
                btnFave.setText("Unfave");
            }
            else
                Toast.makeText(getApplicationContext(), "Song title cannot be empty.", Toast.LENGTH_LONG).show();
        }
        else
        {
            try
            {
                db.removeAnonymousFavorite(currentSong);
                Toast.makeText(getApplicationContext(), "Song has been removed from favorites.", Toast.LENGTH_LONG).show();
                btnFave.setText("Fave");
            }
            catch (Exception e)
            {
                Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
            }
        }
    }
    catch (Exception e)
    {
        Toast.makeText(getApplicationContext(), "Could not add song to favorites.", Toast.LENGTH_LONG).show();
        Log.e("playButton", Log.getStackTraceString(e));
    }
}

//used in onResume for navigation within the app
    private void setPlayButtonText()
    {
        if (!service.isPlaying())
            btnPlay.setText("Play");
        else
            btnPlay.setText("Stop");
    }

private void setFaveButtonText()
{
    runnable = new Runnable() {
        @Override
        public void run() {
            final String currentSong = ServiceHandler.getCurrentSong();
            if (!Settings.isAuthenticated(getApplicationContext()))
            {
                if (!db.alreadyFavorited(currentSong))
                    btnFave.setText("Fave");
                else
                    btnFave.setText("Unfave");
                handler.postDelayed(this, 3000);
            }
        }
    };
runnable.run();
}

FavoritesActivity具有相同的代码。

StreamService:

public StreamService()
{
    this.mp = new MediaPlayer();
    isPlaying = false;
}

@Override
public void onCreate ()
{
    try
    {
        mc = new MediaController(this);
        mp.setDataSource(streamLink);
        mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mc.setMediaPlayer(this);
        mp.prepareAsync();
        Log.d(TAG + ".onCreate()", Boolean.toString(isPlaying));
    }
    catch (Exception e)
    {
        Log.e(TAG, Log.getStackTraceString(e));
        Toast.makeText(getApplicationContext(), "Could not connect to stream.", Toast.LENGTH_LONG).show();
    }
}

@Override
public void onDestroy()
{
    pause();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
    isPlaying = true;
    new Thread(new Runnable()
    {
        @Override
        public void run() {
            start();
        }
    }).start();

    return Service.START_STICKY;
}

@Override
public void start() {
    try
    {
        mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            public void onPrepared(MediaPlayer mp) {
                mp.start();
            }
        });
        mp.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mediaPlayer, int what, int extra) {
                String errorText = "";
                switch (what) {
                    //what = 1
                    case MediaPlayer.MEDIA_ERROR_UNKNOWN:
                        errorText += "Error has occured: ";
                        break;
                    //what = 100
                    case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
                        errorText += "Server has died. ";
                        break;
                }

                switch (extra) {
                    //extra = -1004
                    case MediaPlayer.MEDIA_ERROR_IO:
                        errorText += "Please check your internet connection.";
                        break;
                    //extra = -110
                    case MediaPlayer.MEDIA_ERROR_TIMED_OUT:
                        errorText += "Connection has timed out.";
                        break;
                }
                Toast.makeText(getApplicationContext(), errorText, Toast.LENGTH_LONG).show();
                return true;
            }
        });

        Intent intent = new Intent(this, MainPageActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);

        Notification notification = new NotificationCompat.Builder(getApplicationContext())
                .setContentTitle("Eden Radio")
                .setContentText(ServiceHandler.getCurrentSong())
                .setSmallIcon(R.drawable.ic_stat_av_play)
                .setContentIntent(pi).build();
        startForeground(CLASS_ID, notification);

    }
    catch (Exception e)
    {
        Log.e(TAG, e.getMessage());
        Toast.makeText(getApplicationContext(), "Could not connect to stream.", Toast.LENGTH_LONG).show();
    }
}

@Override
public void pause() {
    mp.stop();
    stopForeground(true);
    isPlaying = false;
}

    @Override
public boolean isPlaying() {
    //mp.isPlaying() always returns false for some reason
    return isPlaying; //and so does this
}

现在,我的问题是:service.isPlaying()总是返回false,所以当我按下播放音乐开始时,我就永远无法停止音乐。为什么会这样?我已经尝试从服务中制作一个单身人士(并且在尝试Service本身就是单身时发现了,但我还是不确定虽然有效,但我已尝试制作isPlaying字段及其相应的方法isPlaying() static,但无济于事。通过播放流时发出的通知,通过其他活动以及通过销毁和重新启动应用程序来访问MainPageActivity只会导致播放按钮重新初始化为播放"播放",永远无法停止流。同样的事情发生在FavoritesActivity

我花了太多时间试图找出它为什么永远不会返回正确的价值。如果你能帮助我,我将非常感激。

提前致谢。

1 个答案:

答案 0 :(得分:0)

将该服务绑定到onResume()中的活动。这将创建一次服务,然后允许您与它进行交互。您可以在这里找到一些关于如何绑定服务并与之交互的文档:

Bound Services