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 3940988
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:29:11+00:00 2026-05-20T00:29:11+00:00

I’m in the process of writing my first android app, a simple media player

  • 0

I’m in the process of writing my first android app, a simple media player just to practice my semi-decent java/mark-up skills.
The app will have the now-playing screen as the “main” page along with a song browser and pop-up music controls. I have done a bit of work in both the former and latter but I’m having a problem with MediaPlayer. If I start a song playing, it will play and pause fine until I exit the music controls dialog and then re-enter it. At this point it seems to create a second instance of the MediaPlayer object.

Main Player Screen

package org.practicesession.zoomplayer;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;

public class zoomPlayer extends Activity implements OnClickListener {
    static MediaPlayer myPlayer;
    private static int stateMediaPlayer;

    /** Called when the activity is first created. */ 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        View returnButton = findViewById(R.id.button_return);
        returnButton.setOnClickListener(this);
        View playerControls = findViewById(R.id.album_art_panel);
        playerControls.setOnClickListener(this);
        View playlistView = findViewById(R.id.song_info_panel);
        playlistView.setOnClickListener(this);
        View shuffleButton = findViewById(R.id.player_shuffle_button);
        shuffleButton.setOnClickListener(this);
        View repeatButton = findViewById(R.id.player_repeat_button);
        repeatButton.setOnClickListener(this);
        View ratingButton = findViewById(R.id.player_rating_button);
        ratingButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
        case R.id.album_art_panel:
            Intent i = new Intent(this, PlayerControls.class);
            startActivity(i);
            break;
        }

    }

    public static void setStateMediaPlayer(int i){
        stateMediaPlayer = i;
    }

    public static int getStateMediaPlayer(){
        return stateMediaPlayer;
    }
}

Popup controls

package org.practicesession.zoomplayer;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

//Extends to implement the functionality of the main page
public class PlayerControls extends Activity implements OnClickListener{
    private final int stateMP_Playing = 1;
    private final int stateMP_Pausing = 2;


    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.playcontrols);

        View exitButton = findViewById(R.id.controls_exit_pane);
        exitButton.setOnClickListener(this);
        View volUpButton = findViewById(R.id.controls_vol_up);
        volUpButton.setOnClickListener(this);
        View playButton = findViewById(R.id.controls_play);
        playButton.setOnClickListener(this);
        View volDownButton = findViewById(R.id.controls_vol_down);
        volDownButton.setOnClickListener(this);

        musicPlayback();
    }
    public void musicPlayback(){
        zoomPlayer.myPlayer = new  MediaPlayer();
        zoomPlayer.myPlayer = MediaPlayer.create(this, R.raw.outta);
        zoomPlayer.setStateMediaPlayer(stateMP_Pausing);
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
        case R.id.controls_exit_pane:
            finish();
            break;
        case R.id.controls_play:
            switch(zoomPlayer.getStateMediaPlayer()){
                case stateMP_Playing:
                    zoomPlayer.myPlayer.pause();
                    zoomPlayer.setStateMediaPlayer(stateMP_Pausing);
                    break;
                case stateMP_Pausing:
                    zoomPlayer.myPlayer.start();
                    zoomPlayer.setStateMediaPlayer(stateMP_Playing);
                    break;
            }
            break;
        }
    }


}

It seems to be a normal java problem rather than anything else. I’ve tried set/get methods, static variables, removing inheritance etc. to no avail

Do note too that the code above is the result of fooling around with this for hours so it’s not as tidy as it can be…for example, the controls screen originally inherited from main screen as many of the imports and methods are shared. I removed this to see if it was causing the problem but no joy

Thanks for any help!

  • 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-05-20T00:29:12+00:00Added an answer on May 20, 2026 at 12:29 am

    If I start a song playing, it will play and pause fine until I exit the music controls dialog and then re-enter it. At this point it seems to create a second instance of the MediaPlayer object.

    That’s what your code tells Android to do. In Java, the new operator creates a new instance of a class. You are creating a new MediaPlayer every time onCreate() of a PlayerControls activity is called. That will occur every time you call startActivity() to launch a PlayerControls activity.

    Typically, in Android, a music player would be implemented via a Service that does the actual music playback. That way, the user is not tied to having any specific activity of yours around. The service would use startForeground() to make sure it is not shut down by Android, plus this puts an icon in the status bar to remind the user that your app is what’s causing the music (probably also giving them rapid access back to your activity).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
We're building an app, our first using Rails 3, and we're having to build
I have thousands of HTML files to process using Groovy/Java and I need to
I am writing an app with both english and french support. The app requests
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.