Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jul 2012
    Posts
    70
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Java Not Playing Songs Within A function?

    I am fairly new to java Ive only started yesterday I do have programming background with PHP! C++,HTML,CSS and Java Script.

    Anyway I have written this out for an android development application and its not working, I want it to play a song it does it when Its inserted up the top of the page but not in a certain bracket, More info in in //Commenting in tne code

    PHP Code:
    package com.mycompany.myapp2;

    import android.app.Activity;
    import android.*;
    import android.os.*;
    import android.view.*;
    import android.widget.*;
    import android.widget.Button;
    import android.widget.TextView;
    import android.media.*;
    import android.media.MediaPlayer;

    public class 
    MainActivity extends Activity 
        
    //** called when the activity is first created 
        
    int counter;
        
    Button add,sub;
        
    TextView display;
        


        @
    Override


        
    public void onCreate(Bundle savedInstanceState


        { 
            
    super.onCreate (savedInstanceState);

            
    setContentView(R.layout.main); 
            
    counter 0;
            
    add = (ButtonfindViewById(R.id.bAdd);
            
    sub = (ButtonfindViewById(R.id.bSub); 
            
    display = (TextViewfindViewById(R.id.tvDisplay);
           
    it plays here?
        
    MediaPlayer mp3 MediaPlayer.create(thisR.raw.son);
        
    mp3.start();
        
            
    add.setOnClickListener(new View.OnClickListener() {

                    public 
    void onClick(View v) {
                        
                        
    //ERROR BELLOW Wont excute here 
                        
    MediaPlayer mp3 MediaPlayer.create(thisR.raw.son); 
                        
                        
    // Make the button do something 
                            
                        
                            
                        
    counter ++;
                        
    display.setText(" " counter); 
                        
                        


                    }



                });


            
    sub.setOnClickListener(new View.OnClickListener() {

                    public 
    void onClick(View v) {

                        
    // Take data away
                        
    counter --;
                        
    display.setText(" " counter);


                    }



                });





        }






  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    I haven't a clue about android development, but these do stand out here:
    Code:
            display = (TextView) findViewById(R.id.tvDisplay);
           it plays here?
        MediaPlayer mp3 = MediaPlayer.create(this, R.raw.son);
        mp3.start();
    
    // and here:
                        MediaPlayer mp3 = MediaPlayer.create(this, R.raw.son);
    'this' has changed its scope. The first one's 'this' refers to the instance of Activity. The second's 'this' refers to the instance created by new View.OnClickListener. Perhaps that will be your issue?
    If so, you can apply the outer's 'this' by creating a final variable and referring to it in the inner class:
    PHP Code:
    final Activity thisActivity this;
    //...
                        
    MediaPlayer mp3 MediaPlayer.create(thisActivityR.raw.son); 
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •