JuzerD
JuzerD

Reputation: 5

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

}

Upvotes: 0

Views: 1597

Answers (3)

ankush dubey
ankush dubey

Reputation: 11

This error can be resolved simply by 1. Creating a new 'raw' folder of 'raw' type under the 'res' folder i.e. The Android Repository.(Just right click over res>>new>>raw) 2. Further a file is needed under the folder in order to resolve the error completely. (TheRedLine goes after inserting the file) This file can be .mp3 (or according to your program)

Upvotes: 0

srishesh prabhu
srishesh prabhu

Reputation: 11

I had seen a tutorial which says you have to create the raw directory. the following might help

  1. Right click on your resouce directory and click on create a new resource directory and name the directory as raw.
  2. select the raw option in the next drop down.
  3. click on ok.
  4. proceeding further just copy the file you want to play in the raw folder.

Upvotes: 1

Brijesh Patel
Brijesh Patel

Reputation: 676

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.

Upvotes: 0

Related Questions