Streaming

package com.schnee.basics;

import java.io.IOException;

import android.os.Bundle;
import android.app.Activity;
import android.content.res.AssetManager;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.media.AudioManager;
import android.widget.TextView;

final public class MediaPlayerTest extends Activity
{
  private TextView textView;
  private MediaPlayer mediaPlayer;

  @Override
  protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    textView = new TextView(this);
    setContentView(textView);
    setVolumeControlStream(AudioManager.STREAM_MUSIC);
    mediaPlayer = new MediaPlayer();
    try{
      AssetManager assetManager = getAssets();
      AssetFileDescriptor descriptor = assetManager.openFd("mai.ogg");
      mediaPlayer.setDataSource(descriptor.getFileDescriptor(),
        descriptor.getStartOffset(),
        descriptor.getLength());
      mediaPlayer.prepare();
      mediaPlayer.setLooping(true);
    }catch(IOException e){
      textView.setText("Couln't 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.release();
      }
    }
  }
}

Remove all ads