import java.io.*;
import android.app.*;
import android.content.res.*;
import android.media.*;
import android.os.*;
import android.widget.*;
public class MediaPlayerTest extends Activity
{
MediaPlayer mediaPlayer;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView textView = new TextView(this);
setContentView(textView);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
mediaPlayer = new MediaPlayer();
try
{
AssetManager assetManager = getAssets();
AssetFileDescriptor descriptor = assetManager.openFd("music.ogg");
mediaPlayer.setDataSource(
descriptor.getFileDescriptor(),
descriptor.getStartOffset(),
descriptor.getLength());
mediaPlayer.prepare();
mediaPlayer.setLooping(true);
}
catch(IOException e)
{
textView.setText("Can not load music file." + e.getMessage());
mediaPlayer = null;
}
}
@Override
protected void onResume()
{
super.onResume();
if(mediaPlayer!=null)
{
mediaPlayer.start();
}
}
@Override
protected void onPause()
{
super.onPause();
if(mediaPlayer!=null)
{
mediaPlayer.pause();
if(isFinishing())
{
mediaPlayer.stop();
mediaPlayer.release();
}
}
}
}
import android.app.*;
import android.content.res.*;
import android.media.*;
import android.os.*;
import android.widget.*;
public class MediaPlayerTest extends Activity
{
MediaPlayer mediaPlayer;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView textView = new TextView(this);
setContentView(textView);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
mediaPlayer = new MediaPlayer();
try
{
AssetManager assetManager = getAssets();
AssetFileDescriptor descriptor = assetManager.openFd("music.ogg");
mediaPlayer.setDataSource(
descriptor.getFileDescriptor(),
descriptor.getStartOffset(),
descriptor.getLength());
mediaPlayer.prepare();
mediaPlayer.setLooping(true);
}
catch(IOException e)
{
textView.setText("Can not load music file." + e.getMessage());
mediaPlayer = null;
}
}
@Override
protected void onResume()
{
super.onResume();
if(mediaPlayer!=null)
{
mediaPlayer.start();
}
}
@Override
protected void onPause()
{
super.onPause();
if(mediaPlayer!=null)
{
mediaPlayer.pause();
if(isFinishing())
{
mediaPlayer.stop();
mediaPlayer.release();
}
}
}
}
'코드조각모음' 카테고리의 다른 글
[android011] 캔바스 예제 - 랜덤 색상 배경화면 (0) | 2011.11.06 |
---|---|
[android010] 전체화면 예제 (0) | 2011.11.06 |
[android008] 효과음을 위한 SoundPool 예제 (0) | 2011.11.06 |
[android007] SD카드 예제 (0) | 2011.11.06 |
[android006] 어셋(Asset) 예제 (0) | 2011.11.06 |