Playing Back Audio Streams : Stream « File « Android






Playing Back Audio Streams

   
package app.test;

import java.io.IOException;

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

public class Test 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("a.ogg");
      mediaPlayer.setDataSource(descriptor.getFileDescriptor(),
          descriptor.getStartOffset(), descriptor.getLength());
      mediaPlayer.prepare();
      mediaPlayer.setLooping(true);
    } catch (IOException e) {
      textView.setText(e.getMessage());
      mediaPlayer = null;
    }
  }

  @Override
  protected void onResume() {
    super.onResume();
    if (mediaPlayer != null) {
      mediaPlayer.start();
    }
  }

  protected void onPause() {
    super.onPause();
    if (mediaPlayer != null) {
      mediaPlayer.pause();
      if (isFinishing()) {
        mediaPlayer.stop();
        mediaPlayer.release();
      }
    }
  }
}

   
    
    
  








Related examples in the same category

1.Stream Proxy
2.Copy Stream
3.Ini File Stream Reader
4.Read InputStream fully to a string
5.Read Stream to byte array
6.read String From Stream
7.Read stream Fully
8.Read InputStream with ByteArrayOutputStream
9.get Resource As Stream, Loads the resource from classpath
10.InputStream to byte array, copy Reader and Writer,
11.InputStream that notifies listeners of its progress.
12.String to InputStream
13.Context.getFileStreamPath
14.stream To String
15.stream To Bytes
16.Load/save Int Map Data
17.Write InputStream into a StringBuilder