Пример #1
0
 GifInfoHandle(AssetFileDescriptor afd, boolean justDecodeMetaData) throws IOException {
   try {
     gifInfoPtr = openFd(afd.getFileDescriptor(), afd.getStartOffset(), justDecodeMetaData);
   } finally {
     afd.close();
   }
 }
Пример #2
0
  public static MediaPlayer create(Context context, int resid) {
    try {
      AssetFileDescriptor afd = context.getResources().openRawResourceFd(resid);
      if (afd == null) return null;

      MediaPlayer mp = new MediaPlayer();
      mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
      afd.close();
      mp.prepare();
      return mp;
    } catch (IOException ex) {
      Log.d(TAG, "create failed:", ex);
      // fall through
    } catch (IllegalArgumentException ex) {
      Log.d(TAG, "create failed:", ex);
      // fall through
    } catch (SecurityException ex) {
      Log.d(TAG, "create failed:", ex);
      // fall through
    } catch (Exception ex) {
      Log.d(TAG, "create failed:", ex);
      // fall through
    }
    return null;
  }
Пример #3
0
 @DSComment("Private Method")
 @DSBan(DSCat.PRIVATE_METHOD)
 @DSGenerator(
     tool_name = "Doppelganger",
     tool_version = "2.0",
     generated_on = "2013-12-30 12:28:26.967 -0500",
     hash_original_method = "3D9FD67EE5212878A40EB00FDD255FB4",
     hash_generated_method = "64022A1AA54923F2F5BCB6D43B903CE2")
 private void openMediaPlayer() throws IOException {
   if (mAudio != null) {
     mAudio.release();
   }
   mAudio = new MediaPlayer();
   if (mUri != null) {
     mAudio.setDataSource(mContext, mUri);
   } else if (mFileDescriptor != null) {
     mAudio.setDataSource(mFileDescriptor);
   } else if (mAssetFileDescriptor != null) {
     // Note: using getDeclaredLength so that our behavior is the same
     // as previous versions when the content provider is returning
     // a full file.
     if (mAssetFileDescriptor.getDeclaredLength() < 0) {
       mAudio.setDataSource(mAssetFileDescriptor.getFileDescriptor());
     } else {
       mAudio.setDataSource(
           mAssetFileDescriptor.getFileDescriptor(),
           mAssetFileDescriptor.getStartOffset(),
           mAssetFileDescriptor.getDeclaredLength());
     }
   } else {
     throw new IOException("No data source set.");
   }
   mAudio.setAudioStreamType(mStreamType);
   mAudio.prepare();
 }
Пример #4
0
 private static boolean hasCodecsForResourceCombo(
     Context context, int resourceId, int track, String mimePrefix) {
   try {
     AssetFileDescriptor afd = null;
     MediaExtractor ex = null;
     try {
       afd = context.getResources().openRawResourceFd(resourceId);
       ex = new MediaExtractor();
       ex.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
       if (mimePrefix != null) {
         return hasCodecForMediaAndDomain(ex, mimePrefix);
       } else if (track == ALL_AV_TRACKS) {
         return hasCodecsForMedia(ex);
       } else {
         return hasCodecForTrack(ex, track);
       }
     } finally {
       if (ex != null) {
         ex.release();
       }
       if (afd != null) {
         afd.close();
       }
     }
   } catch (IOException e) {
     Log.i(TAG, "could not open resource");
   }
   return false;
 }
Пример #5
0
  /**
   * create mediaplayer for music
   *
   * @param pPath the pPath relative to assets
   * @return
   */
  private MediaPlayer createMediaplayer(final String path) {
    MediaPlayer mediaPlayer = new MediaPlayer();

    try {
      if (CocosPlayClient.isEnabled() && !CocosPlayClient.isDemo()) {
        CocosPlayClient.updateAssets(path);
      }
      CocosPlayClient.notifyFileLoaded(path);
      if (path.startsWith("/")) {
        final FileInputStream fis = new FileInputStream(path);
        mediaPlayer.setDataSource(fis.getFD());
        fis.close();
      } else {
        final AssetFileDescriptor assetFileDescritor = this.mContext.getAssets().openFd(path);
        mediaPlayer.setDataSource(
            assetFileDescritor.getFileDescriptor(),
            assetFileDescritor.getStartOffset(),
            assetFileDescritor.getLength());
      }

      mediaPlayer.prepare();

      mediaPlayer.setVolume(this.mLeftVolume, this.mRightVolume);
    } catch (final Exception e) {
      mediaPlayer = null;
      Log.e(Cocos2dxMusic.TAG, "error: " + e.getMessage(), e);
    }

    return mediaPlayer;
  }
