user2746059
user2746059

Reputation: 63

Getting weird errors while using media player

I'm new to Java and my friend helped me to write this code. I'm getting errors on because I think I'm using the wrong syntax. I've added comments to the code, where I'm having problems. I'm not even sure if I should be putting this code inside or outside of oncreate. Can anyone please point me to the directions for a good Java learning resource also.

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class MainActivity extends ActionBarActivity {
public MediaPlayer mediaPlayer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    /*super.onCreate(savedInstanceState);*/
    setContentView(R.layout.activity_main);
    getActionBar().hide();
    mediaPlayer = new MediaPlayer();  //getting an error on this line for invalid syntax.

     //not sure what to put here
        playB() throws IOException {
            playSound(R.raw.b)
        } 

    //not sure what to put here
         playSound(int resId) throws IOException {
             URI uri = URI.parse("android://com.max.bead/" + resId);

             if(mediaPlayer.isPlaying())
             {
                 mediaPlayer.stop();
             }
             else
             {
                 mediaPlayer.setDataSource(getApplicationContext(), uri);
                 mediaPlayer.prepare();
                 mediaPlayer.play();
             }
         }
}

Upvotes: 0

Views: 44

Answers (2)

David Galstyan
David Galstyan

Reputation: 71

Try this.

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        /*super.onCreate(savedInstanceState);*/
        setContentView(R.layout.activity_main);
        getActionBar().hide();
        mediaPlayer = new MediaPlayer();  //getting an error on this line for invalid syntax.
    }
         //not sure what to put here
            private void playB() throws IOException {
                playSound(R.raw.b);
            } 

        //not sure what to put here
            private void playSound(int resId) throws IOException {
                 URI uri = URI.parse("android://com.max.bead/" + resId);

                 if(mediaPlayer.isPlaying())
                 {
                     mediaPlayer.stop();
                 }
                 else
                 {
                     mediaPlayer.setDataSource(getApplicationContext(), uri);
                     mediaPlayer.prepare();
                     mediaPlayer.play();
                 }
            }

Upvotes: 1

Meenaxi
Meenaxi

Reputation: 567

try to set OnPreparedListener on media player

mediaPlayer.setOnPreparedListener(new OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { mp.start(); } });

Upvotes: 0

Related Questions