Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7828189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:14:58+00:00 2026-06-02T10:14:58+00:00

I am trying to play an mp3 sound using the MediaPlayer class inside a

  • 0

I am trying to play an mp3 sound using the MediaPlayer class inside a regular class, which means non Activity. My problem is probably initializing the MediaPlayer object since i have no Context in a non Activity class. How is this solvable?

Thanks!

EDIT

Here is some code from where the function should be called:

public class Bullet extends Thread{
public int bx = SniperActivity.bulletx;
public int by = SniperActivity.bullety;
Player obj = new Player();
SniperActivity con = new SniperActivity();


   public void shoot(){

    if (bx == Place.spotx && by == Place.spoty)
    {
            for (int f = 0; f<Rifle.dam; f++)
    {
                Player.life--;
                if(Player.life<=0){
                obj.death();
    }
    try {
        Bullet.sleep(Rifle.rate);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
           }
    }
    int s = 0;
    s++;
    while (s == Rifle.magazine)
    {
        try {
            Bullet.sleep(Rifle.relode);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        s = 0;
    }
    con.Mplayer.start();
   }
   }

The function is called right up here, in the “con.Mplayer.start()” line, from the shoot() function. the con Object is reffering to an Activity that has the Mplayer initialized in it is trying to start it from here.

Here is the activity code:

public class SniperActivity extends Activity{


    private GameView game;
    private Player player;
    private Scop scope;
    public static int scopx;
    public static int scopy;
    public static int bulletx;
    public static int bullety;
    public static String filename = "SharedData";
    static SharedPreferences someData;
    public static int Width;
    public static int Height;
    public MediaPlayer Mplayer;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    Mplayer = MediaPlayer.create(SniperActivity.this, R.raw.shot);

    someData = getSharedPreferences(filename, 0); 
    Height = someData.getInt("height", 480);
    Width = someData.getInt("width", 480);

    scopx = 1;
    scopy = 1;

    PlayerView playerview;
    playerview = new PlayerView(this, player);
    ScopView Scope;
    Scope = new ScopView(this, scope);

    this.game = new GameView(this);
    game.setviews(playerview, Scope);
    setContentView(game);
    int screenWidth,screenHeight;
    screenWidth = this.getWindowManager().getDefaultDisplay().getWidth();
    screenHeight = this.getWindowManager().getDefaultDisplay().getHeight();
    bulletx = screenWidth/2;
    bullety = screenHeight/2;
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
    float dx;
    Bullet bullet = new Bullet();
    //get the x coordinate of users' press
    dx = event.getX();
     if (dx >=0)
     {
         bullet.shoot();
     }
    return true;
}

public void onDeath(){
    finish();
}


}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T10:15:00+00:00Added an answer on June 2, 2026 at 10:15 am

    Try to pass Context in constructor of simple java file

    PlayMp3 mp3 = new PlayMp3(getApplicatonContext());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to play 6 seperate .mp3 sound files using instances of AVAudioPlayer.
I am trying to play audio buffered sound (.wav) using AudioTrack. Please see the
I am trying to play and audio file using DirectX dll. Audio.FromFile(Message 1.mp3).Play(); But
I am trying to create a soundboard program which will play sounds using the
I am trying to play dynamically few mp3 sounds using AVAudioPlayer. The following code
I'm trying to play a specific portion of a mp3 file using MediaElement. I
I'm trying to play an mp3 audio file on iPhone executing the following code:
I am trying to play audio file using jPlayer in iPad. It works fine
I'm trying to play a sound file from an iPhone program. Here's the code:
I'm trying to use fmodex 4.30.03 to play an MP3 file under Mono/Ubuntu. My

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.