soundpool没有播放声音

时间:2014-07-26 20:45:58

标签: android soundpool

我正在录制用户的声音并将其保存到SD卡。但是当我通过soundpool播放时,它没有获得文件的正确路径.. 关于这个可以帮助我吗?? M使用AddRecordingToMediaLibrary()函数将此已重新录制的音频文件保存到SD卡。

媒体录制代码:

    protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);

          text = (TextView) findViewById(R.id.text1);
          sp = (Spinner)findViewById(R.id.spinner1);

          ArrayAdapter<String> adp=new ArrayAdapter<String>(this,
               android.R.layout.simple_dropdown_item_1line,singers);

               adp.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
               sp.setAdapter(adp);
          // store it to sd card
         //outFile = Environment.getExternalStorageDirectory().
            //    getAbsolutePath() + "/AudioRecord.3gpp";

         File sampleDir = Environment.getExternalStorageDirectory();
          try {
              outputFile = File.createTempFile("sound", ".m4a", sampleDir);
          } catch (IOException e) {
              Toast.makeText(this, "No Memory Card Inserted", Toast.LENGTH_LONG).show();
              Log.e(TAG, "sdcard access error");
              return;
          }


          myRecorder = new MediaRecorder();
          myRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
          myRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
          myRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
          myRecorder.setOutputFile(outputFile.getAbsolutePath());

          startBtn = (Button)findViewById(R.id.start);
          startBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                start(v);
            }
          });

          stopBtn = (Button)findViewById(R.id.stop);
          stopBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                stop(v);
            }
          });

          playBtn = (Button)findViewById(R.id.play);
          playBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                    play(v);    
            }
          });

          stopPlayBtn = (Button)findViewById(R.id.stopPlay);
          stopPlayBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                stopPlay(v);
            }
          });

          playMod = (Button)findViewById(R.id.button1);
          playMod.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
            playModified(v);    
            }


          });
       }

    public void playModified(View v)
       {

           spl = new SoundPool(1,AudioManager.STREAM_MUSIC,0);
           String path = outputFile.getAbsolutePath();
           Toast.makeText(this, path, Toast.LENGTH_LONG).show();
           explosion = spl.load(path ,1);

          String sing = sp.getSelectedItem().toString();
          if(explosion != 0 )
          {
              if(sing == "Singer1")
                    spl.play(explosion, 0.98F, 0.98F, 1, 0, 1.3F);                        
              else if(sing == "singer2")
                    spl.play(explosion, 0.3F, 0.3F, 0, 0, 2.0F);

              else if(sing == "singer3")
                    spl.play(explosion, 0.8F, 0.8F, 0, 0, 0.6F);
              else 
              {
                  Toast.makeText(this, "No File Found A", Toast.LENGTH_LONG).show();
                  return;
              }       
          }
          else 
          {
              Toast.makeText(this, "No File Found B",Toast.LENGTH_LONG).show();
              return;
          }

       }

 protected void addRecordingToMediaLibrary() {
       ContentValues values = new ContentValues(4);
       long current = System.currentTimeMillis();
       values.put(MediaStore.Audio.Media.TITLE, "audio" + outputFile);
       values.put(MediaStore.Audio.Media.DATE_ADDED, (int) (current / 1000));
       values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/3gpp");
       values.put(MediaStore.Audio.Media.DATA, outputFile.getAbsolutePath());
       ContentResolver contentResolver = getContentResolver();

       Uri base = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
       newUri = contentResolver.insert(base, values);

       sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, newUri));
       Toast.makeText(this, "Added File " + newUri, Toast.LENGTH_LONG).show();
   }

0 个答案:

没有答案