Mp3 not playing in Android - android

I create a simple splash screen which has a sound(mp3)
package in.isuru.Hello;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
public class Splash extends Activity {
MediaPlayer player;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//splash is a xml layout
setContentView(R.layout.splash);
//haha is a mp3 file.
player = MediaPlayer.create(Splash.this, R.raw.haha);
player.start();
//Timer to pause 5 seconds before go to next intent
Thread timer = new Thread(){
public void run(){
try{
//player starts playing mp3 file
sleep(5000);
}catch(InterruptedException e){
e.printStackTrace();
}finally{
Intent openStartingPoint = new Intent("in.isuru.HELLO");
startActivity(openStartingPoint);
}
}
};
timer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
player.release();
finish();
}
}
I tried android MediaPlayer not playing mp3 file
too but it didn't work. I consulted Android Documentation too. but nothing seems to work either.

put player.prepare(); before player.start();
See if that works

Related

store audio mp3 files into database and then retrieve on button click android

I am new bee to android. I have some about 120 audio files, which I want to store in database and then on different button clicks the audio files will retrieve which I want to play. I don't know how to do this. I have searched several links but unable to find help. I have little knowledge of SQLite database, any help will be appreciated.
here is my code , I have now only one audio file saved in assets folder.
If this can help.
import java.io.IOException;
import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MyButtonsActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final MediaPlayer mp = new MediaPlayer();
Button c = (Button)findViewById(R.id.btnstop);
Button b = (Button)findViewById(R.id.btnplay);
c.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(mp.isPlaying())
{
mp.stop();
}
}
});
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
try {
mp.reset();
AssetFileDescriptor afd;
afd = getAssets().openFd("audio/001s-ff.mp3");
mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
mp.prepare();
mp.start();
}
catch (IllegalStateException e)
{
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
One thing more , where should I save my 100+ audio files? In assets folder manually or in database? which way will be more light in size?

How to play music in Thread on Android?

I am trying to write code in Android to play music. But i am starting the music in a thread so music is breaking and i need to play the music in thread because i have receive the music name through a Bluetooth socket.
So , the thread will keep listening the socket , to get the updated Music file name. But while playing the music it's breaking and not playing properly.
MyCode:
package com.example.musicexample;
import java.io.IOException;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
Button btnPlay;
MediaPlayer mPlayer;
Thread canThread;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnPlay = (Button) findViewById(R.id.btnPlay);
mPlayer = MediaPlayer.create(getApplicationContext(), R.raw.a);
btnPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
canThread = new Thread(new Runnable() {
#Override
public void run() {
while(!Thread.currentThread().isInterrupted()) {
MainActivity.this.runOnUiThread(new Runnable()
{
public void run()
{
if(mPlayer.isPlaying()){
mPlayer.stop();
mPlayer.reset();
try {
mPlayer.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mPlayer = MediaPlayer.create(getApplicationContext(), R.raw.a);
}
mPlayer.start();
}
});
}
}
});
canThread.start();
}
});
} }
This is the above code , please let me know , how can i play the music properly in a Thread?

Add MediaPlayer object (Song) in Android?

I'm trying to start short song in my first simple app. Everything is working will until I add the "ourSong" object then I am getting the error on the emulator "Unfortunately MyApp has stopped" as I said if i comment "ourSong.start()"+ "ourSong.release()" out, the app works
package com.example.helloworld;
import java.util.Timer;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
public class Splash extends Activity {
MediaPlayer ourSong;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
ourSong = MediaPlayer.create(Splash.this, R.raw.splashsound);
ourSong.start();
Thread timer = new Thread() {
public void run() {
try {
sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
Intent openStartingPoint = new Intent(
"com.example.helloworld.MAINACTIVITY");
startActivity(openStartingPoint);
}
}
};
timer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
ourSong.release();
finish();
}
}
I appreciate any help
Make sure your "raw" folder is located in "res".
For more details have a look at this.
Try it on a real device!
The Problem in my case was the Emulator. I changed to my mobile device (S4) and it works well.

Android MediaPlayer not playing sounds on button click

I'm new to android and I need it for a project I'm working on. I need it to play a sound by clicking a button. I followed online tutorials but my code does not play sounds at all and I'm getting an error on MediaPlayer Error(-19,0). I've tried a lot of the fixes I saw here and I can't seem to make it work. Any help? Here's the code
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
MediaPlayer player;
#Override
protected void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AudioManager audioManager = (AudioManager) getSystemService(MainActivity.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 20, 0);
Button buttonHello = (Button) findViewById(R.id.button1);
buttonHello.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MediaPlayer mp = Medi aPlayer.create(MainActivity.this, R.raw.button);
mp.start();
mp.setOnCompletionListener(new OnCompletion Listener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
};
});
}
});
} catch (Exception e) {
System.out.println("Error!");
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
This links http://marakana.com/forums/android/examples/59.html has the great example to use MediaPlayer class to play song from raw folder.
To play song from files in the memory you can use the following code.
MediaPlayer mp = new MediaPlayer();
mp.reset();
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATA, filename.getAbsolutePath());
Uri selectedImage=RingtoneDownload.this.getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
try {
mp.setDataSource(RingtoneDownload.this,selectedImage);
mp.prepare();
mp.start();
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
This code is working for me while set the source from file.

Main Activity not opening

My app seems to start up properly, with the splash screen and stuff. But when it sleeps for 6 secs and when it supposed to get into the main activity the app crashes any help please?
Here is me code (android.intent.action1.MAINACTIVIVTY, the "action" was purposely changed to "action1")
package com.hellhogone.multitools;
import com.hellhogone.multitools.R;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Window;
import android.view.WindowManager;
public class Splash extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.splash);
MediaPlayer yo = MediaPlayer.create(Splash.this, R.raw.smusic);
yo.start();
Thread timer = new Thread(){
public void run(){
try{
sleep(6000);
}catch(InterruptedException e){
e.printStackTrace();
}finally{
Intent h1 = new Intent("android.intent.action1.MAINACTIVITY");
startActivity(h1);
}
}
};
timer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
You cannot start an activity from another thread than the UI thread. To avoid this problem you can use runOnUiThread() :
}finally{
runOnUiThread(new Runnable() {
public void run() {
Intent h1 = new Intent("android.intent.action1.MAINACTIVITY");
startActivity(h1);
}
});
}

Categories

Resources