【问题标题】:Playing sound from SDCard with MediaPlayer?使用 MediaPlayer 从 SDCard 播放声音?
【发布时间】:2015-05-26 19:45:21
【问题描述】:

让我解释清楚:

  1. 我有一个名为“Sounds”的文件夹,并且有一些 .ogg 文件可以播放。
  2. 我使用Environment.getExternalStorageDirectory().getAbsolutePath() + mySoundsPath; 获得了文件夹路径。
  3. 我从该文件夹中获取了所有列表并将其保存到数组中:List<String> soundList;

我的问题是:

  1. 如何调用我创建的来自soundList 的声音以便它们都可以播放??
  2. 是否需要解码(如图片,解码为Bitmap)??

对不起我的语法。 提前致谢。

【问题讨论】:

    标签: android android-mediaplayer


    【解决方案1】:

    【讨论】:

    • 感谢您的参考..我使用了该参考中的逻辑.. MediaPlayer 只能从 resID 和 Uri 中获取..如果我使用 resID 是不可能的,所以我尝试将字符串转换为 Uri。 . 它有效.. 再次感谢您的参考.. :D
    【解决方案2】:

    公共无效播放(字符串路径){ 计数++; 播放文件=路径;

                //showNotification();
            new NotificationPanel(activity);
    
    
                if(mediaPlayer!=null && mediaPlayer.isPlaying()){
                    Log.d("*****begin*****", "playing");
                    stopPlaying();
                     Log.d("*****begin*****", "stoping");
                  } else{
                     Log.d("*****begin*****", "nothing");
                  }
    
    
                Uri myUri1 = Uri.parse(path);
                mediaPlayer = new MediaPlayer();
                mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    
                try {
                    mediaPlayer.setDataSource(activity, myUri1);            
                } catch (IllegalArgumentException e) {
                    Toast.makeText(activity, "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                } catch (SecurityException e) {
                    Toast.makeText(activity, "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                } catch (IllegalStateException e) {
                    Toast.makeText(activity, "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                } catch (IOException e) {
                    e.printStackTrace();
                }
    
                try {
                    mediaPlayer.prepare();
                } catch (IllegalStateException e) {
                    Toast.makeText(activity, "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                } catch (IOException e) {
                    Toast.makeText(activity, "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                }
                mediaPlayer.start();
    
            }
    
            private void stopPlaying() { 
                if (mediaPlayer != null) {
                    mediaPlayer.stop();
                    mediaPlayer.release();
                    mediaPlayer = null;
               } 
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多