EASY FIX!: PLAY MP3

All your problems with Audio, Video and Images.

EASY FIX!: PLAY MP3

Postby ehaydenr » Wed Oct 20, 2010 9:04 pm

Basically i want to create an app that plays a mp3 file thats in my res/raw. I type the code and im having a problem with accessing the file from the R.raw. Its a really easy fix and if u know how to do this please tell me. Everything i have seen online has told me to access it from the R file or get the path name or something. I know a majority of people on this site know how to do this and i would really appreciate it if someone helped me out. In the end i hope to turn this into a sound board.

****************************************************************************************************************************************

Here is my code.....

SoundEffects.java~

package com.example.SoundEffects;



import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class SoundEffects extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);


Context appContext = getApplicationContext();

MediaPlayer mp = MediaPlayer.create(appContext, R.raw.app-5);

mp.start();

Button Button01 = (Button) this.findViewById(R.id.Button01);
Button01.setOnClickListener(new Button.OnClickListener()
{
public void onClick (View v)
{

}
});

Button Button02 = (Button) this.findViewById(R.id.Button02);
Button02.setOnClickListener(new Button.OnClickListener()
{
public void onClick (View v)
{

}
});

Button Button03 = (Button) this.findViewById(R.id.Button03);
Button03.setOnClickListener(new Button.OnClickListener()
{
public void onClick (View v)
{

}
});

Button Button04 = (Button) this.findViewById(R.id.Button04);
Button04.setOnClickListener(new Button.OnClickListener()
{
public void onClick (View v)
{

}
});

Button Button05 = (Button) this.findViewById(R.id.Button05);
Button05.setOnClickListener(new Button.OnClickListener()
{
public void onClick (View v)
{

}
});

Button Button06 = (Button) this.findViewById(R.id.Button06);
Button06.setOnClickListener(new Button.OnClickListener()
{
public void onClick (View v)
{

}
});
}
}

***************************************************************************************************************************************
XML file ~

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="Welcome to Sound Board"/>
<Button android:text="@+id/Button01" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="@+id/Button02" android:id="@+id/Button02" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="@+id/Button03" android:id="@+id/Button03" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="@+id/Button04" android:id="@+id/Button04" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="@+id/Button05" android:id="@+id/Button05" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="@+id/Button06" android:id="@+id/Button06" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="@+id/Button07" android:id="@+id/Button07" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>


</LinearLayout>

*****************************************************************************************************************************************

R.java


/* AUTO-GENERATED FILE. DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found. It
* should not be modified by hand.
*/

package com.example.SoundEffects;

public final class R {
public static final class attr {
}
public static final class drawable {
public static final int icon=0x7f020000;
}
public static final class id {
public static final int Button01=0x7f050000;
public static final int Button02=0x7f050001;
public static final int Button03=0x7f050002;
public static final int Button04=0x7f050003;
public static final int Button05=0x7f050004;
public static final int Button06=0x7f050006;
public static final int Button07=0x7f050007;
public static final int Button09=0x7f050005;
}
public static final class layout {
public static final int main=0x7f030000;
}
public static final class string {
public static final int app_name=0x7f040001;
public static final int hello=0x7f040000;
}
}

****************************************************************************************************************************************
ehaydenr
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Oct 20, 2010 8:52 pm

Top

Re: EASY FIX!: PLAY MP3

Postby blundell » Thu Oct 21, 2010 10:04 am

Post your code in [ code ] tags [ / code ], your more likely to get a reply.

you have three buttons that do nothing when clicked? You also call them button1 button2 button3 which is completely unhelpful to anyone (including yourself) who comes back to this code in a while and wonders what the hell is going on.

You have also pasted your R.java file, this is autogenerated as the comment says so is useless in this context.

If oyu want to play an MP3 there is a perfect example on the developer.android site:

http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/media/MediaPlayerDemo_Audio.html
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: EASY FIX!: PLAY MP3

Postby Opwiz » Thu Oct 21, 2010 3:14 pm

Your resource name can't contain '-' only lower case letters and numbers (and '_' i believe). So that might be the main problem.
Opwiz
Freshman
Freshman
 
Posts: 4
Joined: Fri Oct 15, 2010 3:39 pm

Re: EASY FIX!: PLAY MP3

Postby srikal88 » Mon Oct 25, 2010 6:19 am

Hi ehaydenr,

Your application works fine if an id is created in the R.java file for your source(app-5). As Opwiz said you need to follow some naming standards for the source you are placing in raw folder, it should not contain capital letters and special characters. And you have not assigned any specific function for your buttons.

Here i attached some code for you. Hope it'l help you to some extent
Attachments
Code.txt
It might help you
(766 Bytes) Downloaded 63 times
srikal88
Freshman
Freshman
 
Posts: 4
Joined: Fri Oct 15, 2010 7:44 am

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 14 guests