0

I am a completely begginer guy in Android Studio and mobile apps in general. I used to create websites on Wordpress and i don't have so much experience on coding. Recently i started experimenting with mobile apps and mostly Android Studio. I bought a template for a Logo Quiz game and i managed to make it run without errors and publish it in Play Store as my first game. The player can see a part of the logo and guess the brand name using the given letters

But i would like to use the same template with new graphics and create a music quiz.

Instead of the logo guess game, the player will be able to listen a part of a song and guess the song's title.

The current project is getting the file names from a database stored in assets/databases folder.

So i managed to add start, pause and stop buttons in my activity_play.xml and succesfully created a mediaplayer in activityPlay.java file like:

public void music(View view) {
        switch (view.getId()){
            case R.id.button:
                // Check if mediaPlayer is null. If true, we'll instantiate the MediaPlayer object
                if(mediaPlayer == null){
                    mediaPlayer = MediaPlayer.create(this, R.raw.music);
                }
                // Then, register OnCompletionListener that calls a user supplied callback method onCompletion() when
                // looping mode was set to false to indicate playback is completed.
                mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mediaPlayer) {
                        // Here, call a method to release the MediaPlayer object and to set it to null.
                        stopMusic();
                    }
                });
                // Next, call start() method on mediaPlayer to start playing the music.
                mediaPlayer.start();
                break;
            case R.id.button2:
                if(mediaPlayer != null) {
                    // Here, call pause() method on mediaPlayer to pause the music.
                    mediaPlayer.pause();
                }
                break;
            case R.id.button3:
                if(mediaPlayer != null){
                    // Here, call stop() method on mediaPlayer to stop the music.
                    mediaPlayer.stop();
                    // Call stopMusic() method
                    stopMusic();
                }
                break;
        }
    }

    private void stopMusic() {
        mediaPlayer.release();
        mediaPlayer = null;
    }

    // Call stopMusic() in onStop() overridden method as well.
    @Override
    protected void onStop() {
        super.onStop();
        stopMusic();
    }

The above code can succesfully play the music.mp3 file located in raw folder. The app i bought is using the following code to load the images and display them for each level:

        String image_a = listDataBase.get(1);
        String image_q = listDataBase.get(2);


        if (isTrue == 1) {
            String imgPath;
            if (numImage == 0) {
                imgPath = "file:///android_asset/logos/" + image_a;
            } else {
                imgPath = "file:///android_asset/logos/" + image_q;
            }
            Picasso.get().load(imgPath).into(imageView);
            linearLayoutNullClick.setVisibility(View.VISIBLE);
            recyclerViewKeys.setVisibility(View.GONE);
            trueLogo = 2;
        } else {
            String imgPath = "file:///android_asset/logos/" + image_q;
            Picasso.get().load(imgPath).into(imageView);
            recyclerViewKeys.setVisibility(View.VISIBLE);
            recyclerViewLogoKeys.setVisibility(View.VISIBLE);
        }

So is it possible to use the same code and load the imgPath into mediaPlayer = MediaPlayer.create(this, R.raw.music);

I tried loading imgPath directly to mediaplayer like this but didn't work:

mediaPlayer = MediaPlayer.create(this, imgPath); 

Then i tried:

private String audioPath;
audioPath = imgPath;
mediaPlayer = MediaPlayer.create(this, audioPath); 

but also didn't work. Tried many more methods i found on the web, but always i am missing something

As i said before i am a newbie in coding and programming so the solution probably will be very easy . Anyone can help please?

Nikos
  • 1
  • public static MediaPlayer create(android.content.Context context, int resid). As you can see, the second parameter is an integer, where you try to pass a string. I don't think the Uri option is viable in your case (public static MediaPlayer create(android.content.Context context, Uri uri))? I never used the latter, so I'm just guessing. – Danny E.K. van der Kolk Apr 02 '22 at 23:11
  • Secondly... you use imgPath first as the second parameter, then, when that does not work, you copy imgPath into a second string, and try to use that one as the second parameter. I suggest you take a basic course in programming first :) – Danny E.K. van der Kolk Mar 06 '23 at 22:37

2 Answers2

0

Are you sure you are getting a audio path in your imgPath object?

Then you can try getting actual storage path from device using code mentioned here

Ruchi
  • 71
  • 3
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 11 '22 at 22:57
0

If you change the value in the database of pic_1.png with audio_1.mp3 then path will start like:

"file:///android_asset/logos/" + YOUR_AUDIO;

So are you sure audio exists in this path?

Ruchi
  • 71
  • 3