Пример #6
0
 private void setDataSourceFromResource(Resources resources, MediaPlayer player, int res)
     throws java.io.IOException {
   AssetFileDescriptor afd = resources.openRawResourceFd(res);
   if (afd != null) {
     player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
     afd.close();
   }
 }
Пример #7
0
  public synchronized long BackgroundMusicCreateFromFile(String fileName, int[] error) {
    if (fileName.equals("")) {
      error[0] = GAUDIO_CANNOT_OPEN_FILE;
      return 0;
    }

    AssetFileDescriptor fd = null;

    if (fileName.startsWith("/")) {
      File file = new File(fileName);
      if (!file.isFile()) {
        error[0] = GAUDIO_CANNOT_OPEN_FILE;
        return 0;
      }
    } else {
      if (patchFile_ != null) fd = patchFile_.getAssetFileDescriptor(fileName);
      if (fd == null && mainFile_ != null) fd = mainFile_.getAssetFileDescriptor(fileName);
      if (fd == null)
        try {
          fd = WeakActivityHolder.get().getAssets().openFd("assets/" + fileName);
        } catch (IOException e) {
        }

      if (fd == null) {
        error[0] = GAUDIO_CANNOT_OPEN_FILE;
        return 0;
      }
    }

    MediaPlayer player = new MediaPlayer();

    int duration = 0;

    try {
      if (fd != null) {
        player.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength());
        fd.close();
      } else {
        player.setDataSource(fileName);
      }
      player.prepare();
      duration = player.getDuration();
      player.release();
      player = null;
    } catch (Exception e) {
      error[0] = GAUDIO_UNRECOGNIZED_FORMAT;
      return 0;
    }

    long gid = nextgid();

    sounds_.put(gid, new Sound(fileName, duration));

    return gid;
  }
  private void openVideo() {
    if (mUri == null || mSurfaceHolder == null) {
      // not ready for playback just yet, will try again later
      return;
    }
    // Tell the music playback service to pause
    // TODO: these constants need to be published somewhere in the framework.
    Intent i = new Intent("com.android.music.musicservicecommand");
    i.putExtra("command", "pause");
    getContext().sendBroadcast(i);

    if (mMediaPlayer != null) {
      mMediaPlayer.reset();
      mMediaPlayer.release();
      mMediaPlayer = null;
    }
    try {
      mMediaPlayer = new MediaPlayer();
      mMediaPlayer.setOnPreparedListener(mPreparedListener);
      mMediaPlayer.setOnVideoSizeChangedListener(mSizeChangedListener);
      mIsPrepared = false;
      Log.v(TAG, "reset duration to -1 in openVideo");
      mDuration = -1;
      mMediaPlayer.setOnCompletionListener(mCompletionListener);
      mMediaPlayer.setOnErrorListener(mErrorListener);
      mMediaPlayer.setOnBufferingUpdateListener(mBufferingUpdateListener);
      mCurrentBufferPercentage = 0;
      if (URLUtil.isAssetUrl(mUri.toString())) { // DST: 20090606 detect asset url
        AssetFileDescriptor afd = null;
        try {
          String path = mUri.toString().substring("file:///android_asset/".length());
          afd = getContext().getAssets().openFd(path);
          mMediaPlayer.setDataSource(
              afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
        } finally {
          if (afd != null) {
            afd.close();
          }
        }
      } else {
        setDataSource();
      }
      mMediaPlayer.setDisplay(mSurfaceHolder);
      mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
      mMediaPlayer.setScreenOnWhilePlaying(true);
      mMediaPlayer.prepareAsync();
      attachMediaController();
    } catch (IOException ex) {
      Log.w(TAG, "Unable to open content: " + mUri, ex);
      return;
    } catch (IllegalArgumentException ex) {
      Log.w(TAG, "Unable to open content: " + mUri, ex);
      return;
    }
  }
Пример #9
0
  public synchronized long BackgroundMusicPlay(long backgroundMusic, boolean paused, long data) {
    Sound sound2 = sounds_.get(backgroundMusic);

    if (sound2 == null) return 0;

    String fileName = sound2.fileName;

    AssetFileDescriptor fd = null;

    if (fileName.startsWith("/")) {
      File file = new File(fileName);
      if (!file.isFile()) return 0;
    } else {
      if (patchFile_ != null) fd = patchFile_.getAssetFileDescriptor(fileName);
      if (fd == null && mainFile_ != null) fd = mainFile_.getAssetFileDescriptor(fileName);
      if (fd == null)
        try {
          fd = WeakActivityHolder.get().getAssets().openFd("assets/" + fileName);
        } catch (IOException e) {
        }

      if (fd == null) return 0;
    }

    MediaPlayer player = new MediaPlayer();

    try {
      if (fd != null) {
        player.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength());
        fd.close();
      } else {
        player.setDataSource(fileName);
      }
      player.prepare();
    } catch (Exception e) {
      return 0;
    }

    long gid = nextgid();

    player.setOnCompletionListener(new OnCompletionListener(this, gid));

    Channel channel = new Channel(gid, player, sound2, data);

    sound2.channels.add(channel);

    channels_.put(gid, channel);

    channel.paused = paused;
    if (!paused) channel.player.start();

    return gid;
  }
