Jump to content
Android Forum - A Community For Android Users and Enthusiasts

Problem on activity


murnesty
 Share

Recommended Posts

Hi,

 

I'm facing problem extends class. From the tutorial on book "Practical Android 4 Games Development", it asking me to make a background music by using android.app.Service and android.media.MediaPlayers.

 

I had created a class and extends to Service, but unfortunately I need to make this class as an activity. So I need to extends this class to Activity also. The problem is java unable to extends more than 1 class. Is there anyway to solve this problem?

 

here is the code with for the class

package com.proandroidgames;
import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
public class SFMusic extends Service
{
public static boolean isRunning = false;
MediaPlayer player;

@Override
public IBinder onBind(Intent arg0)
{
 return null;
}
@Override
public void onCreate()
{
 super.onCreate();

 setMusicOptions(this,
  SFEngine.LOOP_BACKGROUND_MUSIC,
  SFEngine.R_VOLUME,
  SFEngine.L_VOLUME,
  SFEngine.SPLASH_SCREEN_MUSIC);
}
public void setMusicOptions(Context context, boolean isLooped,
    int rVolume, int lVolume, int soundFile)
{
 player = MediaPlayer.create(context, soundFile);
 player.setLooping(isLooped);
 player.setVolume (rVolume,lVolume);
}
public int onStartCommand(Intent intent, int flags, int startId)
{
 try
 {
  player.start();
  isRunning = true;  
 }
 catch(Exception e)
 {
  isRunning = false;
  player.stop();
 }
 return 1;
}
public void onStart(Intent intent, int startId)
{

}
public void onStop()
{
 isRunning = false;
}
public IBinder onUnBind(Intent arg0)
{
 //TODO Auto-generated method stub
 return null;
}
public void onPause()
{

}
@Override
public void onDestroy()
{
 player.stop();
 player.release();
}
@Override
public void onLowMemory()
{
 player.stop();
}
}

 

Thanks.

Murnesty

Link to comment
Share on other sites

  • 11 months later...

Hi there,

 

I'm currently using this book to build and engine for a 2d danmaku, but I constantly run into problems too, here you can find the source code for reference, the book skips on subjects and uses functions that are deprecated and this causes your problems:

 

source: https://www.apress.com/9781430240297

 

the awnser to your problem is adding this to the androindmanifest.xml file:

- or you can add the service in the GUI like the SFGame.java class.

 

The game will not compile hower since the book didnt tell you to instantiate the SFRenderer class in the SGameView class

 

here how it should be:

 

package com.proandroidgames;

 

import android.content.Context;

import android.opengl.GLSurfaceView;

 

public class SFGameView extends GLSurfaceView {

private SFGameRenderer renderer;

 

public SFGameView(Context context) {

super(context);

 

renderer = new SFGameRenderer();

 

this.setRenderer(renderer);

}

}

 

theres more problems ahead with deprecated functions so keep the development documentation open for reference, you should have it available in your sdk manager.

 

Shame this book misses out, but troubleshooting is learning too. I hope u didnt pay for it ;)

Link to comment
Share on other sites

  • 6 months later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...