List of usage examples for android.content.res AssetFileDescriptor getFileDescriptor
public FileDescriptor getFileDescriptor()
From source file:com.zetterstrom.android.soundboarder.MainActivity.java
public void playSound(AssetFileDescriptor afd) { if (mMediaPlayer != null) { try {/*from www .j av a 2 s . c o m*/ if (mMediaPlayer.isPlaying()) mMediaPlayer.stop(); mMediaPlayer.reset(); mMediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength()); mMediaPlayer.prepare(); mMediaPlayer.start(); } catch (IllegalArgumentException e) { Toast.makeText(this, "Error Playing Sound Byte", Toast.LENGTH_SHORT).show(); e.printStackTrace(); } catch (IllegalStateException e) { Toast.makeText(this, "Error Playing Sound Byte", Toast.LENGTH_SHORT).show(); e.printStackTrace(); } catch (IOException e) { Toast.makeText(this, "Error Playing Sound Byte", Toast.LENGTH_SHORT).show(); e.printStackTrace(); } } else { Toast.makeText(this, "Error Playing Sound Byte", Toast.LENGTH_SHORT).show(); } }
From source file:org.cyanogenmod.theme.chooser.AudiblePreviewFragment.java
private MediaPlayer initAudibleMediaPlayer(AssetFileDescriptor afd, int type) throws IOException { MediaPlayer mp = mMediaPlayers.get(type); if (mp == null) { mp = new MediaPlayer(); mMediaPlayers.put(type, mp);/*from ww w . j a v a 2s . c o m*/ } else { mp.reset(); } mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength()); mp.prepare(); mp.setOnCompletionListener(mPlayCompletionListener); return mp; }
From source file:com.Anderson.example.games.tanc.MainActivity.java
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy);/* w w w . j a v a 2 s . c o m*/ // Create the Google API Client with access to Games mGoogleApiClient = new GoogleApiClient.Builder(this).addConnectionCallbacks(this) .addOnConnectionFailedListener(this).addApi(Games.API).addScope(Games.SCOPE_GAMES).build(); mGoogleApiClient.connect(); mMainMenuFragment = new MainMenuFragment(); mGameplayFragment = new GameplayFragment(); mWinFragment = new WinFragment(); mListFragment = new com.Anderson.example.games.tanc.ListFragment(); // listen to fragment events mListFragment.setListener(this); mMainMenuFragment.setListener(this); mGameplayFragment.setListener(this); mWinFragment.setListener(this); // add initial fragment (welcome fragment) getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, mMainMenuFragment).commit(); try { AssetFileDescriptor afd = getAssets().openFd("sounds/backgroundSound.mp3"); bg.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength()); bg.prepare(); bg.setLooping(true); bg.setVolume(20, 20); bg.start(); afd = getAssets().openFd("sounds/buttonSound.wav"); mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength()); mp.prepare(); } catch (IOException e) { e.printStackTrace(); } // IMPORTANT: if this Activity supported rotation, we'd have to be // more careful about adding the fragment, since the fragment would // already be there after rotation and trying to add it again would // result in overlapping fragments. But since we don't support rotation, // we don't deal with that for code simplicity. }
From source file:com.google.android.gms.samples.vision.face.sleepAlert.DAssistActivity.java
/** * Initializes the UI and initiates the creation of a face detector. *///from w w w. j ava 2s . c o m @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); mPreview = (CameraSourcePreview) findViewById(R.id.preview); mGraphicOverlay = (GraphicOverlay) findViewById(R.id.faceOverlay); this.jumbleButton = (Button) findViewById(R.id.Button01); this.exitButton = (Button) findViewById(R.id.button); this.mp = new MediaPlayer(); this.tv = (TextView) findViewById(R.id.DisplayMsg); this.layout = (RelativeLayout) findViewById(R.id.topLayout); this.sc = (CameraSourcePreview) findViewById(R.id.preview); this.go = (GraphicOverlay) findViewById(R.id.faceOverlay); AssetManager am = getApplicationContext().getAssets(); AssetFileDescriptor afd = null; try { afd = am.openFd("Song.mp3"); this.mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength()); this.mp.prepare(); } catch (IOException e) { e.printStackTrace(); } // Check for the camera permission before accessing the camera. If the // permission is not granted yet, request permission. int rc = ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA); if (rc == PackageManager.PERMISSION_GRANTED) { createCameraSource(); } else { requestCameraPermission(); } navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(final MenuItem menuItem) { System.out.println("Srini in onNavigationItemSelected part"); // Handle navigation view item clicks here. int id = menuItem.getItemId(); if (id == R.id.nav_slideshow) { // Handle the camera action } else if (id == R.id.nav_manage) { } else if (id == R.id.nav_manage_toggle) { if (checked == 0) { go.setBackgroundColor(Color.TRANSPARENT); checked = 1; } else { go.setBackgroundColor(Color.BLUE); checked = 0; } } DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawer.closeDrawer(GravityCompat.START); return true; } }); }
From source file:com.google.dotorg.crisisresponse.translationcards.RecordingActivity.java
private void startListening() { recordButton.setBackgroundResource(R.drawable.button_record_disabled); listenButton.setBackgroundResource(R.drawable.button_listen_active); recordingStatus = RecordingStatus.LISTENING; mediaPlayer = new MediaPlayer(); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); try {//ww w . j a v a 2 s . c o m if (isAsset) { AssetFileDescriptor fd = getAssets().openFd(filename); mediaPlayer.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength()); fd.close(); } else { mediaPlayer.setDataSource(new FileInputStream(filename).getFD()); } mediaPlayer.prepare(); } catch (IOException e) { Log.d(TAG, "Error preparing audio."); throw new IllegalArgumentException(e); // TODO(nworden): something } mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { finishListening(mp); } }); mediaPlayer.start(); }
From source file:se.droidgiro.scanner.CaptureActivity.java
/** * Creates the beep MediaPlayer in advance so that the sound can be * triggered with the least latency possible. *//* w ww.jav a 2 s.c o m*/ private void initBeepSound() { if (playBeep && mediaPlayer == null) { // The volume on STREAM_SYSTEM is not adjustable, and users found it // too loud, // so we now play on the music stream. setVolumeControlStream(AudioManager.STREAM_MUSIC); mediaPlayer = new MediaPlayer(); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.setOnCompletionListener(beepListener); AssetFileDescriptor file = getResources().openRawResourceFd(R.raw.beep); try { mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength()); file.close(); mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME); mediaPlayer.prepare(); } catch (IOException e) { mediaPlayer = null; } } }
From source file:com.eggwall.SoundSleep.AudioService.java
/** * Start playing the resource specified here. * * @param type Either MUSIC, or WHITE_NOISE. Passing the same ID twice * is a signal to stop playing music altogether. *//*from w w w. j a v a2 s . c om*/ private void play(int type) { if (type == MUSIC) { final MediaPlayer player = tryStartingMusic(); if (player != null) { mPlayer = player; mPlayer.prepareAsync(); // onPrepared will get called when the media player is ready to play. return; } } // Either we weren't able to play custom music, or we were asked to play white noise. final int resourceToPlay; if (type == WHITE_NOISE) { Log.v(TAG, "Playing white noise."); resourceToPlay = R.raw.noise; } else { Log.v(TAG, "Playing included jingle."); resourceToPlay = R.raw.jingle; } try { final AssetFileDescriptor d = getResources().openRawResourceFd(resourceToPlay); if (d == null) { Log.wtf(TAG, "Could not open the file to play"); return; } final FileDescriptor fd = d.getFileDescriptor(); mPlayer = getGenericMediaPlayer(); mPlayer.setDataSource(fd, d.getStartOffset(), d.getLength()); d.close(); // White noise or the default song is looped forever. mPlayer.setLooping(true); } catch (IOException e) { Log.e(TAG, "Could not create a media player instance. Full error below."); e.printStackTrace(); return; } postSuccessMessage(mTypePlaying); mPlayer.prepareAsync(); }
From source file:org.mercycorps.translationcards.activity.RecordingActivity.java
private void startListening() { recordButton.setBackgroundResource(R.drawable.button_record_disabled); listenButton.setBackgroundResource(R.drawable.button_listen_active); recordingStatus = RecordingStatus.LISTENING; mediaPlayer = new MediaPlayer(); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); try {//from w ww.ja va 2 s .c om if (isAsset) { AssetFileDescriptor fd = getAssets().openFd(filename); mediaPlayer.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength()); fd.close(); } else { mediaPlayer.setDataSource(new FileInputStream(filename).getFD()); } mediaPlayer.prepare(); } catch (IOException e) { Log.d(TAG, "Error preparing audio."); throw new IllegalArgumentException(e); // TODO(nworden): something } mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { finishListening(); } }); mediaPlayer.start(); }
From source file:io.github.carlorodriguez.morningritual.MainActivity.java
private void playLegacyNotificationSound() throws IOException { AssetFileDescriptor afd = getResources().openRawResourceFd(R.raw.sound); if (afd == null) throw new IOException(); if (mMediaPlayer != null) { mMediaPlayer.release();/*from ww w . j av a2 s . c om*/ } mMediaPlayer = new MediaPlayer(); mMediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength()); afd.close(); mMediaPlayer.setAudioStreamType(AudioManager.STREAM_NOTIFICATION); mMediaPlayer.prepare(); mMediaPlayer.start(); }
From source file:com.intel.xdk.player.Player.java
public void startAudio(String strRelativeFileURL, boolean shouldLoop) { if (isPlayingPodcast()) { injectJS(// www. ja v a2 s. co m "javascript:var ev = document.createEvent('Events');ev.initEvent('intel.xdk.player.audio.busy',true,true);document.dispatchEvent(ev);"); return; } else if (isPlayingAudio()) { stopAudio(); } soundName = strRelativeFileURL; Object path = pathToFile(strRelativeFileURL); try { try { if (mediaPlayer != null) { mediaPlayer.release(); mediaPlayer = null; } } catch (Exception e) { } mediaPlayer = new MediaPlayer(); mediaPlayer.setLooping(shouldLoop); mediaPlayer.setOnErrorListener(soundOnError); mediaPlayer.setOnCompletionListener(soundOnComplete); //set data source based on type if (path instanceof AssetFileDescriptor) { try { AssetFileDescriptor afd = ((AssetFileDescriptor) path); mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength()); } catch (FileNotFoundException e1) { e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } else { File file = new File((String) path); try { FileInputStream fis = new FileInputStream(file); mediaPlayer.setDataSource(fis.getFD()); } catch (FileNotFoundException e) { e.printStackTrace(); } } mediaPlayer.prepare(); mediaPlayer.seekTo(audioTime); mediaPlayer.setVolume(audioVolume, audioVolume); mediaPlayer.start(); } catch (Exception e) { injectJS( "javascript:var ev = document.createEvent('Events');ev.initEvent('intel.xdk.player.audio.error',true,true);document.dispatchEvent(ev);"); return; } //activity.trackPageView("/intel.xdk.podcast." + getAudioName(soundName) + ".stop"); setPlayingAudio(true); injectJS( "javascript:var ev = document.createEvent('Events');ev.initEvent('intel.xdk.player.audio.start');document.dispatchEvent(ev);"); }