Пример #10
0
 /** Prepares given media asset (path from assets directory) for playback. */
 public void prepare(final String asset) {
   reset();
   try {
     AssetFileDescriptor fd =
         GUI.getInstance().getContext().getResources().getAssets().openFd(asset);
     setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength());
     fd.close();
     prepare();
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
  @Override
  public void onCompletion(MediaPlayer mp) {
    // IF RAMADAN PLAY PRAYER, IF PRAYER ALREADY PLAYED, STOP
    if (mSettings.getBoolean(AppSettings.Key.USE_ADHAN)
        && mSettings.getBoolean(AppSettings.Key.IS_RAMADAN)) {
      mMediaPlayer.stop();
      mMediaPlayer.release();
      mMediaPlayer = null;
      AssetFileDescriptor assetFileDescriptor = null;
      if (mPrayerNameString.equalsIgnoreCase(getString(R.string.fajr))) {
        assetFileDescriptor = getResources().openRawResourceFd(R.raw.dua_sehri);
      } else if (mPrayerNameString.equalsIgnoreCase(getString(R.string.maghrib))) {
        assetFileDescriptor = getResources().openRawResourceFd(R.raw.dua_iftar);
      }

      if (assetFileDescriptor == null) {
        stopAlarm();
        return;
      }

      try {
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setDataSource(
            assetFileDescriptor.getFileDescriptor(),
            assetFileDescriptor.getStartOffset(),
            assetFileDescriptor.getLength());
        mMediaPlayer.setAudioStreamType(mAudioStream);
        mMediaPlayer.setOnCompletionListener(
            new MediaPlayer.OnCompletionListener() {
              @Override
              public void onCompletion(MediaPlayer mp) {
                stopAlarm();
              }
            });
        mMediaPlayer.setLooping(false);
        mMediaPlayer.prepare();
        mAlarmOff.postDelayed(
            mStartDua =
                new Runnable() {
                  @Override
                  public void run() {
                    mMediaPlayer.start();
                  }
                },
            3000);

      } catch (Exception e) {
        Log.e("RingAlarmActivity", e.getMessage(), e);
      }
    }
  }
Пример #12
0
 public void setLevelMusic(String fileName) {
   String soundtrack = "sounds/" + fileName;
   if (soundtrack.equals(currentSoundtrack)) return;
   try {
     mediaPlayer.stop();
     mediaPlayer.reset();
     AssetFileDescriptor afd = context.getAssets().openFd(soundtrack);
     currentSoundtrack = soundtrack;
     mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
   } catch (Exception e) {
     Log.e("SNAPPERS", e.getMessage(), e);
     return;
   }
 }
Пример #13
0
 public AndroidMusic(AssetFileDescriptor assetDescriptor) {
   mediaPlayer = new MediaPlayer();
   try {
     mediaPlayer.setDataSource(
         assetDescriptor.getFileDescriptor(),
         assetDescriptor.getStartOffset(),
         assetDescriptor.getLength());
     mediaPlayer.prepare();
     isPrepared = true;
     mediaPlayer.setOnCompletionListener(this);
   } catch (Exception e) {
     throw new RuntimeException("Couldn't load music");
   }
 }
Пример #14
0
  protected void initialize() throws IOException {
    try {
      mp = new MediaPlayer();
      String url = TiConvert.toString(proxy.getProperty(TiC.PROPERTY_URL));
      if (URLUtil.isAssetUrl(url)) {
        Context context = proxy.getTiContext().getTiApp();
        String path = url.substring(TiConvert.ASSET_URL.length());
        AssetFileDescriptor afd = null;
        try {
          afd = context.getAssets().openFd(path);
          // Why mp.setDataSource(afd) doesn't work is a problem for another day.
          // http://groups.google.com/group/android-developers/browse_thread/thread/225c4c150be92416
          mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
        } catch (IOException e) {
          Log.e(LCAT, "Error setting file descriptor: ", e);
        } finally {
          if (afd != null) {
            afd.close();
          }
        }
      } else {
        Uri uri = Uri.parse(url);
        if (uri.getScheme().equals(TiC.PROPERTY_FILE)) {
          mp.setDataSource(uri.getPath());
        } else {
          remote = true;
          mp.setDataSource(url);
        }
      }

      mp.setLooping(looping);
      mp.setOnCompletionListener(this);
      mp.setOnErrorListener(this);
      mp.setOnInfoListener(this);
      mp.setOnBufferingUpdateListener(this);

      mp.prepare(); // Probably need to allow for Async
      setState(STATE_INITIALIZED);

      setVolume(volume);
      if (proxy.hasProperty(TiC.PROPERTY_TIME)) {
        setTime(TiConvert.toInt(proxy.getProperty(TiC.PROPERTY_TIME)));
      }
    } catch (Throwable t) {
      Log.w(LCAT, "Issue while initializing : ", t);
      release();
      setState(STATE_STOPPED);
    }
  }
Пример #15
0
  public void LoadMusic(String file) {
    try {
      AssetFileDescriptor assetDesc = assets.openFd(file);
      if (isMusicPlaying()) Music.stop();
      Music.reset();
      Music.setDataSource(
          assetDesc.getFileDescriptor(), assetDesc.getStartOffset(), assetDesc.getLength());
      // Don't block the main thread.
      Music.prepare();
      Music.setVolume(GameVolume, GameVolume);
      Music.start();

    } catch (Exception e) {
    }
  }
  /**
   * Sets the data source as a content Uri. Call this method before the rest of the methods in this
   * class. This method may be time-consuming.
   *
   * @param context the Context to use when resolving the Uri
   * @param uri the Content URI of the data you want to play
   * @throws IllegalArgumentException if the Uri is invalid
   * @throws SecurityException if the Uri cannot be used due to lack of permission.
   */
  public void setDataSource(Context context, Uri uri)
      throws IllegalArgumentException, SecurityException {
    if (uri == null) {
      throw new IllegalArgumentException();
    }

    String scheme = uri.getScheme();
    if (scheme == null || scheme.equals("file")) {
      setDataSource(uri.getPath());
      return;
    }

    AssetFileDescriptor fd = null;
    try {
      ContentResolver resolver = context.getContentResolver();
      try {
        fd = resolver.openAssetFileDescriptor(uri, "r");
      } catch (FileNotFoundException e) {
        throw new IllegalArgumentException();
      }
      if (fd == null) {
        throw new IllegalArgumentException();
      }
      FileDescriptor descriptor = fd.getFileDescriptor();
      if (!descriptor.valid()) {
        throw new IllegalArgumentException();
      }
      // Note: using getDeclaredLength so that our behavior is the same
      // as previous versions when the content provider is returning
      // a full file.
      if (fd.getDeclaredLength() < 0) {
        setDataSource(descriptor);
      } else {
        setDataSource(descriptor, fd.getStartOffset(), fd.getDeclaredLength());
      }
      return;
    } catch (SecurityException ex) {
    } finally {
      try {
        if (fd != null) {
          fd.close();
        }
      } catch (IOException ioEx) {
      }
    }
    setDataSource(uri.toString());
  }
  private MediaPlayer buildMediaPlayer(Context activity) {
    MediaPlayer mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mediaPlayer.setOnCompletionListener(this);
    mediaPlayer.setOnErrorListener(this);

    AssetFileDescriptor file = activity.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 ioe) {
      Log.w(TAG, ioe);
      mediaPlayer = null;
    }
    return mediaPlayer;
  }
