CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Java and JSP (http://www.codingforums.com/forumdisplay.php?f=54)
-   -   Java Not Playing Songs Within A function? (http://www.codingforums.com/showthread.php?t=286239)

Spudster 01-22-2013 10:29 AM

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);


                }



            });





    }







Fou-Lu 01-22-2013 03:07 PM

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); 



All times are GMT +1. The time now is 07:37 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.