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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T00:03:33+00:00 2026-05-21T00:03:33+00:00

I want to play a certain mp3 file when a text is clicked. For

  • 0

I want to play a certain mp3 file when a text is clicked. For example, I clicked the word "Nicholas", the app have to play nicholas.mp3.

Sorry for my messy code, I’m new to android dev:

package com.example.playword;

import java.io.IOException;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
//import android.os.Handler;
import android.view.View;
//import android.view.View.OnClickListener;
//import android.widget.Button;
import android.widget.TextView;

public class PlayWord extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //final Handler mHandler = new Handler();
         
        final TextView nicholas = (TextView) findViewById(R.id.nicholas);
        final TextView was = (TextView) findViewById(R.id.was);
        
        nicholas.setText("Nicholas ");
        was.setText("was ");        
        
        /*
        Button btn = (Button) (findViewById(R.id.nicholasBtn));
        
        btn.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                nicholas.setText("Nicholas (Clicked!) ");
            }

          });
        */
        
        View.OnClickListener handler = new View.OnClickListener(){
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.nicholas: // doStuff
                        
                        MediaPlayer mPlayer = MediaPlayer.create(null, R.raw.aaanicholas);
                        
                        try {
                            mPlayer.prepare();
                        } catch (IllegalStateException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        
                        mPlayer.start();
                        
                        nicholas.setText("Nicholas (Clicked!) ");
                        break;
                        
                    case R.id.was: // doStuff
                        
                        MediaPlayer mPlayer1 = MediaPlayer.create(null, R.raw.aaawas);
                        
                        try {
                            mPlayer1.prepare();
                        } catch (IllegalStateException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        
                        mPlayer1.start();
                        
                        was.setText("was (Clicked!) ");
                        break;
                }
            }
        };

        findViewById(R.id.nicholas).setOnClickListener(handler);
        findViewById(R.id.was).setOnClickListener(handler);
        
    }
}

When I run this, I’m getting a force close error. Do you have a much better idea on this?

  • 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-21T00:03:34+00:00Added an answer on May 21, 2026 at 12:03 am

    You need to pass in a context instance into MediaPlayer.create method:

    MediaPlayer mPlayer = MediaPlayer.create(PlayWorld.this, R.raw.aaanicholas);
    

    Also, after the create() call, prepare is already executed, so you don’t need to execute it explicitly, just invoke start() right after create().

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

Sidebar

Related Questions

I want to play a mp3 file in c#. The form should have a
I want to play a .wav file on a mobile device. I guess my
I want to play youtube video with a certain resolution, like 360p and capture
I use the Play Framework 2.0 (2.0.3). I have a Java project and want
I have an Android app that currently generates a HTML file as the output
I'll explain my scenario. I want to play a small sound at certain situation,
I want to show text one after the other on certain interval of time
I have a .y4m video file, and I want to convert it to .mp4
I am using a media player instance to play a music file.I want to
When a certain event occurs, I want my website to play a short notification

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.