Пример #18
0
  public void playMedia(String path) {

    try {
      AssetFileDescriptor desc = getAssets().openFd(path);
      long start = desc.getStartOffset();
      long end = desc.getLength();
      MediaPlayer mm = new MediaPlayer();
      mm.setDataSource(desc.getFileDescriptor(), start, end);
      mm.prepare();
      mm.setVolume(100, 100);

      mm.start();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      Toast.makeText(getApplicationContext(), "Failed", Toast.LENGTH_SHORT).show();
    }
  }
Пример #19
0
 /** 初始化声音 */
 private void initBeepSound() {
   if (playBeep && mediaPlayer == null) {
     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;
     }
   }
 }
Пример #20
0
 public static void playNotifycationMusic(Context context, String voicePath) throws IOException {
   // paly music ...
   AssetFileDescriptor fileDescriptor = context.getAssets().openFd(voicePath);
   if (mediaPlayer == null) {
     mediaPlayer = new MediaPlayer();
   }
   if (mediaPlayer.isPlaying()) {
     mediaPlayer.stop();
   }
   mediaPlayer.reset();
   mediaPlayer.setDataSource(
       fileDescriptor.getFileDescriptor(),
       fileDescriptor.getStartOffset(),
       fileDescriptor.getLength());
   mediaPlayer.prepare();
   mediaPlayer.setLooping(false);
   mediaPlayer.start();
 }
