[VIDEO-Tut] - Playing Media(mp3) on the emulator

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Postby srujanak » Mon Mar 08, 2010 11:01 am

Hello plusminus,
can you tell me how to play online .ogg files on an Android device? i've been trying to play them the same way as online .mp3 files.Media Player refuses to be initialized for ogg. I'm able to play ogg from my raw folder and from my sdcard. Does some extra programming have to be done for playing ogg files from online sources? This is the ogg i've been trying to play.
http://ia360629.us.archive.org/3/items/ ... t_Stop.ogg.

Thanks in advance :D
Srujana
srujanak
Freshman
Freshman
 
Posts: 3
Joined: Thu Jan 21, 2010 8:57 am

Top

Postby JagadishMurugan » Mon Mar 08, 2010 3:11 pm

To start playback after stop(), prepare() should be called prior to calling start();

package com.example.AudioPlayer;

import android.app.Activity;
import android.os.Bundle;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.util.Log;

import java.io.*;

public class AudioPlayerActivity extends Activity
{
/** Called when the activity is first created. */
protected static final int PLAY = 0x101;
protected static final int STOP = 0x102;
protected static final int PAUSE = 0x103;
int State;

public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);

final MediaPlayer mplayer = MediaPlayer.create(AudioPlayerActivity.this,
R.raw.linkinpark);
mplayer.stop();

// PLAY button
Button play = (Button) this.findViewById(R.id.play);
State = STOP;
play.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {

// mp.prepare();
//mplayer.reset();
if (State == STOP)
{
try
{
mplayer.prepare();
}
catch(IOException e)
{
Log.d("AudioPlayer","IOException");
}
}
mplayer.start();
State = PLAY;
mplayer.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer arg0) {
// End of file
}
});
}
});

// PAUSE button
Button pause = (Button) this.findViewById(R.id.pause);
pause.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
mplayer.pause();
State = PAUSE;
}
});

// STOP button
Button stop = (Button) this.findViewById(R.id.stop);
stop.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
mplayer.stop();
State = STOP;
}
});

}
}
JagadishMurugan
Once Poster
Once Poster
 
Posts: 1
Joined: Mon Mar 08, 2010 2:29 pm
Location: Bangalore

Postby trusk » Fri Apr 16, 2010 7:34 pm

Hi, i'm a noob in Android Programming. I did everything exactly as you said, but I get this error
http://yfrog.com/1nerrorxiwj

Please help!
trusk
Freshman
Freshman
 
Posts: 4
Joined: Fri Apr 16, 2010 7:31 pm

Re: [VIDEO-Tut] - Playing Media(mp3) on the emulator

Postby cavmax » Tue Jun 15, 2010 6:52 pm

Hello all, I'm using the following code snippet to play mp3's from resource/raw:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. MediaPlayer mp = MediaPlayer.create(this, R.raw.sound_1);
  2. mp.start();
Parsed in 0.029 seconds, using GeSHi 1.0.8.4

On some devices, for example HTC Hero (stock) this results in playing all mp3's located in resource/raw. In the emulator and on my HTC Desire it works well and only plays the given resource. Any idea what might cause this?
cavmax
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Jun 15, 2010 6:46 pm

Re: [VIDEO-Tut] - Playing Media(mp3) on the emulator

Postby vengeance_mj » Thu Jun 17, 2010 1:25 am

hi plusminus,
thanks for the wonderful tutorial. I tried the example out but it is giving me an IllegalStateException when mp.prepare() is called. What could be the reason for that? I have included a mp3 file which is less than 1 MB

i would appreciate your help
vengeance_mj
Senior Developer
Senior Developer
 
Posts: 174
Joined: Thu Oct 29, 2009 2:32 am

Re: [VIDEO-Tut] - Playing Media(mp3) on the emulator

Postby sudhakar » Sat Oct 23, 2010 1:05 pm

Hi...I used one activity for video streaming from URL.I just wanna know how to skip into another activity automatically when the video stream comes to an end.Anyone who knows please help me.Thank U in advance..
sudhakar
Freshman
Freshman
 
Posts: 3
Joined: Thu Feb 19, 2009 7:45 am

Top

Re: [VIDEO-Tut] - Playing Media(mp3) on the emulator

Postby ajaypatelaj » Mon Apr 11, 2011 1:25 pm

Image

Got this error i d k ? y
please help me...
and thanks for your code .,..:)
ajaypatelaj
Junior Developer
Junior Developer
 
Posts: 22
Joined: Mon Apr 04, 2011 12:54 pm

Re: [VIDEO-Tut] - Playing Media(mp3) on the emulator

Postby dekrisdarutama » Sat May 14, 2011 11:09 am

if I wanted to play music without using onClick, how?
music is heard when the application is started automatically

what should i do ?
thank you :D
User avatar
dekrisdarutama
Freshman
Freshman
 
Posts: 5
Joined: Wed Oct 13, 2010 12:33 pm

Top
Previous

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 7 guests