Android Mediaplayer - java

i want to play an audio on my android app using the Mediaplayer class.
my problem is on the R.java part.
to better understand my problem, i'll have to show a part of my code
audioControl = MediaPlayer.create(context, R.raw.forward_100hz);
audioControl.start();
so, the problem is on the forward_100hz, which is my wav file which is stated that it cannot be resolved or it is not a field.
how can i resolve this problem?

I think the problem is with your imports. Probably you've already imported the android.R class, but not the R class of your project.

Make sure the file is stored in the folder res/raw
Try deleting the R file generated by eclipse if the file is in place

Make sure the file forward_100hz is in res/raw folder
Check your imports and remove import android.R;
If on Eclipse press Ctrl + Shift + O to auto suggest import and you should import
import your.package.name.R;
If 3 does not work check your manifest file and then res directory for error which might be causing the problem by not generating R.java

As the second parameter of the create method put the following path:
Uri path = Uri.parse("android.resource://<package-name>/"+ R.raw.forward_100hz);

First sure that folder/raw exists and create the Mediaplayer before of on create
MediaPlayer player;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
player=MediaPlayer.create(MainActivity.this,R.raw.forward_100hz);
player.start();
}
}

Related

Cannot resolve symbols getIntent(), findViewById, etc

Cannot resolve symbol findViewById(), getApplicationContext(), and many more basic functions, maybe I did something wrong with the Resources file, but I don't know what to do now to correct.
package com.example.apple.onlinesql;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class Answer extends AppCompatActivity {
TextView questionTextView, answerTextView;
String question, answer;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_answer);
Bundle bundle = getIntent().getExtras();
question = bundle.getString("question");
answer = bundle.getString("answer");
/*cannot resolve symbol findViewById()*/
questionTextView = (TextView) findViewById(R.id.questionTextView);
answerTextView = (TextView) findViewById(R.id.answerTextView);
questionTextView.setText(question);
answerTextView.setText(answer);
}
}
The application hasn't build successfully, as #vasilis mentioned try to clean and rebuild your project. And if that not solve the case, restart your studio and in worst case restart you system.
You appear to be running into the corrupt cache bug in Android Studio.
To fix: File > Invalidate Caches / Restart
I got the way although not very efficient but I got the way. I copied my entire project to a different location and opened it from there. MAGIC it worked for me.

Android Studio MainActivity.java can't find resources

I'm basically writing my first hello world in android studio and the java file says that the xml layout file and other resources in the res file don't exist. I took the references to these things from books/tutorials so they should work.
The book said to use
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main.xml);
}
But when that didn't work I changed it to res.layout.activity_main.xml
my project directory looks like this
How do I make it work?
http://i.stack.imgur.com/N71sl.png
//EDIT
http://i.stack.imgur.com/HUgsm.png
You are referencing res not R.
do something like this: setContentView( R.layout.activity_main);
Leave off the ".xml"
update your android studio in stable channel and restart android studio and build the project again .
You need to understand the concept of the R class in android.
The R class is auto generated every time you build your project and cannot be modified.
This class contains a map of all the projects assets. Every time you created or import a resource, set a dimension, a string, create a layout, add or change id etc. the R file is changed in the background.
So if you want to access a resource you simply call it using the R class.
For example:
layout: R.layout.activity_main
string: R.string.hello_world
id: R.id.et_main_helloWorld
And so on.
More info can be found here.
Make sure you also check Providing Resources and Accessing Resources for a better understanding.
Good luck and happy coding.
In the end I started a new project and added in everything piece by piece, I had put a mp3 file in the values directory which was messing up the R file.

Manually registering a new activity in manifest.xml

tl;dr Look at title + netbeans
I've been writing a game as I have been learning both Java and Android*. Most of it has gone without a hitch- but here we go.
To my understanding- each screen (main menu, gameplay, highscores, ect) is a separate Activity and therefore needs a different layout.
Problem 1:
EDIT: Solved. Thanks Daniel.
Problem 2: Then in the GameActivity.java file there is an error of cannot find symbol (symbol main_1 location class layout). Help?
package lolfighter.notriot;
import android.app.Activity;
import android.os.Bundle;
import lolfighter.notriot.nojoke.R.*;
// #author DEVELOPMENT
public class GameActivity extends Activity {
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(layout.main_1);
}
Also why is the bundle icicle? I have not specified this anywhere.
Edit- Not solved
*- I am in a partnership of sorts with a friend- the original plan was that he would handle all the code and I would handle all the graphic resources (He took an actual class for the Java language) But he has ran into a roadblock. I can't ask him because he has limited internet access (lunch breaks only) and lives in a different state (we go to college together)
Problem 2: then in the GameActivity.java file there is an error of cannot find symbol (symbol main_1 location class layout). Help?
Change layout.main_1 to R.layout.main_1.
Also why is the bundle icicle? I have not specified this anywhere.
You can rename the variable to be whatever you want, it doesn't matter.

raw cannot be resolved or is not a field error (Eclipse/Android/Java)

For some odd reason I'm getting this error: raw cannot be resolved or is not a field
I'm trying to do the following
MediaPlayer click = MediaPlayer.create(this, R.raw.button_click.mp3);
in my onCreate method.
I've checked the folder & mp3 file, and it's <project-name>/raw/button_click.mp3. I've tried:
Cleaning up my project
Restarting Eclipse
Making it R.raw.button_click
Checking if everything's lowercased & spelled correctly
Help, please!
Shouldn't it be in projectname/res/raw, instead of projectname/raw?

Problem playing audio on android

Amendment: This appears to be a problem with the particular audio file I was using. Other applications on the droid such as the Astro file manager also fail to play it, and if I replace it in the package with an AAC file, it plays it without error. I encoded the problematic audio file to MP3 format from a WAV file, using LAME on ubuntu. It would be good to know the limitations of the android media player. mplayer seems to have no problem playing the file on ubuntu. I suppose I should submit a bug report.
Original question: The following code crashes when I try to play it on my droid. The error message given in the log is "Command PLAYER_INIT completed with an error or info PVMFErrNoResources." Then an IOException is raised on the mp.prepare() line. There is a file res/raw/bell.mp3 in my project directory, which I assume corresponds to R.raw.bell in the code below. I am building with "ant debug". In case it's relevant, when I created the project directory with "android create", I set the target number to 4, corresponding in my system to "android 2.0."
What am I doing wrong, here?
import java.io.IOException;
import android.app.Activity;
import android.os.Bundle;
import android.media.MediaPlayer;
import android.util.Log;
public class testapp extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
try {
MediaPlayer mp = MediaPlayer.create(this, R.raw.bell);
mp.prepare();
mp.start();
} catch (IOException e) {
Log.v(getString(R.string.app_name), e.getMessage());
}
}
}
You do not need to call prepare() if you use the static create() method to get the MediaPlayer. It does the prepare() step for you. You only need to call prepare() if you either use the regular MediaPlayer constructor or if you are trying to reset the clip back to the beginning to play back from the existing MediaPlayer object.
Here is a sample project for playing back sounds (in my case, an Ogg clip).

Categories

Resources