Пример #21
0
 private void a() {
   this.doCleanUp();
   try {
     this.s = new MediaPlayer();
     if (this.i) {
       this.s.setDataSource(this.c, Uri.parse(this.f));
     } else if (this.k != 0L) {
       final FileInputStream fileInputStream = new FileInputStream(this.f);
       this.s.setDataSource(fileInputStream.getFD(), this.j, this.k);
       fileInputStream.close();
     } else {
       final AssetManager assets = this.getResources().getAssets();
       try {
         final AssetFileDescriptor openFd = assets.openFd(this.f);
         this.s.setDataSource(
             openFd.getFileDescriptor(), openFd.getStartOffset(), openFd.getLength());
         openFd.close();
       } catch (IOException ex2) {
         final FileInputStream fileInputStream2 = new FileInputStream(this.f);
         this.s.setDataSource(fileInputStream2.getFD());
         fileInputStream2.close();
       }
     }
     this.s.setDisplay(this.e);
     this.s.setOnBufferingUpdateListener((MediaPlayer.OnBufferingUpdateListener) this);
     this.s.setOnCompletionListener((MediaPlayer.OnCompletionListener) this);
     this.s.setOnPreparedListener((MediaPlayer.OnPreparedListener) this);
     this.s.setOnVideoSizeChangedListener((MediaPlayer.OnVideoSizeChangedListener) this);
     this.s.setAudioStreamType(3);
     this.s.prepare();
     if (this.g == 0 || this.g == 1) {
       (this.t = new MediaController(this.c))
           .setMediaPlayer((MediaController.MediaPlayerControl) this);
       this.t.setAnchorView((View) this.d);
       this.t.setEnabled(true);
       this.t.show();
     }
   } catch (Exception ex) {
     if (com.unity3d.player.q.a) {
       a("error: " + ex.getMessage() + ex);
     }
     this.onDestroy();
   }
 }
Пример #22
0
  /**
   * to play audio from assets
   *
   * @param audioPath
   * @param context
   * @param leftVolume
   * @param rightVolume
   */
  public static void playAudio(
      String audioPath, Context context, float leftVolume, float rightVolume) {

    final MediaPlayer myAudio = new MediaPlayer();
    myAudio.setOnCompletionListener(
        new MediaPlayer.OnCompletionListener() {
          @Override
          public void onCompletion(MediaPlayer mp) {
            Log.d("myDebug", "myAudio.release()");
            if (myAudio != null) {
              myAudio.release();
            }
          }
        });

    AssetFileDescriptor afd;
    try {
      afd = context.getAssets().openFd(audioPath);
      try {
        myAudio.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
      } catch (IllegalArgumentException e) {
        Log.d("myDebug", "ERROR: myAudio.setDataSource()");
      } catch (IllegalStateException e) {
        Log.d("myDebug", "ERROR: myAudio.setDataSource()");
      } catch (IOException e) {
        Log.d("myDebug", "ERROR: myAudio.setDataSource()");
      }
    } catch (IOException e) {
      Log.d("myDebug", "ERROR: afd = getAssets().openFd(audioPath)");
    }

    try {
      myAudio.prepare();
    } catch (IllegalStateException e) {
      Log.d("myDebug", "ERROR: myAudio.prepare()");
    } catch (IOException e) {
      Log.d("myDebug", "ERROR: myAudio.prepare()");
    }
    myAudio.setLooping(false);
    myAudio.start();
    myAudio.setVolume(leftVolume, rightVolume);
  }
