JuzerD JuzerD - 1 month ago 15
Android Question

error while creating simple media player app

i am getting red line error message on 'context' and 'raw' in this line MediaPlayer.create(context,R.raw.music); while creating a simple media player app (music is in /res/raw folder).
Pls help..
Here's the full code..




package com.example.mymedia;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


setContentView(R.layout.activity_main);
MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.music);
mediaPlayer.start(); // no need to call prepare(); create() does that for you


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


}

Answer

Try this..

 @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main);
    MediaPlayer mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.music);
    mediaPlayer.start(); // no need to call prepare(); create() does that for you
    }

And i hope you know means of raw.. A raw folder holds files of any type. You need a raw folder under your resources folder (res). In your example, jam is expected as a resource in the raw folder, and will likely be a type of music file as you're using MediaPlayer to try and read it.

You've this error raw cannot be resolved because raw folder doesn't exist, and so the variable raw in class R is not being auto-generated.

Fix the problem by creating the raw folder.

If you already have created res/raw try cleaning the project. Sometimes Eclipse gets confused. If that does work, make a small change to a source file, and save it so the auto-build process kicks off. Sometimes cleaning manually hasn't fixed the problem for me, its a known bug for Eclipse.