Пример #23
0
  public void playSound(Sound sound) {
    int resource = sound.getResourceId();
    if (mPlayer != null) {
      if (mPlayer.isPlaying()) mPlayer.stop();
      mPlayer.reset();

      try {
        AssetFileDescriptor afd = mContext.getResources().openRawResourceFd(resource);
        if (afd == null) return;
        mPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
        afd.close();
        mPlayer.prepare();
      } catch (IOException | IllegalArgumentException | SecurityException e) {
        Log.e(TAG, e.getMessage());
      }
    } else {
      mPlayer = MediaPlayer.create(mContext, resource);
    }
    mPlayer.start();
  }
Пример #24
0
  public MyPlayer(AssetFileDescriptor url) {
    try {
      mediaPlayer.setDataSource(url.getFileDescriptor(), url.getStartOffset(), url.getLength());
      mediaPlayer.prepare();
      mediaPlayer.start();
      System.out.println("------------------------>playing");
      mediaPlayer.setOnCompletionListener(
          new OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer mp) {
              // TODO Auto-generated method stub
              mediaPlayer.stop();
              mediaPlayer.release();
              System.out.println("------------------------>released");
            }
          });
    } catch (Exception e) {
      // TODO: handle exception
    }
  }
  @Override
  public boolean shouldOverrideResourceLoading(MediaPlayer mediaPlayer, Context context, Uri uri) {
    String scheme = uri.getScheme();
    if (scheme == null) return false;
    if (scheme.equals(AndroidProtocolHandler.APP_SCHEME)) {
      uri = AndroidProtocolHandler.appUriToFileUri(uri);
    }

    scheme = uri.getScheme();
    if (!scheme.equals(AndroidProtocolHandler.FILE_SCHEME)) return false;

    try {
      AssetFileDescriptor afd =
          context.getAssets().openFd(AndroidProtocolHandler.getAssetPath(uri));
      mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());

      return true;
    } catch (Exception e) {
      return false;
    }
  }
  /**
   * Creates the beep MediaPlayer in advance so that the sound can be triggered with the least
   * latency possible.
   */
  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;
      }
    }
  }
Пример #27
0
 private void initBeepSound() {
   AssetFileDescriptor localAssetFileDescriptor;
   if ((this.playBeep) && (this.mediaPlayer == null)) {
     setVolumeControlStream(3);
     this.mediaPlayer = new MediaPlayer();
     this.mediaPlayer.setAudioStreamType(3);
     this.mediaPlayer.setOnCompletionListener(this.beepListener);
     localAssetFileDescriptor = getResources().openRawResourceFd(2131099648);
   }
   try {
     this.mediaPlayer.setDataSource(
         localAssetFileDescriptor.getFileDescriptor(),
         localAssetFileDescriptor.getStartOffset(),
         localAssetFileDescriptor.getLength());
     localAssetFileDescriptor.close();
     this.mediaPlayer.setVolume(0.1F, 0.1F);
     this.mediaPlayer.prepare();
     return;
   } catch (IOException localIOException) {
     while (true) this.mediaPlayer = null;
   }
 }
  @Override
  public void onCreate() {

    if (mp.isPlaying()) {
      mp.stop();
      mp.reset();
    }
    try {

      AssetFileDescriptor afd;
      afd = getAssets().openFd("BMare.mp3");
      mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
      mp.prepare();
      mp.start();
    } catch (IllegalStateException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }

    mp.setLooping(false); // Set looping
  }
Пример #29
0
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mPlayer = new MediaPlayer();

    try {
      AssetFileDescriptor afd =
          getResources().openRawResourceFd(R.raw.boysandgirls); // raw 파일 안에는 소문자만 들어가야 한다.
      mPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
    } catch (Exception e) {
      Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
    }

    btStart = (Button) findViewById(R.id.bt_start);
    btStop = (Button) findViewById(R.id.bt_stop);

    btStart.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            try {
              mPlayer.prepare(); // 재생하기전에 준비과정(꼭 필요하다. - 절차)
              mPlayer.start();
            } catch (Exception e) {
              Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
            }
          }
        });

    btStop.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            mPlayer.stop();
          }
        });
  }
Пример #30
0
  public static void playIntro(Context context) {
    try {
      if (mediaPlayer != null && mediaPlayer.isPlaying()) {
        mediaPlayer.stop();
        mediaPlayer.release();
      }
      mediaPlayer = new MediaPlayer();

      AssetFileDescriptor descriptor = context.getAssets().openFd("intro.mp3");
      mediaPlayer.setDataSource(
          descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
      mediaPlayer.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
      mediaPlayer.setLooping(false);
      mediaPlayer.prepare();
      mediaPlayer.start();

      descriptor.close();

    } catch (Exception e) {
      e.printStackTrace();
    }
  }